//----------线性表的动态分配顺序存储结构------------
#include <stdlib.h>
#define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
#define LISTINCREMENT 10 //线性表存储空间的分配增量
typedef int ElemType; //线性表存储数据的类型
typedef struct
{
ElemType *elem; //存储空间基址
int length; //当前长度
int listSize; //当前分配的存储容量(一sizeof(ElemType)为单位)
}SqList;
void initList_Sq(SqList *L);
void destroyList_Sq(SqList *L);
void clearLisr_Sq(SqList *L);
int listEmpty_Sq(SqList L);
int listLength_Sq(SqList L);
int getElem_Sq(SqList L,int i,ElemType *e);
int priorElem_Sq(SqList L,ElemType cur_e,ElemType *pre_e);
int nextElem_Sq(SqList L,ElemType cur_e,ElemType *next_e);
void listInsert_Sq(SqList *L,int i,ElemType e);
void listDelete_Sq(SqList *L,int i,ElemType *e);
int equal(ElemType ve1,ElemType ve2);
int locateListElem(SqList L,ElemType e);
void unionList_Sq(SqList *L,SqList Lb); /* 线形表的合并*/
void mergeList(SqList La,SqList Lb,SqList *Lc); /*线形表的顺序合并*/
void traverseList(SqList L);
void main()
{
SqList L,La,Lc;
int i;
ElemType e;
initList_Sq(&L);
initList_Sq(&La);
initList_Sq(&Lc);
for(i=1;i<=5;i++)
{
listInsert_Sq(&L,i,i);
}
traverseList(L);
printf("/n");
listInsert_Sq(&L,6,6);
traverseList(L);
printf("/n");
listDelete_Sq(&L,6,&e);
traverseList(L);
printf("/n");
for(i=6;i<=10;i++)
{
listInsert_Sq(&La,i-5,i);
}
traverseList(La);
printf("/n");
unionList_Sq(&L,La);
traverseList(L);
destroyList_Sq(&L);
destroyList_Sq(&La);
for(i=1;i<=5;i++)
{
listInsert_Sq(&L,i,2*(i-1)+1);
listInsert_Sq(&La,i,2*i);
}
printf("/n");
traverseList(L);
printf("/n");
traverseList(La);
mergeList(L,La,&Lc);
printf("/n");
traverseList(Lc);
}
void initList_Sq(SqList *L)
{
L->elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem)
{
printf("intialize list failure!!/n");
exit(1);
}
L->length=0;
L->listSize=LIST_INIT_SIZE;
}
void destroyList_Sq(SqList *L)
{
L->elem=NULL;
L->length=0;
L->listSize=0;
}
void clearList_Sq(SqList *L)
{
L->length=0;
}
int listEmpty_Sq(SqList L)
{
if(0==L.length)
{
return 1;
}
return 0;
}
int listLength_Sq(SqList L)
{
return L.length;
}
int getElem_Sq(SqList L,int i,ElemType *e)
{
if(i<1 && i>=L.length)
{
printf("position is false!!/n");
return 1;
}
*e=L.elem[i-1];
return 0;
}
int priorElem_Sq(SqList L,ElemType cur_e,ElemType *pre_e)
{
int i;
for(i=1;i<L.length;i++)
{
if(L.elem[i]==cur_e)
{
break;
}
}
if(i>=1 && i<L.length)
{
*pre_e=L.elem[i-1];
return 0;
}
return 1;
}
int nextElem_Sq(SqList L,ElemType cur_e,ElemType *next_e)
{
int i;
for(i=0;i<L.length-1;i++)
{
if(cur_e==L.elem[i])
{
break;
}
}
if(i>=0 && i<L.length-1)
{
*next_e=L.elem[i+1];
return 0;
}
return 1;
}
void listInsert_Sq(SqList *L,int i,ElemType e)
{
ElemType *newbase=NULL;
ElemType *p,*q;
if(i<1 || i>L->length+1)
{
printf("insert position is false!!/n");
exit(1);
}
if(L->length>=L->listSize)
{
newbase=(ElemType*)realloc(L->elem,(L->listSize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)
{
printf("require space failure!!/n");
exit(1);
}
L->elem=newbase;
L->listSize=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;
}
void listDelete_Sq(SqList *L,int i,ElemType *e)
{
ElemType *p,*q;
if(i<1 || i>L->length)
{
printf("the delete position is error!!/n");
exit(1);
}
p=&(L->elem[i-1]);
*e=*p;
q=&(L->elem[L->length-1]);
for(++p;p<=q;++p)
{
*(p-1)=*p;
}
--L->length;
}
int equal(ElemType ve1,ElemType ve2)
{
if(ve1==ve2)
{
return 1;
}
return 0;
}
int locateListElem(SqList L,ElemType e)
{
int i=1;
ElemType *p=L.elem;
while(i<=L.length && !equal(e,*p++))
{
++i;
}
if(i<=L.length)
{
return i;
}
return 0;
}
void unionList_Sq(SqList *L,SqList Lb)
{
int i,L_len,Lb_len;
ElemType e;
L_len=L->length;
Lb_len=Lb.length;
for(i=1;i<=Lb.length;i++)
{
getElem_Sq(Lb,i,&e);
if(locateListElem(*L,e)==0)
{
listInsert_Sq(L,++L_len,e);
}
}
}
void mergeList(SqList La,SqList Lb,SqList *Lc)
{
int i,j,k,La_len,Lb_len;
ElemType La_e,Lb_e;
i=1;
j=1;
k=0;
La_len=La.length;
Lb_len=Lb.length;
while(i<=La_len && j<=Lb_len)
{
getElem_Sq(La,i,&La_e);
getElem_Sq(Lb,j,&Lb_e);
if(La_e<=Lb_e)
{
listInsert_Sq(Lc,++k,La_e);
++i;
}
else
{
listInsert_Sq(Lc,++k,Lb_e);
++j;
}
}
while(i<=La_len)
{
getElem_Sq(La,i++,&La_e);
listInsert_Sq(Lc,++k,La_e);
}
while(j<=Lb_len)
{
getElem_Sq(Lb,j++,&Lb_e);
listInsert_Sq(Lc,++k,Lb_e);
}
}
void traverseList(SqList L)
{
int i;
if(listEmpty_Sq(L)==1)
{
printf("List is empty!!/n");
return;
}
for(i=0;i<L.length;i++)
{
printf("%d ",L.elem[i]);
if(i+1%8==0)
{
printf("/n");
}
}
}
线形表的动态存储的实现
最新推荐文章于 2023-09-24 22:17:51 发布