顺序表的应用.doc

上传人:钟*** 文档编号:5423201 上传时间:2020-01-29 格式:DOC 页数:10 大小:85.16KB
返回 下载 相关 举报
顺序表的应用.doc_第1页
第1页 / 共10页
顺序表的应用.doc_第2页
第2页 / 共10页
顺序表的应用.doc_第3页
第3页 / 共10页
点击查看更多>>
资源描述
.实验一 顺序表的应用一、实验目的1. 掌握线性表的顺序存储结构的存储特点与操作特点。2. 掌握顺序表的存储表示与基本操作的实现方法。3. 熟悉顺序表的基本应用。4. 了解抽象数据类型的定义、表示与实现的含义。二、实验内容按如下要求编写程序,进行调试,写出调试正确的源代码,给出测试结果。实现线性表在顺序存储结构下的存储表示和基本操作,并应用抽象数据类型线性表完成如下操作:1分别创建两个空的线性表L1、L2;2分别将若干元素插入到L1、L2中,输出操作后表L1和L2的内容;3. 实现两表的合并操作:L1=L1L2,输出合并后表L1的内容。4. 实现两表的求差操作:L1=L1-L2,输出求差后表L1的内容。三、实验结果源代码:#include#include#define OK 1#define TRUE 1#define FALSE 0#define ERROR 0#define OVERFLOW -2#define LIST_INIT_SIZE 100#define LISTINCREMENT 10typedef int Status;typedef int ElemType;typedef struct/存储表示ElemType *elem;int length;int listsize;SqList;/基本操作的函数声明Status InitList_Sq(SqList & L);Status ListInsert_Sq(SqList &L,int i,ElemType e);Status equal(ElemType a,ElemType b);Status GetElem_Sq(SqList L,int i,ElemType &e);int ListLength_Sq(SqList L);void Unoin(SqList& La,SqList Lb);void cha(SqList &La,SqList &Lb);int LocateElem_Sq(SqList L,ElemType e,Status(*compare)(ElemType,ElemType);/基本操作的算法实现Status InitList_Sq(SqList & L)L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType);if(!L.elem) exit(OVERFLOW);L.length=0;L.listsize=LIST_INIT_SIZE;return OK;Status ListInsert_Sq(SqList &L,int i,ElemType e)ElemType *newbase,*p,*q;if(iL.length+1)return ERROR;if(L.length=L.listsize)newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType);if(!newbase) exit(OVERFLOW);L.elem=newbase;L.listsize+=LISTINCREMENT;q=&(L.elemi-1);for(p=&(L.elemL.length-1);p=q;-p)*(p+1)=*p;*q=e;+L.length;return OK;Status ListDelete_Sq(SqList &L,int i,ElemType &e)ElemType *p,* q;if(iL.length)return ERROR;p=&(L.elemi-1);e=*p;q=L.elem+L.length-1;for(+p;p=q;+p)*(p-1)=*p;-L.length;return OK;void cha(SqList &La,SqList &Lb)int la_len,lb_len,i,e;la_len=ListLength_Sq(La);lb_len=ListLength_Sq(Lb);for(i=1;i=lb_len;i+)GetElem_Sq(Lb,i,e);if(LocateElem_Sq(La,e,equal)ListDelete_Sq(La,LocateElem_Sq(La,e,equal),e);Status equal(ElemType a,ElemType b)if(a=b)return TRUE;else return FALSE;Status GetElem_Sq(SqList L,int i,ElemType &e)if(iL.length)return ERROR;e=*(L.elem+i-1);return OK; int ListLength_Sq(SqList L)return L.length;void Unoin(SqList& La,SqList Lb)int la_len,lb_len,i,e;la_len=ListLength_Sq(La);lb_len=ListLength_Sq(Lb);for(i=1;i=lb_len;i+)GetElem_Sq(Lb,i,e);if(!LocateElem_Sq(La,e,equal)ListInsert_Sq(La,+la_len,e);int LocateElem_Sq(SqList L,ElemType e,Status(*compare)(ElemType,ElemType)int i=1;ElemType *p;p=L.elem;while(i=L.length&!(*compare)(*p+,e) +i;if(i=L.length) return i;else return 0;void main()int m,n,i;ElemType e;SqList L1,L2;InitList_Sq(L1);InitList_Sq(L2);coutm;coutn;cout请输入m个元素到L1表中endl;for(i=1;ie;ListInsert_Sq(L1,i,e);cout请输入n个元素到L2表中endl;for(i=1;ie;ListInsert_Sq(L2,i,e);coutL1表的内容endl;for(i=1;i=ListLength_Sq(L1);i+)GetElem_Sq(L1,i,e);coutet;coutendl;coutL2表的内容endl;for(i=1;i=ListLength_Sq(L2);i+)GetElem_Sq(L2,i,e);coutet;coutendl;cout输出L1和L2的合并后;Unoin(L1,L2);coutL1表的内容endl;for(i=1;i=ListLength_Sq(L1);i+)GetElem_Sq(L1,i,e);coutet;coutendl;cout输出L1和L2的差后;cha(L1,L2);coutL1表的内容endl;for(i=1;i=ListLength_Sq(L1);i+)GetElem_Sq(L1,i,e);coutet;coutendl;运行结果:1运行结果:2精选word范本!
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!