笔记——C语言动态链表

先补充一个我会忽略的知识点:
1、print:将信息显示在命令窗口中,输出光标定位在最后一个字符之后。

2、printf:将信息进行格式化显示在命令窗口中,输出光标定位在最后一个字符之后。

3、println:将信息显示在命令窗口中,输出光标换行定位在下一行开头。
单项动态链表的建立主要是反复执行以下3个步骤:
(1)调用malloc函数向系统申请一个节点的存储空间;
(2)输入该节点的数据;
(3)将该节点加入到链表。

//函数功能:建立有若干学生数据的单向动态链表
#include <stdio.h>
#include <malloc.h>
# define NULL 0
# define LEN sizeof(struct student)
struct student
{
	int num;
	float score;
	struct student *next;
};

int n;  //n为节点个数,被定义为全局变量,可被其他函数所用

struct student *creat(void)			//函数无形参
{
	struct student *head,*p1,*p2;
	head=Null;
	n=0;
	p1=p2=(*struct student)malloc(LEN);//分配一个内存空间,p1,p2同时指向第一个节点
	scanf("\t%d,%f",&p1->num,&p1->score); //输入第一个节点的数据
	while(p1->num!=0)							//学号为零,循环结束
	{
		n=n+1;		
		if(n==1)
			head=p1;
		else
			p2->next=p1;
		p2=p1;							         //p2指向新的表尾
		p1=(*struct student)malloc(LEN);
		scanf("\t%d,%f",&p1->num,&p1->score);
	}
	p2->next=NULL;
	free(p1);
	return(head);

}

删除链表结点

要求:编写一个函数,删除动态链表中的指定节点。
分析:
(1)首先找到准备删除的节点;
(2)找到节点后,修改节点的链接关系;
(3)删除该节点后,释放该节点空间。

struct student *del(struct student *head,int num)
{
	struct student *head,*p1,*p2;
	if(head==NULL)
	{
		printf("\nThe List is NULL!\n");
		return(head);
	} //链表为空,输出提示信息后返回
	p1=head;
	if(p1->num!=num & p1->next!=NULL)		//p1不是所要找的节点,且p1不是尾节点
	{
		p2=p1;
		p1=p1->next;
	}

	if(p1->num==num)
	{
		if(p1==head)
			head=p1->next;
		else
			p2->next=p1->next;
		free(p1);
		printf("Delete num is %d\n",num);
		n=n-1;
	}
	else
		printf("%d is not been found!\n",num);

	return(head);


}

插入链表结点

要求:编写一个函数,在链表中插入一个结点
分析:
(1)找到新结点应该插入在哪个位置,找到该节点的前一个结点,以备插入之用;
(2)通过修改相关指针完成插入。

struct student *insert(struct student *head,struct student *stud)
//形参stud是要指向要插入的新节点
{
	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->num<=p1->num)		//新节点数据小于当前节点数据
		{
			if(head==p1)
				head=p0;
			else
				p2->next=p0;
			p0->next=p1;
		}
		else					//新节点插入到表尾
		{
			p1->next=p0;
			p0->next=NULL;
		}
	}
	n=n+1;
	return(head);
}

综合例程,链表的建立、输出、删除和插入

//函数功能:建立有若干学生数据的单向动态链表
//建表、输出、删除和插入以及主函数可放在同一文件中,也可单独放在.c文件中
#include <stdio.h>
#include <malloc.h>
# define NULL 0
# define LEN sizeof(struct student)
struct student
{
	int num;
	float score;
	struct student *next;
};

int n;  //n为节点个数,被定义为全局变量,可被其他函数所用

struct student *creat(void)			//创建链表,函数无形参
{
	struct student *head,*p1,*p2;
	head=NULL;
	n=0;
	p1=p2=(struct student *)malloc(LEN);//分配一个内存空间,p1,p2同时指向第一个节点
	scanf("\t%d,%f",&p1->num,&p1->score); //输入第一个节点的数据
	while(p1->num!=0)							//学号为零,循环结束
	{
		n=n+1;		
		if(n==1)
			head=p1;
		else
			p2->next=p1;
		p2=p1;							         //p2指向新的表尾
		p1=(struct student *)malloc(LEN);
		scanf("\t%d,%f",&p1->num,&p1->score);
	}
	p2->next=NULL;
	free(p1);
	return(head);

}

struct student *del(struct student *head,int num)	//删除链表结点
{
	struct student *p1,*p2;
	if(head==NULL)
	{
		printf("\nThe List is NULL!\n");
		return(head);
	} //链表为空,输出提示信息后返回
	p1=head;
	if(p1->num!=num & p1->next!=NULL)		//p1不是所要找的节点,且p1不是尾节点
	{
		p2=p1;
		p1=p1->next;
	}

	if(p1->num==num)
	{
		if(p1==head)
			head=p1->next;
		else
			p2->next=p1->next;
		free(p1);
		printf("Delete num is %d\n",num);
		n=n-1;
	}
	else
		printf("%d is not been found!\n",num);

	return(head);


}


struct student *insert(struct student *head,struct student *stud)	//插入链表结点
//形参stud是要指向要插入的新节点
{
	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->num<=p1->num)		//新节点数据小于当前节点数据
		{
			if(head==p1)
				head=p0;
			else
				p2->next=p0;
			p0->next=p1;
		}
		else					//新节点插入到表尾
		{
			p1->next=p0;
			p0->next=NULL;
		}
	}
	n=n+1;
	return(head);
}

void print(struct student *head)
{
	struct student *p;
	printf("共有 %d 条学生记录,包括:\n",n);	//n为全局变量
	p=head;
	while(p!=NULL)
	{
		printf("\t%d\t%0.1f\n",p->num,p->score);
		p=p->next;
	}
}


void main()
{
	struct student *head,*stu;
	int del_num;
	printf("请输入记录:\n");
	head=creat();			//建立链表
	print(head);
	printf("请输入删除的记录编号:\n");
	scanf("%d",&del_num);
	while(del_num!=0)
	{
		head=del(head,del_num);		//删除指定结点
		print(head);
		printf("请输入删除的记录编号");
		scanf("%d",&del_num);
	}
    printf("请输入插入的记录:\n");
	stu=(struct student *)malloc(LEN);
	scanf("%d,%f",&stu->num,&stu->score);
	while(stu->num!=0)
	{
		head=insert(head,stu);		//插入新的结点
		print(head);
		printf("请输入插入的记录:\n");
		stu=(struct student *)malloc(LEN);
		scanf("%d,%f",&stu->num,&stu->score);
	}


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值