欢迎来到天天文库
浏览记录
ID:39578571
大小:157.00 KB
页数:7页
时间:2019-07-06
《数据结构所有实验综合打印版复习版》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、//链表。#include#includeusingnamespacestd;structnode{intnum;node*next;};node*creat()//链表建立{intn=0;node*p1,*p2,*head;p1=p2=newnode;cin>>p1->num;head=NULL;while(p1->num!=0){if(n==0){head=newnode;head->next=p1;}elsep2->next=p1;p2=p1;p1=newnode;cin>>p1->num;n++;}p2->next=N
2、ULL;returnhead;}intlistlength(node*l)//计算长度{node*p=l->next;intcount=0;while(p){count++;p=p->next;}returncount;}intsearch(node*l,intvalue)//链表的查找{node*p=l;intindex=0;while(p){if(p->num==value)returnindex;p=p->next;index++;}return0;}voidprint(node*head){//打印链表node*p=head->next;while(p){c
3、out<num<<"";p=p->next;}cout<next;deletetemp;}cout<<"hadbeendestructed."<next->next;q=p->next;if(p==NULL)return;r=l-
4、>next;r->next=NULL;while(p->next!=NULL){//每次将未逆置的链表中的首结点*p,挂在部分逆置好的链表首结点*r之前,//在改变p->next之前,将p的后继结点存储在q之中p->next=r;r=p;p=q;q=p->next;}p->next=r;//最后一结点的处理l->next=p;}node*Merge(node*head1,node*head2){//合并两个有序链表node*head;if(head1==NULL)returnhead2;if(head2==NULL)returnhead1;if(head1->num
5、<=head2->num){head=head1;head1=head1->next;}else{head=head2;head2=head2->next;}node*temp=head;while(head1!=NULL&&head2!=NULL){if(head1->num<=head2->num){temp->next=head1;head1=head1->next;temp=temp->next;}else{temp->next=head2;head2=head2->next;temp=temp->next;}}if(head1==NULL)temp->ne
6、xt=head2;if(head2==NULL)temp->next=head1;returnhead->next;}node*Delete(node*head,intnum)//删除节点{if(head==NULL){cout<<"ListisNull"<num!=num&&p1->next){p2=p1;p1=p1->next;}if(p1->num==num){if(p1==head){head=p1->next;}elsep2->next=p1->next;}el
7、secout<<"DonotFindTheNum"<num=num;if(head==NULL){head=p0;p0->next=NULL;returnhead;}while(p1->numnum&&p1->next){p2=p1;p1=p1->next;}if(p1->num>=p0->num){if(p1==head)head=p0;elsep2->ne
此文档下载收益归作者所有