- 1、本文档共5页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
动态链表的建立删除插入完整程序
这个程序主要对动态链表的建立、删除、插入的完整程序,注释很详尽对刚学习c语言的人来说应该有所帮助吧,程序是我在.cpp格式下运行好的。
/******************************************
******
*
* 文件名 :C practice
* 创建时间 : 2013-1-18
* 文件描 : c的基础练习 链表的
* 建立及使用
* 错误记录及修改:
* 在动态链表输出时出错
* p-num!=0 改为p-!=NULL;原因在于最后输入
* 0 0时没有将最后的链表建立进去,当访问
* p-num时没有值,最后输错出错
*******************************************
******/
#include stdio.h
#include string.h
#include stdlib.h
#define Len sizeof(struct student) //定义一个结构体等长的常量len,用于给新的结构体分配空间
/*********************************
结构体名:student
**********************************/
struct student
{
int num;
int score;
struct student *next;
};
struct student *head,*p1,*p2;
int n=0; //用于记录插入链表的个数
/*********************************
函数名 :*creat()
参数 :无
功能 :用于插入新的链表
返回值 :返回链表的首地址 head
*********************************/
struct student *creat()
{
head=NULL;
p1=(struct student*)malloc(Len); //给新的节点分配空间
p2=p1;
puts(请输入学生号和成绩);
scanf(%d %d,(p1-num),(p1-score));
while(p1-num!=0)
{
n++;
if(n==1) head=p1;
else p2-next=p1;
p2=p1;
p1=(struct student*)malloc(Len);
printf(建立链表%d\n,n);
scanf(%d %d,(p1-num),(p1-score));
}
p2-next=NULL;
puts(链表建立结束);
return head;
}
/*******************************************
函数名 :print()
参数 :结构体的首地址 *head1
功能 :输出链表
返回值 : 无
*******************************************/
void print(struct student *head1)
{
struct student *p3;
p3=head1;
puts(输出链表);
puts(学号 成绩);
while(p3!=NULL)
{
printf(%d %d\n,p3-num,p3-score);
p3=p3-next;
}
}
/*******************************************
函数名 :*del()
参数 :链表首地址 *head 以及要删除的学习
学号
功能 :把指定学号的学生信息从链表中
删除掉,
返回值 :返回链表的首地址head
********************************************/
struct student *del(struct student *head ,int num)
{
struct student *sp1,*sp2;
if(head==NULL) {printf(list is null!); goto End;}
sp1=head;
while(num!=sp1-num) //删除的不是所要找的结点
{
sp2=sp1;sp1=sp1-next; //当节点中的学号不想等时移动指针sp1
}
if(num==sp1-num)
文档评论(0)