线性表数据结构C语言实现
程序流程:
rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;
int InitList(SqList &L)
{
L.elem = (int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem)
exit(OVERFLOW);
L.length =0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
int CreateList(SqList &L)
{
int i;
printf("Input the datas: ");
for(i= 0;i <L.length;i++)
scanf("%d",&L.elem[i]);
return OK;
}
int OutputList(SqList L)
{
int i;
printf("Ouput the datas /n");
for(i= 0;i <L.length;i++)
printf("%d/t",L.elem[i]);
printf("/n");
return OK;
}
int LocateElem(SqList L,int e, int(*compare)(int,int))
{
int j,*p;
j =1;
p = L.elem;
while(j <= L.length && !(*compare)(*p++,e))
++j;
if(j<=L.length)
return j;
else
return 0;
}
int compare(int a,int b)
{
if(a==b)
return 1;
else
return 0;
}
int ListInsert(SqList &L,int i,int e)
{
int *p,*q,*newbase;
if((i<1) || (i >L.length+1))
return ERROR;
if(L.length >= L.listsize)
{
newbase = (int *)realloc(L.elem,(L.length + LISTINCREMENT)*sizeof(int));
if(!newbase)
exit(OVERFLOW);
L.elem = newbase;
L.listsize += LISTINCREMENT;
}
q = &L.elem[i-1];
for(p = &L.elem[L.length-1];p>=q;--p)
*(p+1) = *p;
*q = e;
L.length++;
return OK;
}
void main()
{
int i,n,m;
SqList La;
InitList(La);
printf("/nInput the length of the list La: ");
scanf("%d",&n);
La.length = n;
CreateList(La);
SqList Lb;
InitList(Lb);
printf("/nInput the length of the list Lb: ");
scanf("%d",&m);
Lb.length = m;
CreateList(Lb);
SqList Lc;
InitList(Lc);
SqList Ld;
InitList(Ld);
Ld.length = m + n;
int *pa,*pb,*pd;
int *pa_last,*pb_last;
pa = La.elem;
pb = Lb.elem;
Ld.listsize = Ld.length = La.length + Lb.length;
pd = Ld.elem = (int *)malloc(Ld.listsize*sizeof(int));
if(!Ld.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)
{
*pd++ = *pa++;
}
else
{
*pd++ = *pa++;
}
}
while( pa <= pa_last) *pd++ = *pa++;
while( pb <= pb_last) *pd++ = *pb++;
for(i = 1;i <= Ld.length;i++)
{
int *e;
int j;
e = &Ld.elem[i];
j = LocateElem( Lc, *e,compare);
if(!j)
ListInsert(Lc, Lc.length+1,*e);
}
printf("output the datas of La !/n");
OutputList(La);
printf("output the datas of Lb !/n");
OutputList(Lb);
printf("output the datas of Ld !/n");
OutputList(Ld);
printf("output the datas of Lc !/n");
OutputList(Lc);
}