- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
算法设计基础实验
班级: 学号: 姓名: 实验一 线性表的应用
实验内容:
1.给定一线性表L (15,25,05,36,78,85,23),写出顺序存储结构下的插入、删除、排序操作的算法及程序。
2.写出链接存储结构下的插入、删除、排序操作的算法及程序。
实验要求:
1.掌握顺序及链接存储下的插入、删除算法;
2.掌握直接插入排序算法;
实验程序
#include
#include
struct List int *list;
int size;
int MaxSize;
;
void InitList List L L.MaxSize 10;
L.list new int[L.MaxSize];
if L.list NULL cout 动态可分配的存储空间用完,退出运行! endl; exit 1 ; L.size 0; bool InsertList List L,int item,int pos if pos -1||pos L.size+1 cout pos值无效! endl; return false; int i;
if pos 0 for i 0;i L.size;i++ if item L.list[i] break; pos i+1; else if pos -1 pos L.size+1;
if L.size L.MaxSize int k sizeof int ;
L.list int* realloc L.list,2*L.MaxSize*k ;
if L.list NULL cout 动态可分配的空间用完,退出运行! endl; exit 1 ; L.MaxSize 2*L.MaxSize; for i L.size-1;i pos-1;i-- L.list[i+1] L.list[i];
L.list[pos-1] item;
L.size++;
return true; bool DeleteList List L,int item,int pos if L.size 0 cout 线性表为空,删除无效! endl;
return false; if pos -1||pos L.size cout pos值无效! endl;
return false; int i;
if pos 0 for i 0;i L.size;i++ if item L.list[i] break;
if i L.size return false;
pos i+1; else if pos -1 pos L.size;
item L.list[pos-1];
for i pos;i L.size;i++ L.list[i-1] L.list[i];
L.size--;
if float L.size /L.MaxSize 0.4L.MaxSize 10 int k sizeof int ;
L.list int* realloc L.list,L.MaxSize*k/2 ;
L.MaxSize L.MaxSize/2; return true; void SortList List L int i,j;
int x;
for i 1;i L.size;i++ x L.list[i];
for j i-1;j 0;j-- if x L.list[j] L.list[j+1] L.list[j];
else break;
L.list[j+1] x; void TraverseList List L for int i 0;i L.size;i++ cout L.list[i] ;
cout endl; void main int a[7] 15,25,5,36,78,85,23 ;
int i;
int x;
List t;
InitList t ;
for i 0;i 7;i++ InsertList t,a[i],i+1 ; SortList t ;
TraverseList t ;
cout 输入待删除元素的值:;
cin x;
if DeleteList t,x,0 cout 删除成功! endl;
else cout 删除失败! endl;
TraverseList t ;
cout 按值插入,输入待插入元素的值: endl;
cin x;
if InsertList t,x,0 cout 插入成功! endl;
else cout 插入失败! endl;
TraverseList t
文档评论(0)