已知两个线性升序表LA,LB,然后合并两个表为LC,并保持升序
来源:互联网 发布:linux安装windows软件 编辑:程序博客网 时间:2024/06/04 23:16
问题一:若两表都是顺序表,则可以运用归并排序的思想直接合并两张表,从而达到时间复杂度最小。
核心代码如下:
Conlink Conbine(Conlink LA,Conlink LB,Conlink LC){cout<<"conbine:";int i=0,j=0,k=0;while(i<=LA.last&&j<=LB.last) //table A and B is not empty{if(LA.date[i]<LB.date[j]) //while is the little,which insert into the C{LC.date[k]=LA.date[i];i++;k++;}else{LC.date[k]=LB.date[j];j++;k++;}}while(i<=LA.last) //to save the rest of the List A{LC.date[k]=LA.date[i];i++;k++;}while(j<LB.last) //to save the rest of the List B{LC.date[k]=LB.date[j];k++;j++;}LC.last=k-1; //to check the lastreturn LC;}
上述核心算法即运用已经有序这一个条件,来合并两表,比一个一个数遍历起来时间复杂度小得多。
完整代码如下:
//creat two Sequential table and conbine the two to the third #include<iostream> using namespace std;typedef int elemtype; //the true type you can define it yourself#define MAXSIZE 100 //you can define it yourselftypedef struct ConLinklist{elemtype date[MAXSIZE];int last;}Conlink;Conlink Creat(Conlink L,int n); //creat new Sequential tableConlink Conbine(Conlink LA,Conlink LB,Conlink LC); //conbine the two tablevoid Output(Conlink L); //to display the twoConlink Creat(Conlink L){int i=0,n;L.last=-1;cout<<"please input the number of the figure in the list:";cin>>n;cout<<endl;for(i=0;i<n;i++){if(L.last<MAXSIZE){ L.last++; cout<<"please input the "<<i+1<<" figure:"; cin>>L.date[L.last]; } } return L;}Conlink Conbine(Conlink LA,Conlink LB,Conlink LC){cout<<"conbine:";int i=0,j=0,k=0;while(i<=LA.last&&j<=LB.last) //table A and B is not empty{if(LA.date[i]<LB.date[j]) //while is the little,which insert into the C{LC.date[k]=LA.date[i];i++;k++;}else{LC.date[k]=LB.date[j];j++;k++;}}while(i<=LA.last) //to save the rest of the List A{LC.date[k]=LA.date[i];i++;k++;}while(j<LB.last) //to save the rest of the List B{LC.date[k]=LB.date[j];k++;j++;}LC.last=k-1; //to check the lastreturn LC;}void Output(Conlink L){int i=0;cout<<"the result is:"<<endl;for(i=0;i<=L.last;i++)cout<<L.date[i]<<" ";cout<<endl;}int main(){Conlink LA,LB,LC;LA=Creat(LA);Output(LA);LB=Creat(LB);Output(LB);LC=Conbine(LA,LB,LC);Output(LC);return 0;}
问题二:若两表为链表,该如何合并:
算法思想:采用归并排序逆向用的最基本的思想来合并,从而保证有序而且时间复杂度为O(m+n)。
具体代码如下:
//to conbine two Linklist to be one#include<iostream>using namespace std;typedef int elemtype;typedef struct ConLinklist{elemtype date;struct ConLinklist *next;}*Linklist,Conlink;Linklist Creat(Linklist L); //creat a new LinklistLinklist Conbine(Linklist LA,Linklist LB); // conbine two Linklistvoid Output(Linklist LD); //to show the linklist//the function to creat two LinklistLinklist Creat(Linklist L,int n){Linklist p,r;int i;L=new(Conlink);L->next=NULL;p=L;for(i=0;i<n;i++){r = new(Conlink);p->next=r;p=r;cout<<"please input the "<<i+1<<" number:";cin>>p->date;}p->next=NULL;return L;}//the function to link the two listLinklist Conbine(Linklist LA,Linklist LB,Linklist LC){ Linklist p,q,r;p=LA->next;q=LB->next;r=LC;cout<<"Conbine:";while(p!=NULL&&q!=NULL)if(p->date<q->date){ r->next=p; r=p; p=p->next;} else{r->next=q;r=q;q=q->next;}while(p){r->next=p;r=p;p=p->next;}while(q){ r->next=q;r=q;q=q->next;}r->next=NULL;return LC;}//the function to display the listvoid Output(Linklist LD){Linklist p;p=LD->next;while(p){cout<<p->date<<" ";p=p->next;}cout<<endl;}//mainint main(){Linklist LA=NULL,LB=NULL,LC;LC=new(Conlink);LA=Creat(LA,6);Output(LA);LB=Creat(LB,3);Output(LB);LC=Conbine(LA,LB,LC);Output(LC);return 0;}代码计算结果为成功合并,其中核心为Conbine函数,链表的构建用的是尾插法,保证和自己输入的升序一样保持升序。
阅读全文
0 0
- 已知两个线性升序表LA,LB,然后合并两个表为LC,并保持升序
- 合并两个升序线性链表
- 合并两个升序链表
- 线性表a、b为两个有序升序的线性表,编写一程序,使两个有序线性表合并成为一个有序升序线性表h
- 两个链表一升一降,合并为一个升序链表。
- 两个升序单向链表的合并
- 合并两个升序链表(递归)
- 合并两个链表成一个升序链表
- 合并两个链表成一个升序链表
- 将La和Lb合并为一个新的线性表Lc的几种结构的归纳
- 数据结构中线性表的基本操作-合并两个线性表-按照元素升序排列
- 线性链表之将单调递增的la和lb中的数据元素按值递增,将la和lb合并为新的线性表lc,使lc中的元素仍然单调递增
- 数据结构 - 有两个链表,第一个升序,第二个降序,合并为一个升序链表(C++)
- 有两个顺序表LA,LB,其元素均为非递减有序排列,编写算法将它们合并成一个顺序表LC,要求LC也是非递减有序排列。
- YTU-OJ-单链表(线性表)-合并两个升序链表后降序输出
- 线性表LA和LB合并
- 已知两个链表 la和 lb,其元素值递增排序。编程将la和lb合并成一个递减有序(相同值元素只保留一个)的链表lc。(北方名校经典试题) 本题选做
- 将la,lb链表合并成lc。
- String.ValueOf和toString区别
- 土木硕士转行互联网小结
- 阿里云SLB配置http跳转https
- iOS 扩展性极强的波浪效果工具类
- awakeFromNib和initWithCoder差别
- 已知两个线性升序表LA,LB,然后合并两个表为LC,并保持升序
- 如果处理服务器与客户端时间不一致的问题
- node+express 后端api
- eclipse linux 下自动提示快捷键
- 分布式数据库, 高级形态 分布式事务数据库
- Hibernate 一级缓存,二级缓存,查询缓存
- Linux进程的共享文件
- 倒计时工具类:PYContDownManager
- android 内存监测工具 DDMS 和mat 工具使用