| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 304 人关注过本帖
标题:两线性表元素按原顺序合并
只看楼主 加入收藏
winner1995
Rank: 2
等 级:论坛游民
帖 子:64
专家分:41
注 册:2015-10-11
结帖率:88.89%
收藏
 问题点数:0 回复次数:0 
两线性表元素按原顺序合并
#include<iostream.h>
#include <stdlib.h>
#define maxsize 20
int n;
typedef struct
{int * data;
int length;
int listsize;
}sqlist;
void createsqlist(sqlist &list)
{list.data=(int*)malloc(sizeof(int)*maxsize);
if(!list.data)
exit(1);
list.length=0;
list.listsize=maxsize;
}
void initsqlist(sqlist &list)
{int i;
  int * p;
cout<<"plese enta the number of data"<<endl;
cin>>n;
p=list.data;
cout<<"please enta your data in order"<<endl;
for(i=0;i<n;i++,p++)
{cin>>*p;
list.length++;}
cout<<"your list is"<<endl;
p=list.data;
for(i=0;i<n;i++,p++)
cout<<*p<<"\t";
cout<<endl;
}
void mergelist(sqlist &La,sqlist &Lb)
{sqlist Lc;
int *p1,*p2,*p3,*p1_last,*p2_last;
p1=La.data;
p2=Lb.data;
Lc.listsize=Lc.length=La.length+Lb.length;
Lc.data=(int*)malloc(sizeof(int)*(Lc.listsize));
p3=Lc.data;
p1_last=p1+La.length-1;
p2_last=p2+Lb.length-1;
if(!Lc.data)
exit(1);
while(p1<=p1_last&&p2<=p2_last)
{if(*p1<=*p2)
*p3++=*p1++;
else
*p3++=*p2++;
}
while(p1<=p1_last)
*p3++=*p1++;
while(p2<=p2_last)
*p3++=*p2++;
cout<<"combining has finished"<<endl;
cout<<"your new list is:"<<endl;
p3=Lc.data;
for(int i=0;i<Lc.length;i++,p3++)
cout<<*p3<<"\t";
cout<<endl;
}
int main()
{sqlist La,Lb;
createsqlist(La);
initsqlist(La);
createsqlist(Lb);
initsqlist(Lb);
mergelist(La,Lb);
return 0;
}
2015-10-22 21:26
快速回复:两线性表元素按原顺序合并
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040143 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved