试验一:线性表的设计与实现.docx_第1页
试验一:线性表的设计与实现.docx_第2页
试验一:线性表的设计与实现.docx_第3页
试验一:线性表的设计与实现.docx_第4页
全文预览已结束

下载本文档

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

文档简介

计算机软件基础实验报告 08020327 钟鑫通过本次实验,加深了对c语言的理解。提高了自身阅读样本程序、调试程序的能力。一、/* 线性表的程序设计shiyan11.c */#include /*编译预处理命令*/#include #define Null 0#define MaxSize 1024 /*定义数据域最大长度*/typedef int DataType;typedef struct node /*定义线性链表*/ DataType dataMaxSize; int last; SequenList;void PrintOut(SequenList *L);int Delete(SequenList *L,int i);void CreateList(SequenList *L);int Insert(SequenList *L,DataType x,int i);/*在主函数外先声明此函数,否则后面的函数无法正常使用*/int main() /*主函数*/ SequenList MyList,*L; char cmd; int i,t,x; L=&MyList; L-last=-1;do do clrscr(); /*在c+的环境下不需要此函数所以直接删除即可*/ printf(ntc,C.Create Listn); /*首先建立主菜单*/ printf(nti,I .Insert); printf(ntd,D.Delete); printf(ntq,Q.QuitntYour choice:); cmd=getchar(); while(cmd!=d)&(cmd!=D)&(cmd!=q)&(cmd!=Q)&(cmd!=i)&(cmd!=I)&(cmd !=c)&(cmd !=C);/* or: while(toupper(cmd)!=D)&(toupper(cmd)!=Q)& (toupper(cmd)!=I)&(toupper(cmd)!=C);*/ switch(cmd) /*建立分支结构,实现引导功能*/ case c: case C:CreateList(L); break; case i: case I:printf(nInput the data to be inserted:); /* 插入元素到指定 scanf(%d,&x); 位置 */ printf(nInput the poistion to be inserted:); printf(n(1-%d)n,(L-last+2); scanf(%d,&i); Insert(L,x,i); /*函数应先声明再调用*/ PrintOut(L); getch(); break; case d: case D:printf(nInput the index_No of data to be deletedn); printf(n(1-%d):n,(L-last+1); /* 删出指定位置的元素 */ scanf(%d,&i); Delete(L,i); PrintOut(L); getch(); break; default: break; while(cmd!=q) & (cmd!=Q); /* or: while(toupper(cmd)!=Q);*/return Null; /*定义的主函数需要一个返回值,表示结束main()的运行,作为程序的结束状态。因之前以将Null赋值为0,因此可直接返回Null.*/*以下为定义插入函数,先将要插入位置后的每个数据往后挪一位,再将要插入的数据放入链表中。*/int Insert(SequenList *L,DataType x,int i) /*前面没有声明就调用,因此只 int j; 需在前面加上定义即可*/ p=L;/* 删去此行,将本函数中所有的p均改为L.否则,还需再定义p */ if(L-last=(MaxSize-1) /*p均改为L*/ printf(nOverflow!); return Null; else if(i(L-last+2) printf(range error); return Null; else for(j=L-last;j=i-1;j-)L-dataj+1=L-dataj; L-datai-1=x; L-last=L-last+1; return (1);/*定义程序在执行后,在屏幕上的显示格式的函数。*/void PrintOut(SequenList *L) int i; for(i=0;ilast;i+) printf(data%d=,i+1); printf(%dt,L-datai); if(i!=0)&(i%4)=0) printf(n); /* PrintOut */*以下为定义删除函数,先将第i个元素之后的所有元素向前移一个位置,然后再删除第i个元素。*/int Delete(SequenList *L,int i) int j; if(iL-last+1) printf(range error); return Null; else for(j=i;jlast+1;j+)L-dataj-1=L-dataj; L-last-; return (1);/*以下为定义建表函数,自定义表的长度和每个数据的值。*/void CreateList(SequenList *L) int n,i; int mydata; printf(nPlease input total number of data itemn); scanf(%d,&n); /*定义表长*/ for(i=0;idatai=mydat

温馨提示

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

评论

0/150

提交评论