//带头结点的单链表 #include <iostream> #include <stdlib.h> using namespace std; typedef int ElemType; typedef struct Node { ElemType data; struct Node *next; }LNode,*LinkList; void InitList(LinkList *L); void Create_head(LinkList l); void Create_rear(LinkList l); void Output(LinkList l); void ReverseList(LinkList l); LNode *Pos(LinkList l,int i); void Insert_Order(LinkList &l,int x); LinkList MergeList(LinkList La,LinkList Lb); int ListLength(LinkList l); void SortList(LinkList l); void ShowMenu() { cout<<"——————————————————————————————"<<endl; cout<<"1、初始化一个单链表"<<endl; cout<<"2、用头插法建立一个单链表"<<endl; cout<<"3、用尾插法建立一个单链表"<<endl; cout<<"4、输出一个单链表"<<endl; cout<<"5、反转一个单链表"<<endl; cout<<"6、按位置查找某节点"<<endl; cout<<"7、在有序单链表中有序插入一个数据"<<endl; cout<<"8、获取单链表的长度"<<endl; cout<<"9、对该单链表排序"<<endl; cout<<"10、合并两个单链表"<<endl; cout<<"0、退出"<<endl; cout<<"——————————————————————————————"<<endl; } int main() { ShowMenu(); int n; LinkList l; while(1) { cout<<"请输入你要操作的元素序号:"; cin>>n; switch(n) { case 1: { InitList(&l); break; } case 2: { Create_head(l); break; } case 3:{ Create_rear(l); break; } case 4:{ Output(l); break; } case 5:{ ReverseList(l); break; } case 6:{ cout<<"请输入你要查找的位置:"; int m; cin>>m; LNode *p = Pos(l,m); if(p) cout<<"该位置的值为:"<<p->data<<endl; break; } case 7:{ cout<<"请输入你要插入的元素:"; int x; cin>>x; Insert_Order(l,x); cout<<"插入后输出该单链表:"; Output(l); break; } case 8:{ cout<<"该单链表长度为:"<<ListLength(l)<<endl; break; } case 9:{ SortList(l); cout<<"输出排序后的链表:"; Output(l); break; } case 10:{ LinkList l1,l2; InitList(&l1); InitList(&l2); Create_rear(l1); Create_rear(l2); SortList(l1); SortList(l2); LinkList p = MergeList(l1,l2); cout<<"输出合并以后的链表:"; Output(p); break; } case 0:{ exit(0); break; } default:{ cout<<"非法的数字序号!\n"; } } } return 0; } void InitList(LinkList *L) { *L = new LNode; (*L)->next = NULL; } void Create_head(LinkList l) { int m ; while(1) { cin>>m; if(m==-1) break; LNode *s = new LNode; s->data = m; s->next = l->next; l->next = s; } } void Create_rear(LinkList l) { cout<<"请依次输入数据,并以-1作为结束标记:\n"; int m; LNode *p = l; while(1) { cin>>m; if(m==-1) break; LNode *s = new LNode; s->data = m; p->next = s; p = s; } p->next = NULL; } void ReverseList(LinkList l) { LNode *p = l->next; //p为原链表的当前处理节点 l->next = NULL; //逆置单链表初始为空 LNode *q; while(p) { q = p->next; //q保存当前处理节点的下一节点 p->next = l->next; l->next = p; p = q; } } /* LNode *Pos(LinkList l,int i) { if(i<=0) return NULL; LNode *p = l->next; int j = 1; while(j<i && p->next) { j++; p = p->next; } if(i==j) return p; else return NULL; } */ LNode *Pos(LinkList l,int i) { if(i<0) return NULL; LNode *p = l; while(i--) { p = p->next; if(p == NULL) { return NULL; break; } } return p; } //有序插入 void Insert_Order(LinkList &l,int x) { LNode *s = new LNode; LNode *p,*pre; s->data = x; s->next = NULL; if(l->next == NULL) l->next = s; if(x <= l->next->data) { s->next = l->next; l->next = s; } else { p = l->next; while(p && x>p->data) { pre = p; p = p->next; } s->next = pre->next; pre->next = s; } } LinkList MergeList(LinkList La,LinkList Lb) { LNode *pa,*pb; LinkList Lc; pa = La->next; pb = Lb->next; Lc = La; Lc->next = NULL; LNode *r = Lc; while(pa && pb) { if(pa->data<=pb->data) { r->next = pa; r = pa; pa = pa->next; } else { r->next = pb; r = pb; pb = pb->next; } } if(pa) r->next = pa; else r->next = pb; delete Lb; return Lc; } int ListLength(LinkList l) { LNode *p = l->next; int len = 0; while(p) { len++; p = p->next; } return len; } void SortList(LinkList l) { LinkList p; int n,i,j; int temp; n = ListLength(l); if(n==0) return; p = l->next; for(i=1;i<n;i++) { p = l->next; for(j=0;j<n-j;j++) { if(p->data > p->next->data) { temp = p->data; p->data = p->next->data; p->next->data = temp; } p = p->next; } } } void Output(LinkList l) { LNode *p; p=l->next; while(p) { cout<<p->data<<" "; p = p->next; } cout<<"\n"; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/huolang_vip/article/details/46852159