有序链表的插入
2017-12-02 22:21
232 查看
struct node { int data; node* next; }; node head; int len; node* creat() { head.next=0; node* r=&head; r->next=NULL; node* p; int data; for( int i=0;i<len;i++) { cin>>data; p=new node; p->data=data; p->next=r->next; r->next=p; r=p; } return &head; } void insert( const int& data)//有序递增链表要插入的数据data { node* pre=&head; node* cur=head.next; while(cur) { if(cur->data>data) break; else { pre=cur; cur=cur->next; } } node* obj=new node; obj->data=data; pre->next=obj; obj->next=cur; } void show() { node* p=head.next; while(p) { cout<<p->data; if(p->next) cout<<' '; p=p->next; } cout<<endl; } void discard() { node* p=head.next; while(p) { node* q=p; p=p->next; delete q; } } int main() { int data; while(cin>>len>>data) { creat(); insert(data); show(); discard(); } return 0; }
相关文章推荐
- 编程算法 - 有序双循环链表的插入 代码(C)
- 将节点插入到建立起来的有序链表中
- 保证值有序的链表插入算法
- 数据结构——单链表的创建、逆置、插入、有序表的建立、有序单链表合并等基础操作!!
- [LeetCode] Merge Two Sorted Lists 混合插入有序链表
- 把一个新数插入到一个有序的单链表中
- 将一个值插入到有序的单链表,实现插入、删除、输出等基本操作
- 有序插入时,数组与链表效率比较
- java数据结构-链表实现堆栈 控制插入的方式,成为有序链表
- 重温数据结构:有序循环链表的插入
- 数据结构与算法分析笔记与总结(java实现)--链表9:有序环形链表中插入新结点问题
- [LintCode] Merge Two Sorted Lists 混合插入有序链表
- 有序链表插入与删除——C语言
- 10实现有序双向循环链表的插入操作
- PTA 7-1 有序链表的插入(20 分)
- 有序链表插入新结点的改进
- 链表的有序插入
- 【C与指针】有序单链表插入节点
- 有序插入建立链表 C语言实现
- 有序双向链表的插入 C语言实现