实验十 链表操作

上传人:壹****1 文档编号:506999378 上传时间:2022-10-21 格式:DOC 页数:4 大小:16.50KB
返回 下载 相关 举报
实验十 链表操作_第1页
第1页 / 共4页
实验十 链表操作_第2页
第2页 / 共4页
实验十 链表操作_第3页
第3页 / 共4页
实验十 链表操作_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《实验十 链表操作》由会员分享,可在线阅读,更多相关《实验十 链表操作(4页珍藏版)》请在金锄头文库上搜索。

1、实验十 链表操作实验目的 掌握链表的定义和使用方法,能熟练进行链表的各种操作。实验内容 定义一学生链表,完成建立链表、打印链表、插入链表和删除链表操作。实验步骤1分别定义四个函数create( ),print( ), insert( ),del( )。2在主函数中调用上述函数,测试各函数的功能。源程序:#include#includestruct studentint no;struct student *next;/建立链表struct student *create()struct student *head,*p1,*p2;head=NULL;p1=p2=(struct student

2、*)malloc(sizeof (struct student);scanf(%d,&p1-no);while(p1-no!=0)if (head=NULL) head=p1;else p2-next=p1;p2=p1;p1=(struct student *)malloc(sizeof (struct student);scanf(%d,&p1-no);p2-next=NULL;return head;/打印链表void print(struct student *head)struct student *p;p=head;while(p!=NULL) printf(%5d,p-no); p

3、=p-next; printf(n);/删除链表struct student *del(struct student *head,int num) struct student *p1,*p2;if (head=NULL) printf(NULLn);elsep1=head;while(p1-no!=num&p1-next!=NULL)p2=p1;p1=p1-next;if (p1-no=num) if (p1=head) head=head-next; else p2-next=p1-next; free(p1); printf(delete success!n); else printf(

4、not foundn);/这一块是对应原来的循环条件return head;/用递归反向输出链表void output(struct student *head)if (head-next!=NULL) output(head-next); printf(%5d, head-no); /用递归recursion正向输出链表void output1(struct student *head)if (head!=NULL) printf(%5d, head-no); output1(head-next); main()struct student *head,*head1;int num;head=create();output1(head);/print(head);printf(n);printf(n);scanf(%d,&num);head=del(head,num);print(head);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 商业计划书

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号