资源预览内容
第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
第9页 / 共13页
第10页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
数据结构数据结构 C+C+顺序表顺序表#include #include #include #include using namespace std;#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2#define LIST_INIT_SIZE 100#define LISTINCREMENT 10typedef int Status;typedef structint *elem;int length;int listsize;SqList;/线性表的顺序存储结构Status InitList_Sq(SqList L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int);if(!L.elem)/存储分配失败coutListLength_Sq(L) return ERROR;/i 值不合法e=L.elemi-1;return OK;Status compare(int a,int b)/两个元素的关系比较,若 a=b,返回 TURE,否则返回 FALSEif(a=b)return TRUE;elsereturn FALSE;int LocateElem_Sq(SqList L,int e,Status(* compare)(int ,int)/在顺序线性表 L 中查找第 1 个值与 e 满足 compare()的元素的位序,若找到,返回位序,否则返 0int i=1,*p=L.elem;while(iL.length+1) return ERROR;if(L.length=L.listsize)/ 当前存储空间已满,增加容量int *newbase=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof (int);if(!newbase)/存储分配失败cout=q;-p)/插入位置及之后的元素右移*(p+1)=*p;*q=e;/插入 e+L.length;/表长增 1return OK;Status ListDelete_Sq(SqList if(iL.length) return ERROR;/i 值不合法p=/p 为被删除元素的位置e=*p;/被删除元素的值赋给 eq=L.elem+L.length-1;/表尾元素的位置for(+p;pc;while(c13);return c;int main()SqList L;int i,e,cur_e,next_e,pre_e,n;for(;)switch(main_menu()case 1: InitList_Sq(L);couti;if(GetElem_Sq(L,i,e)coute;i=LocateElem_Sq(L,e,compare);if(i)coutcur_e;if(PriorElem_Sq(L,cur_e,pre_e)coutcur_e;if(NextElem_Sq(L,cur_e,next_e)couti;coute;couti;cout“ntt 删除前线性表各元素为:nn“;ShowList_Sq(L);if(ListDelete_Sq(L,i,e)cout“ntt 删除后线性表各元素为:nn“;ShowList_Sq(L);cout“ntt 删除的元素值为:“eendl;cout“nntt“;system(“pause“);break;elsecout“ntt 输入的 i 值不合法!无法删除n“;cout“nntt“;system(“pause“);break;case 11:system(“cls“);if(ListEmpty_Sq(L)cout“nnnttt 线性表为空n“;cout“nntt“;system(“pause“);break;elsecout“nnntt 线性表非空,其各元素值为:nn“;ShowList_Sq(L);cout“nntt“;system(“pause“);break;case 12:system(“cls“);cout“nnntt 线性表各元素为:n“;ShowList_Sq(L);cout“nntt“;system(“pause“);break;case 13:system(“cls“);cout“nntt“;system(“pause“);exit (0);return 0;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号