c程序设计——链表的输入,输出,插入,删除

c程序设计笔记
链表实现动态输入,输出,多次插入,多次删除功能

在多次插入时,要多次用malloc开辟动态存储空间,否则,将输出时陷入死循环

#include<stdio.h>
#include <malloc.h>
#define L sizeof(struct student)
struct student					//定义一个结构体 
{
	int num;
	int score;
	struct student *next;
};
int n;							//结点数 



struct student *creat()			//实现结构体的输入 
{
	struct student *head;
	struct student *p1,*p2;
	n=0;	
	p1=p2=(struct student*)malloc (L);			//开辟动态存储空间 
	printf("请输入学生 学号  成绩\n");
	scanf("%d,%d",&p1->num,&p1->score);
	head=NULL;
	while(p1->num!=0)
	{
		n++;									//输入的学号不是0,结点数加一
		if(n==1)
		{
			head=p1;							//先将p1赋给头结点 
		}
			
		else
		{
			p2->next=p1;						//第n次以后执行(n>=2),所以知道上一个结点为p2
		}
		
		p2=p1;									//将p1赋给p2,下面重新输入p1,p2表示p1上一个结点
		
		p1=(struct student*)malloc(L);		//开辟动态存储空间,如果不开辟,重新占据上一个输入的p1值	
		printf("请输入学生 学号  成绩\n");
		scanf("%d,%d",&p1->num,&p1->score);
	}//while循环结束 
	
	
	p2->next=NULL;								//输出链表结束,将尾结点的指向下一个结点的指针,指向空指针
	return head;

	
}


struct student *del(struct student *head,int num)		//实现删除结点;定义一个返回值为结构体指针的函数 ; 
{
	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是上一个while找到要删除的结点 
	{
		if(head==p1)									//判断找到的结点是否为头结点 
			head=p1->next;
		else											//不是头结点,直接把要删除结点的next赋给上一个结点的next(要删除节点是尾结点也可以实现) 
			p2->next=p1->next;
		printf("delete:%d\n",num);
		n--;
	}
	else												//没有找到结点,一直到尾节点也没找到										 
		printf("%d not been found! \n",num);

	return head;
}

struct student *insert(struct student *head,struct student *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)													//while第一个条件出来 
	{
		if(head==p1)
		{														//要插入到原头节点之前 
			head=p0;
		}
		else													 //要插入的点,在链表中间
		{
			p2->next=p0;
		}
																		 
	
		p0->next=p1;
		
		
	}
		
	else																	//while第二个条件出来 ,要插在原尾结点后面 
	{
		p1->next=p0;
		p0->next=NULL;
	}

	n++;
	return head;
}

void print(struct student *head)										//实现输出功能 
{ 
	struct student *p;
	printf("\nNow,These %d retords are:\n",n);
	p=head;
	printf("num    score\n");
	if(head!=NULL)
	do
	{
		
		printf("%4d %4d\n",p->num,p->score);
		p=p->next;
	}while(p!=NULL);
}
int main(void)
{
	struct student *head,*stu;
	int num;
	head=creat();
	print(head);
	
	printf("\nenter the node to delete\n");
	scanf("%d",&num);
	while(num!=0)
	{
		head=del(head,num);
		print(head);
		printf("\nenter the node to delete\n");
		scanf("%d",&num);
	}
	
	
	
	printf("\ninput the inserted record:");
	stu=(struct student*)malloc (L);				//注意每次插入时,都要开辟动态空间 
	scanf("%d,%d",&stu->num,&stu->score);
	while(stu->num!=NULL)
	{
		head=insert(head,stu);
		print(head);
		printf("\ninput the inserted record:");
		stu=(struct student*)malloc (L);			//注意每次插入时,都要开辟动态空间 
		scanf("%d,%d",&stu->num,&stu->score);
		
	}
	
}

运行

请输入学生 学号  成绩
1001,1
请输入学生 学号  成绩
1002,2
请输入学生 学号  成绩
1003,3
请输入学生 学号  成绩
1004,4
请输入学生 学号  成绩
1005,5
请输入学生 学号  成绩
0,0

Now,These 5 retords are:
num    score
1001    1
1002    2
1003    3
1004    4
1005    5

enter the node to delete
1003
delete:1003

Now,These 4 retords are:
num    score
1001    1
1002    2
1004    4
1005    5

enter the node to delete
1005
delete:1005

Now,These 3 retords are:
num    score
1001    1
1002    2
1004    4

enter the node to delete
0

input the inserted record:1005,5

Now,These 4 retords are:
num    score
1001    1
1002    2
1004    4
1005    5

input the inserted record:1007,7

Now,These 5 retords are:
num    score
1001    1
1002    2
1004    4
1005    5
1007    7

input the inserted record:1000,0

Now,These 6 retords are:
num    score
1000    0
1001    1
1002    2
1004    4
1005    5
1007    7

input the inserted record:1003,3

Now,These 7 retords are:
num    score
1000    0
1001    1
1002    2
1003    3
1004    4
1005    5
1007    7

input the inserted record:0,0

--------------------------------
Process exited after 43.32 seconds with return value 0
请按任意键继续. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值