数据结构c++顺序表

上传人:子 文档编号:41861423 上传时间:2018-05-31 格式:DOC 页数:13 大小:41.50KB
返回 下载 相关 举报
数据结构c++顺序表_第1页
第1页 / 共13页
数据结构c++顺序表_第2页
第2页 / 共13页
数据结构c++顺序表_第3页
第3页 / 共13页
数据结构c++顺序表_第4页
第4页 / 共13页
数据结构c++顺序表_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《数据结构c++顺序表》由会员分享,可在线阅读,更多相关《数据结构c++顺序表(13页珍藏版)》请在金锄头文库上搜索。

1、数据结构数据结构 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;/线性表的

2、顺序存储结构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(*

3、 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

4、+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)co

5、ute;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;elseco

6、ut“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;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号