- 4
- 0
- 约7.87千字
- 约 9页
- 2016-09-21 发布于重庆
- 举报
C语言顺序表基本函数
一.无空间限制顺序表
1.宏定义:
#includestdio.h
#includestdlib.h
#define OVERFLOW -2
#define OK 1
#define TRUE 1
#define ERROR 0
#define FALSE 0
#define LIST_SEQLIST_INIT_SIZE 100
#define LIST_SEQLIST_INCR_SIZE 100
#define ElemType ****;
2.结构体:
typedef struct{
ElemType *elem;
int last;
int length;
}Seqlist;
3.基本函数:
int Initlist_Seqlist_ 1(Seqlist *L)
/*初始化链表:1.先决条件:无;2.函数作用:开辟空间并用elem记住首地址,
和初始化链表相关数据last,length。*/
{
(*L).elem=(ElemType *)malloc(LIST_SEQLIST_INIT_SIZE*sizeof(ElemType));
if(!(*L).elem) return OVERFLOW;
(*L).length=LIST_SEQLIST_INIT_SIZE;
(*L).last=0;
return OK;
}
int Initlist_Seqlist_ 2(Seqlist *L)
/*初始化链表:1.先决条件:初始化结构体数据;2.函数作用:删除已有链表,
重新开辟空间并用elem记住首地址,和初始化链表相关数据last,length。*/
{
if((*L).elem)
free((*L).elem);
(*L).elem=(ElemType *)malloc(LIST_SEQLIST_INIT_SIZE*sizeof(ElemType));
if(!(*L).elem) return OVERFLOW;
(*L).length=LIST_SEQLIST_INIT_SIZE;
(*L).last=0;
return OK;
}
int Insert_Seqlist(Seqlist *L)
/*插入数据e到链表上:1.先决条件:初始化结构体数据,原数据按递增顺序排列或无数据;
2.函数作用:不受空间限制地插入数据,插入后数据仍按递增顺序排列。*/
{
ElemType e,*p,*q;
int count;
printf(请输入数据:);
scanf(%d,e);//会因ElemType不同而不同
if((*L).last=(*L).length)
{
(*L).elem=(ElemType *)realloc((*L).elem,(LIST_SEQLIST_INIT_SIZE+LIST_SEQLIST_INCR_SIZE)*sizeof(ElemType));
if((*L).elem==NULL)
return OVERFLOW;
else (*L).length+=LIST_SEQLIST_INCR_SIZE;
}
for(p=(*L).elem,count=(*L).last;e*pcount0;p++,count--);
q=p;
for(p=(*L).elem+((*L).last);p!=q;p--)
*p=*(p-1);
*p=e;
(*L).last++;
return OK;
}
int Delete_Seqlist(Seqlist *L,)
/*删除函数:1.先决条件:无空间限制顺序表;2.函数作用:从顺序表中删除自第i个元素开始的k个元素。*/
{
ElemType *p,*q;
int i,k;
printf(将从顺序表中删除自第i个元素开始的k个元素,请输入i,k(以空格间隔):\n);
scanf(%d%d,i,k);
getchar();
if(i1||i(*L).last)
{
printf(删除起点越界!\n);
return ERROR;
}
if(k0)
{
printf(删除个数有误!\n);
return ERROR;
}
if(i+k-1(*L).last)
{
printf(删除个数越界!\n);
return ERROR;
}
for(p=(*L).elem+i-1,q=p+k;q!=(*L).elem+(*L).last;q++,p++)
*p=*q;
(*L).last=(*L).last-k;
return OK;
}
int Printf_Seqlist(Seqlist *L)
/*显示函数
原创力文档

文档评论(0)