线性表归并运算
来源:互联网 发布:淘宝app确认订单页面 编辑:程序博客网 时间:2024/06/16 04:34
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define INITSIZE 100 //线性表存储空间的初始分配量
#define LISTINCREMENT 10 //线性表存储空间的分配增量
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int ElemType;
typedef int Status;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
#include<stdlib.h>
#include<malloc.h>
#define INITSIZE 100 //线性表存储空间的初始分配量
#define LISTINCREMENT 10 //线性表存储空间的分配增量
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int ElemType;
typedef int Status;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList(SqList &L)
{//构造一个空的顺序表L。
L.elem = (ElemType * )malloc(INITSIZE*sizeof(ElemType));
if(! L.elem)exit(OVERFLOW);
L.length = 0;
L.listsize = INITSIZE;
return(OK);
}
{//构造一个空的顺序表L。
L.elem = (ElemType * )malloc(INITSIZE*sizeof(ElemType));
if(! L.elem)exit(OVERFLOW);
L.length = 0;
L.listsize = INITSIZE;
return(OK);
}
void Assign(SqList &L)
{//为顺序表L的各元素赋值
int i,N;
printf("Please input the Number of the SqList:");
scanf("%d",&N);
printf("Please input the elements:");
for(i=0;i<N;i++){
scanf("%d",&L.elem[i]);
L.length++;
}
}
{//为顺序表L的各元素赋值
int i,N;
printf("Please input the Number of the SqList:");
scanf("%d",&N);
printf("Please input the elements:");
for(i=0;i<N;i++){
scanf("%d",&L.elem[i]);
L.length++;
}
}
void MergeList(SqList La, SqList Lb, SqList &Lc)
{//已知线性表La和Lb中的数据元素按值非递减排列
//归并得到新表Lc,数值也按值非递减排列
ElemType *pa,*pb,*pc,*pa_last,*pb_last;
pa=La.elem;
pb=Lb.elem;
Lc.listsize=Lc.length=La.length+Lb.length;
pc=Lc.elem=(ElemType*)malloc(Lc.listsize*sizeof(ElemType));
if(!Lc.elem)exit(OVERFLOW);//存储分配失败
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++;//插入La中的剩余元素
while(pb<=pb_last) *pc++=*pb++;//插入Lb中的剩余元素
}//MergeList
{//已知线性表La和Lb中的数据元素按值非递减排列
//归并得到新表Lc,数值也按值非递减排列
ElemType *pa,*pb,*pc,*pa_last,*pb_last;
pa=La.elem;
pb=Lb.elem;
Lc.listsize=Lc.length=La.length+Lb.length;
pc=Lc.elem=(ElemType*)malloc(Lc.listsize*sizeof(ElemType));
if(!Lc.elem)exit(OVERFLOW);//存储分配失败
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++;//插入La中的剩余元素
while(pb<=pb_last) *pc++=*pb++;//插入Lb中的剩余元素
}//MergeList
void ListTraverse(SqList L){
int i;
for(i=0;i<L.length;i++){
printf("%d ",L.elem[i]);
}
printf("\n");
printf("the length is:%d\n",L.length);
}
int i;
for(i=0;i<L.length;i++){
printf("%d ",L.elem[i]);
}
printf("\n");
printf("the length is:%d\n",L.length);
}
int main()
{
SqList L,L1,L2;
InitList(L1);
Assign(L1);
InitList(L2);
Assign(L2);
MergeList(L1,L2,L);
ListTraverse(L);
return 0;
}
{
SqList L,L1,L2;
InitList(L1);
Assign(L1);
InitList(L2);
Assign(L2);
MergeList(L1,L2,L);
ListTraverse(L);
return 0;
}
阅读全文
0 0
- 线性表归并运算
- 线性表-归并算法
- 线性表的归并
- 归并两个线性表
- 线性表的归并
- 线性表的归并排序
- 线性表的线性存储及其运算
- 数据结构 线性表 顺序表的归并
- 线性表顺序表归并算法
- 线性表应用-有序顺序表归并
- 例2-2归并线性表
- 数据结构 线性表 单链表的归并
- 有序线性表的归并算法
- Java数据结构——线性表归并
- 线性表求与运算
- 数据结构-线性表-其他运算
- 线性表的基本运算
- 线性表的基本运算
- Python笔记5
- 收集宝石
- 2D 激光雷达扫描匹配的方法
- zabbix监控sersync
- Ubuntu设置防火墙端口
- 线性表归并运算
- Python frozenset
- Protocol Buffers 在游戏中的应用
- LeetCode-jump-game-ii
- intellij idea十个常用快捷键
- Oracle---备份(backup)
- Js中<%=%>是做什么的
- Maven——maven配置与Myeclipse创建maven项目
- JZOJ5394. 【NOIP2017提高A组模拟10.5】Ping 树上差分 树状数组