链表的输入,输出,查找,删除,插入

本文通过C语言实现学生信息链表的创建、打印、查找、删除和添加功能,详细介绍了链表的操作过程和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(student)
typedef struct student
{
long num;
    float score;
    struct student *next;
}student;
    int n=0;
student *create()
{
student *head;
student *p1,*p2;
p1=p2=(student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
void PrintStudent(student *head)
{
student *p;
p=head;
if(p!=NULL)
do
{
printf("%ld,%f",p->num,p->score);
p=p->next;
}
while(p!=NULL);
}


student *SearchStudent(student *head,long num)
{
student *p;
p=head;
while(p!=NULL&&p->num!=num)
p=p->next;
return p;
}
student *DeleteStudent(student *head,long num)
{
    student *p,*pre;
p=head;
if(head->num==num)
{
pre=head;
head=head->next;
}
else
{
pre=head;
p=head->next;
while(p!=NULL)
{
if(p->num==num)
break;
else
{
pre=p;
p=p->next;
}
}
printf("the pre num is %d\n",pre->num);
pre->next=pre->next->next;
}
return head;
}
student *AddStudent(student *head,long num,long newnum,float score)
{
student *p,*s;
p=SearchStudent(head,num);
s=(student *)malloc(sizeof(student));
s->num=newnum;
s->score=score;
s->next=p->next;
p->next=s;
return head;
}
void main()
{
student *pt;
student *result;
long num1,num2,num3,num4;
float s;
pt=create();
PrintStudent(pt);
printf("\n");
scanf("%ld",&num1);
result=SearchStudent(pt,num1);
printf("the result is :%ld\n%f\n",result->num,result->score);
    scanf("%ld",&num2);
DeleteStudent(pt,num2);
PrintStudent(pt);
scanf("%ld,%ld,%f",&num3,&num4,&s);
AddStudent(pt,num3,num4,s);
PrintStudent(pt);

}


不会写main函数。。若是叫我脱离笔记我也写不出来。。刚刚接触,稍微陌生。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值