#include
#include
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}Sqlist;
ElemType InitList_Sq(Sqlist *L)
{
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(!L->elem) exit(1);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
ElemType CreateList(Sqlist *L)
{
int s;
ElemType *newbase;
printf("please input data end with 0: "); scanf("%d",&s); while(s!=0) { if (L->length>=L->listsize) { newbase=(ElemType
*)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase) exit(1);
}
} } L->elem[L->length]=s; L->length++; scanf("%d",&s); return OK; L->elem=newbase; L->listsize+=LISTINCREMENT;
void Mergelist_sqlist(Sqlist *La, Sqlist *Lb,Sqlist *Lc) {
ElemType *pa,*pb,*pc,*pa_last,*pb_last; pa_last=La->elem+La->length-1; pb_last=Lb->elem+Lb->length-1; pa=pa_last;pb=pb_last; Lc->listsize=Lc->length=La->length+Lb->length; pc=Lc->elem=(ElemType*)malloc(Lc->listsize*sizeof(ElemType)); if(!Lc->elem)exit(1); while(pa>=La->elem&&pb>=Lb->elem) { } if(*pa
while(pa>=La->elem)*pc++=*pa--;
while(pb>=Lb->elem)*pc++=*pb--;
}
void print(Sqlist L)
{
int i;
for(i=0;i
printf("%4d",L.elem[i]);
} printf("\n");
void main()
{
Sqlist La;
Sqlist Lb;
Sqlist Lc;
// clrscr();
InitList_Sq(&La);
InitList_Sq(&Lb);
InitList_Sq(&Lc);
CreateList(&La);
CreateList(&Lb);
printf("the datas of list A:\n");
print(La);
printf("the datas of list B:\n");
print(Lb);
printf("the result for mergelisted:\n");
Mergelist_sqlist(&La,&Lb,&Lc); print(Lc);
getch();
}
#include
#include
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}Sqlist;
ElemType InitList_Sq(Sqlist *L)
{
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(!L->elem) exit(1);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
ElemType CreateList(Sqlist *L)
{
int s;
ElemType *newbase;
printf("please input data end with 0: "); scanf("%d",&s); while(s!=0) { if (L->length>=L->listsize) { newbase=(ElemType
*)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase) exit(1);
}
} } L->elem[L->length]=s; L->length++; scanf("%d",&s); return OK; L->elem=newbase; L->listsize+=LISTINCREMENT;
void Mergelist_sqlist(Sqlist *La, Sqlist *Lb,Sqlist *Lc) {
ElemType *pa,*pb,*pc,*pa_last,*pb_last; pa_last=La->elem+La->length-1; pb_last=Lb->elem+Lb->length-1; pa=pa_last;pb=pb_last; Lc->listsize=Lc->length=La->length+Lb->length; pc=Lc->elem=(ElemType*)malloc(Lc->listsize*sizeof(ElemType)); if(!Lc->elem)exit(1); while(pa>=La->elem&&pb>=Lb->elem) { } if(*pa
while(pa>=La->elem)*pc++=*pa--;
while(pb>=Lb->elem)*pc++=*pb--;
}
void print(Sqlist L)
{
int i;
for(i=0;i
printf("%4d",L.elem[i]);
} printf("\n");
void main()
{
Sqlist La;
Sqlist Lb;
Sqlist Lc;
// clrscr();
InitList_Sq(&La);
InitList_Sq(&Lb);
InitList_Sq(&Lc);
CreateList(&La);
CreateList(&Lb);
printf("the datas of list A:\n");
print(La);
printf("the datas of list B:\n");
print(Lb);
printf("the result for mergelisted:\n");
Mergelist_sqlist(&La,&Lb,&Lc); print(Lc);
getch();
}