数据结构(C语言版)算法序列表的插入删除,伪码转实码

#include
#include


#define LIST_INIT_SIZE  50
#define LISTINCREMENT   5


typedef int ElemType;
typedef int Status;


typedef struct
{
int *elem;
int length;
int listsize;
}SqList;


int  InitList_Sq(SqList *L)
{
L->elem=(int *)malloc(LIST_INIT_SIZE * sizeof(int));
if(!L->elem)exit(-1);
L->length=0;
L->listsize= LIST_INIT_SIZE;
return 0 ;
}


int ListInsert_Sq(SqList *L,int pos,ElemType e)
{
ElemType  *newbase,*p,*q;
if(pos<1||pos>L->length+1) return (-1);
if(L->length>=L->listsize)
{
newbase=(ElemType *)realloc(L->elem,(L->listsize+ LISTINCREMENT)*sizeof(ElemType));
if(!newbase)exit(-1);
L->elem=newbase;
L->listsize+= LISTINCREMENT;
}
q=&(L->elem[pos-1]);
for(p=&(L->elem[L->length-1]);p>=q;--p)*(p+1)=*p;
*q=e;
++(L->length);
return 1;


}






void traverse(SqList L)
{
int i;
printf("The list data is: \n");
for(i=0;i printf("%5d",L.elem[i]);
printf("\n");
}


int ListDelete_Sq(SqList *L,int i)
{
ElemType *p,*q;
ElemType e;
if((i<1)||(i> L->length))return -1;
p=&((L->elem)[i-1]);
e=*p;
q=L->elem+L->length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--(L->length);
return e;
}


int main(void)
{
int i,e;
SqList L;
InitList_Sq(&L);
for (i=1;i<=10;i++)
ListInsert_Sq(&L,i,i);
traverse(L);
e=ListDelete_Sq(&L,5);
printf("Delete elem vaule is :%d\n",e);
traverse(L);
system("pause");
return 0;
}

你可能感兴趣的:(数据结构(C语言版)算法序列表的插入删除,伪码转实码)