c语言数据结构线性表之顺序表功能函数
2016-10-30 20:21
387 查看
/[b]**[/b]数据结构—–线性表——顺序表—[b]**************[/b]/
/创建一个顺序表**/
/初始化操作,建立一个新的线性表*/
/若线性表为空,返回TRUE,否则返回FALSE***/
/将线性表的第i个元素返回给e*********/
/在线性表中查找与给定值e相等的元素,如果查找成功,返回该元素在表中的序号表示成功,否则返回0表示失败****/
/在线性表sqlist中的第i个位置插入元素e****************/
/删除线性表sqlist中的第i个元素,并用e返回其值****/
#include "stdio.h" #include "stdlib.h" #define ERROR 0 #define FALSE 0 #define OK 1 #define TRUE 1 #define MAXSIZE 20 typedef int Elemtype; typedef int Status;
/创建一个顺序表**/
typedef struct { Elemtype data[MAXSIZE]; int length; }Sqlist;
/初始化操作,建立一个新的线性表*/
Status ListInit(Sqlist *sqlist) { int i; //i = sqlist ->length; for(i=0;i<sqlist->length;i++) { sqlist->data[i]=0; } sqlist->length = 0; return OK ; }
/若线性表为空,返回TRUE,否则返回FALSE***/
Status ListEmpty(Sqlist sqlist) { if(sqlist.length == 0) return TRUE ; else return ERROR; }
/将线性表的第i个元素返回给e*********/
Status GetElem(Sqlist sqlist,int i,Elemtype *e) { if(i<=0||i>MAXSIZE||sqlist.length==0) { return ERROR; } *e = sqlist.data[i-1];//第i个元素在数组的第i-1个位置 return OK; }
/在线性表中查找与给定值e相等的元素,如果查找成功,返回该元素在表中的序号表示成功,否则返回0表示失败****/
Status LocateElem(Sqlist sqlist,Elemtype e ) { int i,length1=0; for(i=0;i<sqlist.length;++i) { length1+=1; if(sqlist.data [i] == e) { return length1; } } return FALSE; }
/在线性表sqlist中的第i个位置插入元素e****************/
Status InsertList(Sqlist *sqlist,int i,Elemtype e ) { int f; if(i<1||i>sqlist->length+1 )//当i不在范围时 { return FALSE; } if(sqlist->length==MAXSIZE) { return FALSE; } if(i<sqlist->length)//i不是最后一个 { for(f=sqlist->length-1;f>=i-1;--f) { sqlist->data[f+1]=sqlist->data[f]; } } sqlist->data[i-1] = e;//插入 sqlist ->length ++; return OK ; }
/删除线性表sqlist中的第i个元素,并用e返回其值****/
Status DeleteList(Sqlist *sqlist,int i,Elemtype *e) { int f; if(i>sqlist->length||i<1)//i的取值范围 { return ERROR; } if(sqlist->length == 0) { return ERROR; } *e = sqlist->data[i-1]; if(i<sqlist->length) { for(f=i;f<sqlist->length;++f) { sqlist->data[f-1] =sqlist->data[f]; } } sqlist->length -= 1; return OK; }
int main() { Sqlist a; Status status; Elemtype find,e1,e2; int f1; printf("hello word!!\n\n"); a.length =0; a.data[2] = 12; a.length=3; status = ListEmpty(a); printf("a.data[2]=%d\na.length=%d\nstatus=%d\n\n",a.data[2],a.length,status); ListInit(&a); status = ListEmpty(a); printf("a.data[2]=%d\na.length=%d\nstatus=%d\n\n",a.data[2],a.length,status); a.data[2] = 12; a.length=3; GetElem(a,3,&e1); printf("a.data[2]=%d\ne1=%d\n\n",a.data[2],e1); ListInit(&a); for(f1=0;f1<MAXSIZE-1;++f1) { a.data [f1] = f1; a.length+=1; } find = LocateElem(a,8); if(find!=0) printf("被查元素在线性表的第%d个位置\na.length = %d\n\n",find,a.length); InsertList(&a,4,12); printf("a.length=%d\n\n",a.length); DeleteList(&a,5,&e2); printf("%d已被删除\n\n",e2); system("pause"); return 0; }
相关文章推荐
- c语言数据结构之顺序表单链表的创建及功能函数
- c语言数据结构之顺序表单循环链表的创建及功能函数
- c语言数据结构之顺序表静态链表的创建及功能函数
- (单向链表)数据结构的创建和功能函数(C语言)
- C语言中函数参数的入栈顺序
- C语言中函数参数的入栈顺序
- 数据结构C语言实现系列——线性表
- 数据结构C语言实现系列——线性表
- c语言经典的排序函数swap():输入的两个整数按大小顺序输出。
- C语言中函数参数入栈的顺序
- C语言中函数参数的入栈顺序
- 数据结构--顺序线性表
- C语言中函数参数的入栈顺序
- 数据结构-线性表-顺序表示 (二)
- 数据结构 c语言实现 顺序表(Sequential List)
- 数据结构线性表之顺序表实现
- 数据结构C语言实现系列——线性表
- C语言实现数据结构中的顺序栈
- C语言中strstr函数功能及用法
- 数据结构-线性表-顺序表示 (一)