数据结构word版
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#define LIST_INT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}Sqlist;
Status InitList_Sq(Sqlist &L)
{
L.elem=(ElemType*)malloc(LIST_INT_SIZE*sizeof(ElemType));
L.length=0;
L.listsize=LIST_INT_SIZE;
return 1;
}
void scan_Sq(Sqlist &L)
{
int i;
printf("请输入线性表的元素个数:");
scanf("%d",&L.length);
for(i=0;i scanf("%d",&L.elem[i]); } void print_Sq(Sqlist &L) { int i; for(i=0;i printf("%4d",L.elem[i]); } void MergeList_Sq(Sqlist &La,Sqlist &Lb,Sqlist &Lc) { int i,j,k; 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)); 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() { Sqlist La,Lb,Lc1; ElemType e; InitList_Sq(La); InitList_Sq(Lb); InitList_Sq(Lc1); scan_Sq(La); printf("A线性表的元素为:"); print_Sq(La); printf("\n"); scan_Sq(Lb); printf("B线性表的元素为:"); print_Sq(Lb); printf("\n"); printf("则A,B线性表的合并为:"); MergeList_Sq(La,Lb,Lc1); print_Sq(Lc1); printf("\n"); }