链表排序问题
2015-08-26 10:36
316 查看
写一个函数 它获取两个传引用的参数,两个参数都是指针变量,分别指向两个链表的表头 而且两个链表都由int类型的值构成,假定两个链表已经排好序了,位于表头的数子是最小的数字,然后从小到大依次排列,函数返回一个新的链表的表头的指针。新链表包含原来两个链表的所有的节点,而且同样按从小到大的顺序排列。
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include"nicai.h"
#include<math.h>
#include<string>
#include<algorithm>
//#include<cmath>
using namespace std;
struct Node
{
int data;
Node *link;
};
Node* thired;
typedef Node* NodePtr;
void head_sert(NodePtr& head,int number)
{
NodePtr temp;
temp=new Node;
temp->data=number;
temp->link =head;
head=temp;
}
void paixu(NodePtr& a,NodePtr&b)
{
thired=new Node;
thired->data =0;
thired->link =NULL;
while((a->link !=NULL)&&(b->link!=NULL))
{if(a->data>b->data)
{
NodePtr temp;
temp=new Node;
temp->data=a->data;
temp->link =thired;
thired=temp;
a=a->link ;
}
else
{
NodePtr temp;
temp=new Node;
temp->data=b->data;
temp->link =thired;
thired=temp;
b=b->link ;
};
};
if(a->link ==NULL)
{
NodePtr temp;
temp=new Node;
temp->data=b->data;
//temp->link =thired;
thired=temp;
}
if(b->link ==NULL)
{
NodePtr temp;
temp=new Node;
temp->data=a->data;
temp->link =thired;
thired=temp;
}
while(thired->link !=NULL)
{
cout<<thired->data;
thired=thired->link ;
}
}
void main()
{
Node* first;
first=new Node;
first->link =NULL;
Node* second;
second=new Node;
second->link =NULL;
for(int i=1;i<8;i=i+2)
{
head_sert(first,i);
};
for(int i=2;i<9;i=i+2)
{
head_sert(second,i);
};
paixu(first,second);
}
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include"nicai.h"
#include<math.h>
#include<string>
#include<algorithm>
//#include<cmath>
using namespace std;
struct Node
{
int data;
Node *link;
};
Node* thired;
typedef Node* NodePtr;
void head_sert(NodePtr& head,int number)
{
NodePtr temp;
temp=new Node;
temp->data=number;
temp->link =head;
head=temp;
}
void paixu(NodePtr& a,NodePtr&b)
{
thired=new Node;
thired->data =0;
thired->link =NULL;
while((a->link !=NULL)&&(b->link!=NULL))
{if(a->data>b->data)
{
NodePtr temp;
temp=new Node;
temp->data=a->data;
temp->link =thired;
thired=temp;
a=a->link ;
}
else
{
NodePtr temp;
temp=new Node;
temp->data=b->data;
temp->link =thired;
thired=temp;
b=b->link ;
};
};
if(a->link ==NULL)
{
NodePtr temp;
temp=new Node;
temp->data=b->data;
//temp->link =thired;
thired=temp;
}
if(b->link ==NULL)
{
NodePtr temp;
temp=new Node;
temp->data=a->data;
temp->link =thired;
thired=temp;
}
while(thired->link !=NULL)
{
cout<<thired->data;
thired=thired->link ;
}
}
void main()
{
Node* first;
first=new Node;
first->link =NULL;
Node* second;
second=new Node;
second->link =NULL;
for(int i=1;i<8;i=i+2)
{
head_sert(first,i);
};
for(int i=2;i<9;i=i+2)
{
head_sert(second,i);
};
paixu(first,second);
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- [C/C++]反转链表
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- C#实现基于链表的内存记事本实例
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解