线性表的合并
时间:2011-09-12
来源:互联网
#include<iostream.h>
#include<stdlib.h>
#include <malloc.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int
typedef struct
{
ElemType *elem;
int length;
int listsize;
}List;
void mergeList(List La,List Lb,List &Lc)
{
//InitList(Lc);
ElemType *pa,*pb,*pc,*pa_last,*pb_last;
pa=La.elem;pb=Lb.elem;
Lc.length=La.length+Lb.length;
pc=Lc.elem=(ElemType*)malloc(Lc.length*sizeof(ElemType));
if(!Lc.elem) exit(0);
pa_last=La.elem+La.length-1;
pb_last=Lb.elem+Lb.length-1;
while(pa<=pa_last&&pb<=pb_last)
{
if(*pa<=*pb)
*pc++=*pa++;
else
*pc++=*pb++;
while(pa<=pa_last)
{
*pc++=*pa++;
}
while(pb<=pb_last)
{
*pc++=*pb++;
}
}
}
void main()
{
List La,Lb,Lc;
int num;
cout<<"请输入第一组元素的个数:";
cin>>num;
La.length=num;
cout<<"请输入第一组数据:";
int *p=new int[num];
for(int i=1;i<=num;i++)
{
cin>>p[i];
cout<<" ";
}
La.elem=p;
cout<<"请输入第二组元素的个数:";
cin>>num;
Lb.length=num;
cout<<"请输入第二组数据:";
int *m=new int [num];
for(i=1;i<=num;i++)
{
cin>>m[i];
cout<<" ";
}
Lb.elem=m;
mergeList(La,Lb,Lc);
for(i=1;i<=Lc.length;i++)
{
cout<<*(Lc.elem+i-1)<<" "<<endl;
}
}
为什么这个程序输出的结果没有将两个线性表按非递减的顺序合并输出呢?
#include<stdlib.h>
#include <malloc.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int
typedef struct
{
ElemType *elem;
int length;
int listsize;
}List;
void mergeList(List La,List Lb,List &Lc)
{
//InitList(Lc);
ElemType *pa,*pb,*pc,*pa_last,*pb_last;
pa=La.elem;pb=Lb.elem;
Lc.length=La.length+Lb.length;
pc=Lc.elem=(ElemType*)malloc(Lc.length*sizeof(ElemType));
if(!Lc.elem) exit(0);
pa_last=La.elem+La.length-1;
pb_last=Lb.elem+Lb.length-1;
while(pa<=pa_last&&pb<=pb_last)
{
if(*pa<=*pb)
*pc++=*pa++;
else
*pc++=*pb++;
while(pa<=pa_last)
{
*pc++=*pa++;
}
while(pb<=pb_last)
{
*pc++=*pb++;
}
}
}
void main()
{
List La,Lb,Lc;
int num;
cout<<"请输入第一组元素的个数:";
cin>>num;
La.length=num;
cout<<"请输入第一组数据:";
int *p=new int[num];
for(int i=1;i<=num;i++)
{
cin>>p[i];
cout<<" ";
}
La.elem=p;
cout<<"请输入第二组元素的个数:";
cin>>num;
Lb.length=num;
cout<<"请输入第二组数据:";
int *m=new int [num];
for(i=1;i<=num;i++)
{
cin>>m[i];
cout<<" ";
}
Lb.elem=m;
mergeList(La,Lb,Lc);
for(i=1;i<=Lc.length;i++)
{
cout<<*(Lc.elem+i-1)<<" "<<endl;
}
}
为什么这个程序输出的结果没有将两个线性表按非递减的顺序合并输出呢?
作者: qishengjie901026 发布时间: 2011-09-12
线性表的合并?
道理应该和归并排序差不多.
道理应该和归并排序差不多.
作者: superdullwolf 发布时间: 2011-09-13
while(pa<=pa_last&&pb<=pb_last)
{
if(*pa<=*pb)
*pc++=*pa++;
else
*pc++=*pb++;
} while(pa<=pa_last)
{
*pc++=*pa++;
}
while(pb<=pb_last)
{
*pc++=*pb++;
}
{
if(*pa<=*pb)
*pc++=*pa++;
else
*pc++=*pb++;
} while(pa<=pa_last)
{
*pc++=*pa++;
}
while(pb<=pb_last)
{
*pc++=*pb++;
}
作者: bullbat 发布时间: 2011-09-13
这样试试看
void mergeList(List La,List Lb,List &Lc)
{
//InitList(Lc);
ElemType *pa,*pb,*pc,*pa_last,*pb_last;
pa=La.elem;pb=Lb.elem;
Lc.length=La.length+Lb.length;
pc=Lc.elem=(ElemType*)malloc(Lc.length*sizeof(ElemType));
if(!Lc.elem) exit(0);
pa_last=La.elem+La.length-1;
pb_last=Lb.elem+Lb.length-1;
while(pa<=pa_last&&pb<=pb_last)
{
if(*pa<=*pb)
*pc++=*pa++;
else
*pc++=*pb++;
}
while(pa<=pa_last)
{
*pc++=*pa++;
}
while(pb<=pb_last)
{
*pc++=*pb++;
}
}
void mergeList(List La,List Lb,List &Lc)
{
//InitList(Lc);
ElemType *pa,*pb,*pc,*pa_last,*pb_last;
pa=La.elem;pb=Lb.elem;
Lc.length=La.length+Lb.length;
pc=Lc.elem=(ElemType*)malloc(Lc.length*sizeof(ElemType));
if(!Lc.elem) exit(0);
pa_last=La.elem+La.length-1;
pb_last=Lb.elem+Lb.length-1;
while(pa<=pa_last&&pb<=pb_last)
{
if(*pa<=*pb)
*pc++=*pa++;
else
*pc++=*pb++;
}
while(pa<=pa_last)
{
*pc++=*pa++;
}
while(pb<=pb_last)
{
*pc++=*pb++;
}
}
作者: liwei3290 发布时间: 2011-09-13
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28