计算机本科数据结构与算法实验指导书资料_第1页
计算机本科数据结构与算法实验指导书资料_第2页
计算机本科数据结构与算法实验指导书资料_第3页
计算机本科数据结构与算法实验指导书资料_第4页
计算机本科数据结构与算法实验指导书资料_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、资料内容仅供您学习参考,如有不当或者侵权,请联系改正或者删除。实验一线性表的实验一、实验目的1、 掌握用 visual c+6.0上机调试顺序表的基本方法;2、 掌握顺序表的基本操作,插入、删除、查找、以及有序顺序表的合并等算法的实现;3、 掌握用 visual c+6.0上机调试单链表的基本方法;4、 掌握单链表的插入、删除、查找、求表长以及有序单链表的合并算法的实现;5、 进一步掌握循环单链表和双链表的插入、删除、 查找算法的实现。二、实验内容下面是顺序表的部分基本操作实现的算法,请同学们自己设计主函数和部分算法,调用这些算法 ,完成下面的实验任务。/* 常见的符号常量定义*/# defi

2、ne ok 1# define error 0# define maxsize 10 #define list_init_size 10 #define listincrement 10 #define true 1#define false 0资料内容仅供您学习参考,如有不当或者侵权,请联系改正或者删除。#define ok 1#define error 0#define success 1#define unsuccess 0#define duplicate -1#define nullkey 0 / 0为无记录标志#define n 10 /数据元素个数#define eq(a,b)

3、(a)=(b)#define lt(a,b) (a)(b)#define lq(a,b) (a)=(b)/*定义 elemtype为 int 或别的自定义类型*/typedef int elemtype;/*顺序存储类型*/typedef struct int *elem;int length;int listsize;sqlist;/* 构造一个空线性表算法*/资料内容仅供您学习参考,如有不当或者侵权,请联系改正或者删除。intinitlist_sq(sqlist&l)/initlist_sq()function/inititial a sq_listl.elem=( elemtype *)

4、malloc(list_init_size*sizeof(elemtype);if (!l.elem) return(0);l.length=0;l.listsize=list_init_size;return(1);/end of initlist_sq() function/*从顺序表中查找与给定元素值相同的元素在顺序表中的位置 */int locateelem_sq(sqlist l, elemtype e)/locateelem_sq() sub-function int i=1;int *p=l.elem;while(i=l.length&*p+!=e)+i;if(i=l.lengt

5、h)return (i);资料内容仅供您学习参考,如有不当或者侵权,请联系改正或者删除。elsereturn (error); /locateelem_sq() end/*向顺序表中插入元素*/int listinsert_sq(sqlist &l,int i,inte)/listinsert_sq()if(il.length+1)illegal/i (location) is printf(initial failure!n);return (error);if(l.length=l.listsize)end of the sqlist elemtype *newbase;newbase=(

6、 elemtype/insert into the*)realloc(l.elem,(l.listsize+listincrement)*sizeof(elemtype);if(!newbase) printf(overflow!n); return (error);资料内容仅供您学习参考,如有不当或者侵权,请联系改正或者删除。l.elem=newbase;l.listsize+=listincrement;int *p,*q;q=&(l.elemi-1);/ /q point at the element beforethe inserted onefor(p=&(l.eleml.lengt

7、h-1);p=q;-p)/movethe element*(p+1)=*p;*q=e;+l.length;return (ok); /listinser_sq() end/*从顺序表中删除元素*/void listdelete_sq(sqlist &l,int i, int &e)/listdelete_sq() functionint *p,*q;if(il.length)printf(” %d is overflow !n, i);资料内容仅供您学习参考,如有不当或者侵权,请联系改正或者删除。exit(0);p=&(l.elemi-1);e=*p;q=l.elem+l.length-1;f

8、or (+p;p=q;+p)*(p-1)=*p;-l.length;printf(success to delete sq_list !n);/end of listdelete_sq() function/*有序顺序表的合并*/int merge_sq(sqlist &a,sqlist &b,sqlist &c)/merge_sq() function /merge the sqlist a and b to c c.listsize=c.length=a.length+b.length; c.elem=(elemtype*)malloc(c.listsize*sizeof(elemtype);if(!c.elem) printf( overflow !n); /failure to allocate room in ram资料内容仅供您学习参考,如有不当或者侵权,请联系改正或者删除。return(0);inti=0,j=0;/iof a.elem and b.elem int k=0;/k isthe subscript of c.elemwhile(ia.length)&(jb.lengthand j is the subscript)/tomergewhen i =b.lengthif(a.elem i=b.elem j) c.el

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论