链表的综合操作实验报告
链表的综合操作
一、实验目的
1、理解数据结构中链表的定义,熟练掌握单链表的建立、插入与删除等操作的程序。
2、掌握单链表中结点的结构。 3、熟练掌握简单的菜单设计方法。
4、加深对C语言的使用,特别是函数的参数调用、指针类型的应用和链表的建立等各种基本操作。
通过本实验理论和实践的相结合,深化理解和掌握书本上的理论知识,培养数据结构的应用能力与实践能力。
二、实验用仪器设备、器材或软件环境
win-TC、Microsoft Visual C++ 2008 Express Edition
三、实验原理、方案设计、程序框图、预编程序等
实验原理:根据链表结构体的定义,运用指针变量处理,开辟结点,建立前后相连关系,创建出链表,再对链表进行输出、插入、删除等的综合操作。 实验设计:本程序包含6个函数: ①创建新链表函数creat ( ) ②输出链表内容函数print ( ) ③插入元素函数insert ( ) ④删除元素函数del ( ) ⑤菜单函数menu ( ) ⑥主函数main ( )
程序流程图:
creat()函数的流程图
print()函数的流程图
del()函数流程图
insert()函数流程图
menu()函数流程图
主要程序:
#include #include "stdio.h" #include "malloc.h" #include #define NULL 0
#define LEN sizeof(struct student) struct student {long num;
char phone[15]; char addr[15]; long zip;
struct student *next; }; int n;
struct student *creat() /*开始创建creat()函数*/ {struct student *head; struct student *p1,*p2; n=0;
p1=p2=(struct student *)malloc(LEN); printf("num:");scanf("%ld",&p1->num); if(p1->num!=0)
{printf("phone:");scanf("%s",p1->phone); printf("addr:");scanf("%s",p1->addr); printf("zip:");scanf("%ld",&p1->zip);
printf("ok!please input next\n"); }
head=NULL;
while(p1->num!=0) {n=n+1;
if(n==1) head=p1; else p2->next=p1; p2=p1;
p1=(struct student*)malloc(LEN);
printf("num:");scanf("%ld",&p1->num); if(p1->num!=0)
{printf("phone:");scanf("%s",p1->phone); printf("addr:");scanf("%s",p1->addr); printf("zip:");scanf("%ld",&p1->zip); } }
p2->next=NULL; return(head);
} /*结束函数creat()的创建*/
void print(struct student *head) /*开始创建print()函数*/ {struct student *p; p=head;
if(head!=NULL)
{printf("\nNow,These%d records are:\n",n);
printf("\n-----------|------------------|----------------|---------------\n");
printf("number | telephone | address | zip code\n"); printf("-----------|------------------|----------------|---------------\n");
do
{printf("0%-10ld| %-14s| %-9s
| %-13ld\n",p->num,p->phone,p->addr,p->zip); printf("-----------|------------------|----------------|---------------\n");
p=p->next;
}while(p!=NULL); }
else printf("link list null!\n");
} /*结束函数print()的创建*/
struct student *del(struct student*head,long num) /*开始创建del()函数*/ {struct student*p1,*p2;
if(head==NULL) {printf("\nlist null!\n");return head;} p1=head;
while(num!=p1->num&&p1->next!=NULL)
{p2=p1;p1=p1->next;} if(num==p1->num)
{if((p1==head)) head=p1->next; else p2->next=p1->next; printf("delete:%ld\n",num); n=n-1; }
else printf("0%ldis not been found !\n",num); return head;
} /*结束函数del()的创建*/
struct student *insert(struct student *head,struct student *stud) /*开始函数insert()的创建*/ {struct student *p0,*p1,*p2; p1=head; p0=stud;
if(head==NULL)
{head=p0;p0->next=NULL;} else
{while ((p0->num>p1->num)&&(p1->next!=NULL)) {p2=p1; p1=p1->next; } if(p0->numnum)
{if(head==p1) head=p0; else p2->next=p0; p0->next=p1;} else
{p1->next=p0;p0->next=NULL;} } n=n+1; return head;
} /*结束函数insert()的创建*/
int menu() /*开始创建菜单函数menu()*/ {int c,flag=1;
printf("\n************************* menu **************************\n"); printf("1.creat link list\n"); printf("2.output link list\n"); printf("3.input records\n"); printf("4.delete records\n"); printf("5.exit\n");
printf("\n************************* menu **************************\n"); printf("\please input your choose:"); scanf("%d",&c); while(flag)
{ if(n==0&&c!=1&&c!=5)
{printf("please choose 1 before you creat link list\n"); printf("\nPlease enter the number 1 to 5:"); scanf("%d",&c); flag=1; }
else flag=0 ; }
while( c5)
{ printf("\nPlease enter the number 1 to 5:"); scanf("%d",&c); } return c;
} /*结束菜单函数的创建*/
void main() /*开始建立主函数*/ { struct student *head,*newstu; long del_num; for(;;)
{ switch (menu())
{ case 1:printf("input records:\n"); head=creat(); break;
case 2:print(head); break;
case 3: printf("\ninput the inserted record:\n"); newstu=(struct student *)malloc(LEN);
printf("num:");scanf("%ld",&newstu->num); if(newstu->num!=0)
{ printf("phone:");scanf("%s",newstu->phone); printf("add:");scanf("%s",newstu->addr); printf("zip:");scanf("%ld",&newstu->zip); printf("ok!please input next\n");
}
while(newstu->num!=0)
{ head=insert(head,newstu); print(head);
printf("input the inserted record:\n"); newstu=(struct student *)malloc(LEN);
printf("num:");scanf("%ld",&newstu->num); if(newstu->num!=0)
{ printf("phone:");scanf("%s",newstu->phone); printf("add:");scanf("%s",newstu->addr); printf("zip:");scanf("%ld",&newstu->zip); printf("ok!please input next\n");
} } break;
case 4:printf("\ninput the deleted number:"); scanf("%ld",&del_num); while(del_num!=0)
{ head=del(head,del_num); print(head);
printf("\ninput the deleted number:"); scanf("%ld",&del_num); } break;
case 5: system("CLS");
printf("Thank you for using the link list\n\n\n\n"); getch(); exit(0); } } }
程序代码说明:num----学号;phone----电话号码;addr----家庭住址;zip----邮编 newstu----新插入的学生;del_num----删除的学生学号 菜单声明:
1、creat link list----创建新链表 2、output link list----输出链表 3、input records----插入新成员 4、delete records----删除成员、 5、exit----退出链表
在菜单中选择需进行的操作,程序将依照菜单选项实行操作。
如图Ⅰ所示,先选择菜单中的1,创建链表。分别输入10名学生的学号、电话、地址、邮政编码。输完最后一个学生后,在下一个结点的学号处输0,程序就会自动结束创建链表,回到菜单选择。
图Ⅱ
图Ⅰ
选择菜单中的2,输出学生详细情况,如图Ⅱ所示,程序列出所输入的10名学生的学号、电话、地址、邮政编码。
选择菜单中的选项3,
进行新结点的插入。
如图Ⅲ所示,插入学号
为08071205学生的详
细信息,链表将打出11
个成员的信息状况,且
按学号由小到大的顺序
自动排列好。
图Ⅲ
选择菜单中的选项4,
进行链表中结点的删除
操作。
选择删除学号为
08071203的学生信息,
链表将删除该生信息,
显示剩余10名学生的
信息。如图Ⅳ所示。
图Ⅳ
若所选择删除的学生学号不存在于先前创建的链表中,则程序将无法找到
删除结点,例如我从上面链表中选
择删除学号为08071213的学生信
息,程序无法找到,如图所示,将
会显示08071213is not been found!
且原本的链表将保持不变,还是输
出原来10名同学的信息。
图Ⅴ
完成以上4项操作后,选择菜单选项5、退出程序。
图Ⅵ
程序将会清屏,最后显示Thank you fou using the link list,结束程序。
图Ⅶ
四、实验步骤、程序调试方法
根据课本内容(1)建立链表,一个一个地输入各结点数据,并建立起前后相互链接的关系。(2)输出链表,将链表中各结点的数据依次打出显示。设一个指针变量p,先指向第1个结点,显示p所指的结点,然后p后一个结点再显示之,直到链表尾结点。(3)删除链表中的结点是从p指向第1个结点开始,检查该结点的数据是否等于要删除的数据,如果相等就将该结点删除,如不相等,则将p后移一个结点,如此进行下去,直到表尾为止。(4)插入结点,将一个结点插入到已知的链表中某结点之后。(5)插入主函数和菜单函数。按照步骤编好运用程序后,调试进行数据处理。
数据处理:先逐个输入我们所需的学生信息结构体:学号、电话、家庭住址,邮政编码,然后输入结点信息,打出所创建链表的结点内容。再将一个新学生信息插入到已有链表中,程序会自动排列插入的结点内容然后输出。然后又从创建的动态链中删除一个结点信息。最后选择退出结束程序。
五、实验中存在的问题及解决方案
1、电话号码有11位,而如果用长整型定义phone的话,将超出长整型的范围,而用实型float,在win-TC中无法运行。所以最后将电话phone定义为数组char[ ]。
2、将学号定义为长整型long,而学号开头第一位是0,若输入学号08071201,输出时0将会被自动删除,最后输出8071201。为了解决这问题,我在学号输出是在前多输一个0,即为“0%ld”,这样就可以正确输出学生学号。
3、由于win-TC程序运行界面不方便截图,所以我改用Microsoft Visual C++ 2008 Express Edition来运行程序。
六、心得体会
通过本次对链表建立的实验,加深了我对C语言的使用,特别是函数的参数调用、指针类型的应用和链表的建立等各种基本操作,初步了解了数据结构,让我掌握单链表的建立、插入与删除。本实验帮助我深化理解和掌握书本上十一章结构体的理论知识,增强了我对c语言的兴趣。通过本次理论和实践相结合,使我学会如何把书本上有关数据结构和算法的知识用于解决实际问题,培养了我对链表的应用能力和上机的实践能力。通过本次实验也让我认识到了自己的不足,粗心,知识掌握我不扎实,以后会努力改进,从而更好地掌握C语言。