1、实验容或题目 (1) 用结构体描述一个字符型的单向链表。 (2) 创建线性表;在线性表中插入元素、删除元素;显示线性表中所有元素的基本操作。 (3) 用if语句设计一个选择式菜单。 线 性 表 子 系 统 **************************************************** * 1--------建 表 * * 2--------插 入 * * 3--------删 除 * * 4--------显 示 * * 5--------查 找 * * 6--------求 表 长 * * 0--------返 回 * **************************************************** 2、实验目的与要求 (1) 掌握线性表的特点; (2) 掌握线性表顺序存储结构和链式存储结构的基本运算; (3) 掌握线性表的创建,插入,删除和显示线性表中元素的基本操作。 3、实验步骤与源程序 ⑴ 实验步骤 首先构建出各个函数在主调函数main()函数中的作用以及返回值:CreatList()函数是用来建立线性表,InsList()函数用来插入结点元素,DelList()函数用来删除结点元素,ShowList()函数用来显示线性表,SearchList()函数用来查找线性表元素,主函数main()用于菜单界面,方便操作。之后便是具体实现每个函数功能的细节。程序编好后经过不断地差错改错,最后得到正确的源程序。 ⑵ 程序源代码 #include #include typedef struct linknode { char data; struct linknode *next; }linnode; linnode *head; int n; void CreateList() { } void InsList(int i,char x) { linnode *s,*p; linnode *p,*s; int z; char x; n=0; z=1; head=(linnode *)malloc(sizeof(linnode)); p=head; printf(\"\\n\\请逐个输入节点,以‘x’为结束标记!\\n\"); printf(\"\\n\"); while(z) { } printf(\"\\输入一个字符数据,并按回车:\"); scanf(\"%c\getchar(); if(x!='x') { } else z=0; s=(linnode *)malloc(sizeof(linnode)); n++; s->data=x; p->next=s; s->next=NULL; p=s; } int j; p=head; j=10; while(p!=NULL&&jdata=x; s->next=p->next; p->next=s; n++; j++; p=p->next; void DelList(char x) { linnode *p,*q; if(head==NULL) { } if(head->next==NULL) { } printf(\"\\n\\线性表已为空!\"); return; printf(\"\\n\\链表下溢!\"); return; } q=head; p=head->next; while(p!=NULL&&p->data!=x) { } if(p!=NULL) { } else printf(\"\\n\\抱歉!没有找到您要删除的结点。\"); q->next=p->next; free(p); n--; printf(\"\\n\\结点%c已经被删除!\q=p; p=p->next; void ShowList() { linnode *p=head; printf(\"\\n\\显示线性表的所有元素:\"); if(head->next==NULL||p==NULL) printf(\"\\n\\链表为空!\"); else { } printf(\"\\n\\\"); while(p->next!=NULL) { } printf(\"%5c\p=p->next; } void SearchList(char x) { } void main() { int choice,i,j; char x; head=NULL; linnode *p; int i=1; if(head==NULL) { } if(head->next==NULL) { } p=head->next; while(p!=NULL&&p->data!=x) { } if(p!=NULL) printf(\"\\n\\在表的第%d位上找到值为%c的结点!\p=p->next; i++; printf(\"\\n\\线性表为空,没有任何节点!\"); return; printf(\"\\n\\链表下溢!\"); return; else printf(\"\\n\\抱歉!未找到值为%c的结点!\ j=1; while(j) { printf(\"\\n\"); printf(\"\\n\\ 线性表子系统 \"); printf(\"\\n\\*********************************\"); printf(\"\\n\\* 1------建 表 *\"); printf(\"\\n\\* 2------插 入 *\"); printf(\"\\n\\* 3------删 除 *\"); printf(\"\\n\\* 4------显 示 *\"); printf(\"\\n\\* 5------查 找 *\"); printf(\"\\n\\* 6------求 表 长 *\"); printf(\"\\n\\* 0------返 回 *\"); printf(\"\\n\\*********************************\"); printf(\"\\n\\ 请选择菜单号(0--6):\"); scanf(\"%d\ getchar(); if(choice==1) CreateList(); else if(choice==2) { printf(\"\\n\\请输入插入的位置i和插入的数据(输入格式:i,x):\"); scanf(\"%d,%c\ InsList(i,x); } else if(choice==3) { printf(\"\\n\\请输入要删除的数值:\"); scanf(\"%c\ DelList(x); } else if(choice==4) if(head==NULL) printf(\"\\n\\请先建立线性表!\"); else ShowList(); else if(choice==5) { printf(\"\\n\\请输入要查找的元素:\"); scanf(\"%c\ SearchList(x); } else if(choice==6) printf(\"\\n\\线性表长度为:%d\ else if(choice==0) j=0; else printf(\"\\n\\输入错误!请重新输入!\"); } } 4、测试数据与实验结果 图一 建立一个线性表 图二 线性表数据的插入 图三 线性表数据的删除 图四 线性表数据的查找 图五 求表长 图六 返回 5、结果分析与实验体会 在学过本节有关线性表的两种存储结构的基础操作后,做这个验证性的实验的焦点便在于验证各种操作的正确与否,在对照书上的线性表基本运算的实现的算法思路和注意事项,结合老师上课强调的每种算法的注意重点,找出了原始程序的几个主要的错误点: 1、某些地方运用了C++语言而不是C,如:每个函数在为申请一块LinkNode类型的存储单元的操作,并将其地址复制给变量时:p=new LinkNode;而在C环境中应为:p=( LinkList *)malloc(sizeof(LinkNode));。再如:在释放p所指的结点时运用了delete(p);,应该为free(p);。 2、某些地方将赋值语句写在定义该变量的上面,这就直接导致了程序编译时的错误,如:n=0;linnode *p,*s;char x;int z=1;应改为: linnode *p,*s;char x;int z=1,n=0;。