[C/C++]链表的连接
来源:互联网 发布:自然排名优化的方法 编辑:程序博客网 时间:2024/06/05 04:25
运用递归将两个链表进行连接
- 建立2个数据项按从大到小排列的链表,实现2个链表的合并,并输出合并后链表的数据项。
函数代码如下
#include<iostream>using namespace std;struct node{ int data; node *next;};node *head=NULL;node *tail=NULL;node *temp;node *createlist(int n){ int num; cin>>num; head=new node; if(head==NULL) { cout<<"No memory available!"; return NULL; } else { head->data=num; head->next=NULL; tail=head; } for(int i=0;i<n-1;i++) { cin>>num; temp=new node; if(temp==NULL){ cout<<"No memory available!"; return head; } else { temp->data=num; temp->next=NULL; tail->next=temp; tail=temp; } } return head;}void display(node *head){ cout<<"List is :"<<endl; while(head) { cout<<head->data<<" "; head=head->next; } cout<<endl;}node *link(node *a,node *b){ node *c=NULL; if(a==NULL) return b; else if(b==NULL) return a; if(a->data<=b->data) { c=a; c->next=link(a->next,b); } else { c=b; c->next=link(a,b->next); } return c;}int main(){ int n,m; node *a,*b,*c; cout<<"请按从小到大的顺序依次输入两组数据:"<<endl; cout<<"第一组数据长度个数为:"<<endl; cin>>n; cout<<"请输入第一组数据"<<endl; a=createlist(n); cout<<"第二组数据长度个数为:"<<endl; cin>>m; cout<<"请输入第二组数据"<<endl; b=createlist(m); c=link(a,b); cout<<"将两组数据从小到大连接后:"<<endl; display(c); return 0;}
0 0
- [C/C++]链表的连接
- 由“两个链表的连接”思考C
- C连接
- 一组C/C++的连接!
- C语言库函数的连接
- c#,oracle的连接字符串
- Objective-C 字符串的连接
- Objective-C 字符串的连接
- c编译器的连接属性
- java连接C的动态连接库
- 双链在C与MySql连接时的应用
- 复杂链表的复制(C/C++)
- VS2013 C语言(C++)连接linux的MySQL服务器
- VS2010连接MySQL数据库的平台搭建(C/C++)
- 字串的连接最长路径查找(C/C++)
- c的链表
- C#.NET连接oracle的客户端方式
- c实现的连接MYSQL数据库
- Gradle的文件编码设置
- Singleton模式的例子
- Win8 Metro(C#)数字图像处理--2.35图像肤色检测算法
- 牌型种数 ----蓝桥杯(暴力方法)
- vim 安装vim-go 打造GOLANG 专用IDE
- [C/C++]链表的连接
- 黑马程序员_容器
- 论Android中的值传递
- Qt5串口通信(一)
- Valid Palindrome
- hdu 2141 can you find it?
- xcode 6.0模拟器二次运行后文本框键盘问题
- EasyUI的拖动及放置组件
- UVA 11029 Leading and Trailing(大数n^k的前x位高精度问题)(好题)