C++实现两个已经排序的链表进行合并
2015-08-25 23:18
701 查看
//定义两个同种单向链表,包含一个整数值和一个指向本节点的类型的指针,该链表中的数据都已经排好序
//编制程序,合并两个链表
#include<iostream.h>
#include<iomanip.h>
struct Node
{
int key;
Node *next;
};
void Create(Node *&);
void Print(Node *);
Node *Compare(Node *,Node *); //合并
int main()
{
Node *list1,*list2,*head;
Create(list1);
cout<<"The list1 is:\n";
Print(list1);
Create(list2);
cout<<"The list2 is:\n";
Print(list2);
cout<<"Compare the link is:\n";
head=Compare(list1,list2);
Print(head);
return 0;
}
void Create(Node *&head) //接受引用,形参head就是指向实参的那个变量的别名
{
Node *pb,*pend;
head=NULL;
pb=pend=new Node;
cout<<"Please input value(input 0 is over!):\n";
cin>>pb->key;
while(pb->key!=0)
{
if(head==NULL)
head=pb;
else
pend->next=pb;
pend=pb;
pb=new Node;
cin>>pb->key;
}
pend->next=NULL;
delete pb;
}
void Print(Node *head)
{
while(head)
{
if(head->next==NULL)
cout<<head->key<<endl;
else
cout<<head->key<<"->";
head=head->next;
}
}
Node *Compare(Node *list1,Node *list2)
{
Node *newhead;
if(list1->key<=list2->key) //设置新的头结点
newhead=list1;
else
newhead=list2;
Node *prev1,*prev2;
prev1=list1;
prev2=list2;
while(list1 && list2) //遍历两条链表开始合并
{
if(prev1->key<=prev2->key)
{
if(prev1->next==NULL)//是最后一个节点时终止循环
break;
list1=prev1->next; //预存下一个节点
if(prev1->next->key>prev2->key)
prev1->next=prev2; //将两个节点联系起来
prev1=list1; //移动节点
}
else
{
if(prev2->next==NULL)
break;
list2=prev2->next;
if(prev2->next->key>prev1->key)
prev2->next=prev1;
prev2=list2; //移动节点
}
}
if(list1==NULL) //list1链表比较短时
prev1->next=prev2;
else
prev2->next=prev1;
return newhead;
}
//编制程序,合并两个链表
#include<iostream.h>
#include<iomanip.h>
struct Node
{
int key;
Node *next;
};
void Create(Node *&);
void Print(Node *);
Node *Compare(Node *,Node *); //合并
int main()
{
Node *list1,*list2,*head;
Create(list1);
cout<<"The list1 is:\n";
Print(list1);
Create(list2);
cout<<"The list2 is:\n";
Print(list2);
cout<<"Compare the link is:\n";
head=Compare(list1,list2);
Print(head);
return 0;
}
void Create(Node *&head) //接受引用,形参head就是指向实参的那个变量的别名
{
Node *pb,*pend;
head=NULL;
pb=pend=new Node;
cout<<"Please input value(input 0 is over!):\n";
cin>>pb->key;
while(pb->key!=0)
{
if(head==NULL)
head=pb;
else
pend->next=pb;
pend=pb;
pb=new Node;
cin>>pb->key;
}
pend->next=NULL;
delete pb;
}
void Print(Node *head)
{
while(head)
{
if(head->next==NULL)
cout<<head->key<<endl;
else
cout<<head->key<<"->";
head=head->next;
}
}
Node *Compare(Node *list1,Node *list2)
{
Node *newhead;
if(list1->key<=list2->key) //设置新的头结点
newhead=list1;
else
newhead=list2;
Node *prev1,*prev2;
prev1=list1;
prev2=list2;
while(list1 && list2) //遍历两条链表开始合并
{
if(prev1->key<=prev2->key)
{
if(prev1->next==NULL)//是最后一个节点时终止循环
break;
list1=prev1->next; //预存下一个节点
if(prev1->next->key>prev2->key)
prev1->next=prev2; //将两个节点联系起来
prev1=list1; //移动节点
}
else
{
if(prev2->next==NULL)
break;
list2=prev2->next;
if(prev2->next->key>prev1->key)
prev2->next=prev1;
prev2=list2; //移动节点
}
}
if(list1==NULL) //list1链表比较短时
prev1->next=prev2;
else
prev2->next=prev1;
return newhead;
}
相关文章推荐
- 链表排序(C语言)选择排序
- Leetcode Sort List 链表排序
- leetCode解题报告之Sort List
- LeetCode Reorder List
- 对链表进行排序
- 链表排序(冒泡、选择、插入、快排、归并、希尔、堆排序)
- 单链表实现冒泡排序算法
- 单向链表的简单使用
- leetcode解题之148. Sort List Java版(对链表排序)
- C语言实现链表之单向链表(十四)链表打印和排序
- LeetCode----Sort List
- 链表的排序
- LeetCode:Sort List
- LeetCode Sort List(链表排序)
- Sort List--LeetCode
- lintcode链表排序
- LeetCode随笔之排序算法
- LeetCode 148. Sort List
- 链表的带参数排序,可以通过姓名、成绩进行升序或降序操作
- C++ <ADT:单链表追加、选择排序、删除>