预览加载中,请您耐心等待几秒...
1/2
2/2
在线预览结束,喜欢就下载吧,查找使用更方便
如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
#include<iostream>usingnamespacestd;typedefstructl{intdata;structl*next;}T;T*creation()//创建链表{T*head,*p;intf=1,d;head=newT;p=head;cout<<"输入链表中的元素并且以0作为结束标志:"<<endl;//0用于判断结尾cin>>d;head->data=d;do{T*temp=newT;cin>>d;if(d!=0){temp->data=d;p->next=temp;p=temp;}elsef=0;}while(f);p->next=NULL;returnhead;}T*reverse(T*head)//链表的逆置{if(head==NULL||head->next==NULL)returnhead;T*p1,*p2,*p3;p1=head;p2=head->next;do{p3=p2->next;p2->next=p1;p1=p2;p2=p3;}while(p2);head->next=NULL;head=p1;returnhead;}voidshow(T*head)//输出链表{if(head==NULL){cout<<"链表为空\n";}do{cout<<head->data<<"";head=head->next;}while(head!=NULL);cout<<endl;}intmain(){T*head;head=creation();cout<<"输入的链表为:"<<endl;show(head);cout<<"此链表的逆置为:"<<endl;head=reverse(head);show(head);return0;}