实验指导书(2007级).docVIP

  • 3
  • 0
  • 约8.27千字
  • 约 12页
  • 2017-08-20 发布于北京
  • 举报
实验一:线性表的基本操作 实验目的:掌握线性表的基本操作:插入、删除、查找以及线性表合并等运算在顺序存储结构的运算。能从实际问题中抽象出线性表结构及其运算的模型 实验课时:3课时 实验要求: 掌握线性表的顺序存储的基本运算的算法和程序 掌握线性表链式存储的基本运算的算法 根据算法写出对应程序(使用语言没有硬性规定,但推荐使用C) 调试成功,并保存程序源代码,以备检查 实验内容: 1. 设计——实现用户通过键盘输入一组数,并输入一个需要插入的数,和插入的位置,完成整个插入。例,输入1,2,3,4再输入5,3。最后程序运行结果为1,2,5,3,4。 先写出完成该操作的算法或执行流程 以程序方式实现算法 数的长短自行先定义,可以用数组存储。 注意插入位置的判断 2.验证——上机实现链表的基本操作程序,并结合对应的算法,了解链表的工作情况。 #includestdio.h #includestdlib.h #includeconio.h struct mylist {int data; struct mylist *next; }; struct mylist *createlist(void) {int x; struct mylist *s,*q,*h; h=(struct mylist *)malloc(sizeof(struct mylist)); h-next=NULL; q=h; printf(input number:); scanf(%d,x); while(x!=0) { s=(struct mylist*)malloc(sizeof(struct mylist)); s-data=x; q-next=s; printf(input the number:); scanf(%d,x); s-next=NULL; q=s; } return(h); } int putlist(struct mylist *head) {int i=0; if(head-next==NULL) {return 0;} while (head-next !=NULL) {i++; printf(the %d number in list is:%d\n,i,head-next-data); head=head-next; } return 1; } void main() {struct mylist *my,*a; my=createlist(); if(putlist(my)==0) printf(the input list is empty!); while(my-next !=NULL) {a=my; my=my-next; free(a); } printf(over,press any key to end); getch(); } 3.将第1题所描述问题用链表方式实现,并以实验报告形式提交(报告2) 4.验证——上机实现一元多项式的加法程序,并结合算法理解链表的工作过程和基本操作的实现。 #includestdio.h #includemalloc.h #includestdlib.h #define LEN sizeof(node) typedef struct polynode /*用单链表存储多项式的结点结构*/ { int coef; /*多项式的系数*/ int exp; /*指数*/ struct polynode *next; /*它又指向struct polynode类型的数据,以此建立链表*/ }node; /*意即node*与list同为结构指针类型*/ node * create(void) /*用尾插法建立一元多项式的链表的函数*/ { node *h,*r,*s; int c,e; h=(node *)malloc(LEN); /*建立多项式的头结点,为头结点分配存储空间*/ r=h; /*r指针始终动态指向链表的当前表尾,以便于做尾插入,其初值指向头结点*/ printf(coef:); scanf(%d,c); /*输入系数*/ printf(exp: ); scanf(%d,e); /*输入指数*/ while(c!=0) /*输入系数为0时,表示多项式的输入结束*/ { s=(node *)malloc(LEN); /*申请新结点*/ s-coef=c; /*申请新结点后赋值*/ s-exp=e; /*申请新结点后赋值*/ r-next=s; /*做尾插,插入新结点*/ r=s; /*r始终指向单链表的表尾*/ printf(coef:); scanf(%d,c); printf(exp: ); scanf(%

文档评论(0)

1亿VIP精品文档

相关文档