《数据结构顺序表相关操作》由会员分享,可在线阅读,更多相关《数据结构顺序表相关操作(9页珍藏版)》请在金锄头文库上搜索。
1、数据结构顺序表相关操作数据结构顺序表相关操作头文件#define OK 1#define ERROR 0#define LIST_INIT_SIZE 100#define LISTINCREMENT 10#define TRUE 1#define FALSE 0#define INFEASIBLE -1#define OVERFLOW -2typedef int Status;typedef int ElemType;typedef structElemType *elem;int length;int listsize;List;Status InitList(List Status Des
2、troyList(List Status GetElem (List L, int i, ElemType Status ListInsert(List Status LocateElem(List L, ElemType e);Status PriorElem(List L,ElemType cur_e,ElemType *pre_e);Status NextElem(List L,ElemType cur_e,ElemType *next_e);Status ListDelete(List #include #include #include “h1.h“功能函数Status InitLi
3、st(List /if(!L.elem) exit(OVERFLOW);L.length = 0;L.listsize=LIST_INIT_SIZE;printf(“初始化一个线性表nn“);return OK;/InitListStatus GetElem (List L, int i, ElemType /i 值不合法e=L.elemi -1;return OK;/ GetElemStatus ListInsert(List if (iL.length+1) return ERROR;if (L.length=L.listsize)newbase=(ElemType*)realloc(L.
4、elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType);if (!newbase) return(OVERFLOW);L.elem=newbase;L.listsize+=LISTINCREMENT;q=for(p=p=q;-p) *(p+1)=*p;*q=e;+L.length;return OK;/ ListInsertStatus ListDelete(List if (iL.length) return ERROR;p=e= *p;q=(L.elem+L.length-1);for (+p;pL.length)return INFEASIBLE
5、;else*pre_e=*-p;return OK;Status NextElem(List L,ElemType cur_e,ElemType *next_e) /* 初始条件:顺序线性表 L 已存在 */* 操作结果:若 cur_e 是 L 的数据元素,且不是最后一个,则用 next_e 返回它的后继, */* 否则操作失败,next_e 无定义 */int i=1;ElemType *p=L.elem;while(iL.length)printf(“输入错误!n“);continue ;GetElem(L,b,e1);if(k=ERROR)printf(“读取数据失败,“,b);else
6、printf(“第%d 个元素的值为%dn“,b,e1);break;case 3:printf(“输入删除第几个数:“);scanf(“%d“,if(cL.length)printf(“输入错误!元素不存在n“);k=ListDelete(L,c,e2); / 删除第 c 个数据if(k=ERROR) / 表中不存在第 c 个数据printf(“删除第%d 个元素失败n “,c);else / 表中存在第 c 个数据,删除成功,其值赋给 e2printf(“删除第%d 个元素成功,其值为%dnn“,c,e2);break;case 4:printf(“请输入要查找的数:“);scanf(“%
7、d“,printf(“要查找元素的位序为%dn“,LocateElem(L,e);break;case 5:printf(“请输入数:“);scanf(“%d“,i=PriorElem(L,e1, /* 求 e1 的前驱 */if(i=INFEASIBLE)printf(“元素%d 无前驱n“,e1);elseprintf(“元素%d 的前驱为:%dn“,e1,e);break;case 6:printf(“请输入数:“);scanf(“%d“,i=NextElem(L,e1, /* 求 e1 的后继 */if(i=INFEASIBLE)printf(“元素%d 无后继n“,e1);elseprintf(“元素%d 的后继为:%dn“,e1,e);break;case 0:printf(“操作结束!“); break;default: printf(“输入选择出错!n“);/ end of switchwhile(select!=0); /end of while/ end of main