数据结构单链表的合并.doc_第1页
数据结构单链表的合并.doc_第2页
数据结构单链表的合并.doc_第3页
数据结构单链表的合并.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

问题描述 假设有两个按元素值递增次序排列的线性表,均以单链表形式存储。请编写算法将这两个单链表归并为一个按元素值递减次序排列的单链表,并计算表长。要求利用原来两个单链表的结点存放归并后的单链表。基本要求用链式存储结构实现存储#include stdafx.h#include iostreamusing namespace std;struct Node int num ;Node *next ; ;Node* Create() /创建单链表 int n=0;Node *p1,*p2,*head; p1=p2=new Node;head=NULL;while (p1-num!=0) if (n=1) head=p1; else p2-next=p1; p2=p1;p1=new Node;cinp1-num; if(p1-numnum&p1-num!=0)coutp1-num;n+; p2-next=NULL; return head; void Print(Node *head) /输出链表Node* p=head;while (p) coutnumnext; coutnext;while (q) r=q-next; q-next=p; p=q;q=r;head-next=NULL;head=p;return head;Node *MergeList(Node *head1,Node *head2) /合并单链表,降序if(head1=NULL)return head2;if(head2=NULL)return head1;Node *head;if(head1-num=head2-num)head=head1;head1=head1-next;elsehead=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;elsetemp-next = head2;head2 = head2-next ;temp =temp-next; if (head1 = NULL) temp-next = head2;if (head2 = NULL)temp-next = head1;return head;int Count(Node *head)/求表长 Node* p=head;int i=0;while (p) i+;p=p-next; return i;int main(int argc, char* argv)Node *p1,*p2;p1,p2=new Node; cout创建单链表1,递增排序,0作为结束符!n;p1=Create();cout单链表1为n;Print(p1);cout*n;cout创建单链表2,递增排序,0作为结束符!n;p2=Create();cout单链表2为:n;Print(p2);cout*n;cout合并单链表为(降序排列):n;Node *p3;p3=MergeList(ReverseList(p1),Reve

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论