完整单链表的建立,输入,输出,转变
完整单链表的建立,输入,输出,转变
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include"iostream.h" #include"stdlib.h" typedef int elemtype; typedef struct londe{ elemtype data; struct londe *next; }londe,*linklist; void create(linklist &L){ int i,j; linklist p,q; cout<<"please input the number of node:"; cin>>i; L=p=(linklist)malloc(sizeof(londe)); for(j=1;j<=i;j++){ q=(linklist)malloc(sizeof(londe)); cout<<"please input a data of i="<<j<<":"; cin>>q->data; p->next=q; p=p->next; };q->next=NULL;} void turnover(linklist L) { linklist p,q; q=L->next;L->next=NULL; while(q){p=q->next;q->next=L->next;L->next=q;q=p;} } void output(linklist &L) { linklist p; p=L->next; while(p){cout<<p->data<<"->";p=p->next;} cout<<endl;} void main() { linklist L1; create(L1); output(L1); turnover(L1); output(L1); }
|