C语言:动态链表的建立,查找,删除,插入功能的实现

#include<stdio.h>
#include<malloc.h>
struct student
{
	int num;
 
	float score;
 
	struct student *next;
};
int i, n;

struct student *creat()
{
 
	struct student *head, *p1, *p2;
 
	p1=p2=(struct student*)malloc(sizeof(struct student));
 
	printf("Please input student information numbers: ");
 
	scanf("%d", &n);

	for(i=0; i<n; i++)
 
	{
 
		printf("Num %d: ", i+1);
  
		scanf("%d", &p1->num);
  
		printf("Score %d: ", i+1);
  
		scanf("%f", &p1->score);
  
		if(i==0)
  
		{
   
			head  = p1;
  
		}
   
		if(i>0)
   
		{
   
			p2->next = p1;
   
		}
   
		p2 = p1;
  
		p1 = (struct student*)malloc(sizeof(struct student));
 
	}
 
	p2->next = NULL;
 
	return head;

}

void find(struct student *head)
{
 
	int s; 
 
	printf("\nPlease input num to find the student information: ");
 
	scanf("%d", &s);
 
	while( (head->num != s) && head->next)
 
	{
 
		head = head->next;
 
	}

	if( head->num == s )

	{
  
		printf("Num: %d\n", head->num);
 
		printf("Score: %.2f\n", head->score);
 
	}
 
	else
 
		printf("\nCan not find the student information\n\n");

}


struct student *del(struct student *head)

{ 
 
	struct student *p, *p1;
 
	int num;
 
	printf("\n\nPlease input your want to delete the student information's num: ");
 
	scanf("%d", &num);
 
	p = head;
 
	while(num != p->num && p->next)
 
	{
 
		p1 = p, p=p->next;

	}

	if( num == p->num )
 
	{
  
		if(p==head)
  
			head = p->next;
 
		else

			p1->next = p->next;
 
	}
 
	else
 
		printf("\n%d not found\n", num);
 
	return head;

}


struct student *insert(struct student *head)
{
 
	struct student  *p, *p1, *p2;
 
	p= (struct student*)malloc(sizeof(struct student));
 
	printf("Please input your want to insert the student information:\n");

	printf("Num: ");
 
	scanf("%d", &p->num);
 
	printf("Score: ");
 
	scanf("%f", &p->score);
 
	p1=head;
 
	if(head == NULL)
 
	{
  
		head = p;
  
		p->next = NULL;
 
	}

	while(p->num > p1->num && p1->next)
 
		p2 = p1, p1=p1->next;
 
	if( p->num <= p1->num )
 
	{
 
		if(head == p1)
  
			head = p, p->next=p1;
  
		else
  
			p2->next = p, p->next=p1;

	}
 
	else
 
		p1->next = p, p->next = NULL;
 
	return head;
}


void print(struct student *head)
{
	int i=0;
 
	printf("\n\noutput the students information:\n");
 
	while(head != NULL)

	{
	
  
		printf("Num %d: %d\n", i+1, head->num);
 
		printf("Score %d: %.2f\n\n", i+1, head->score);
  
		head = head->next;
		i++;
	}

}
main()
{

	struct student *head;
 
	head = creat();
 
	print(head);

	find(head);
 
	head = del(head);
 
	printf("\n                     After delete the student information\n");
 
	print(head);
 

	head = insert(head);
 
	printf("\n                     After insert the student information are \n");
 
	print(head);
}


 

  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值