C语言链表删除经典笔试题,Problem A: C语言习题 链表建立,插入,删除,输出

#include

#include

#include

typedef struct student

{

long num;

float score;

struct student *next;

}student;

student *creatlink(void)

{

student *head = NULL;

student *last , *p ;

p =(student *)malloc(sizeof(student));

scanf("%ld%f",&p->num,&p->score);

while(p->num!=0)

{

if(head==NULL)

{

head=p;

}

else

last->next=p;

last=p;

p =(student *)malloc(sizeof(student));

scanf("%ld %f",&p->num,&p->score);

}

last->next=NULL;

free(p);

return head;

}

student *dellink(student *head,long del)

{

student *p1=head,*p;

while(p1)

{

if(del==p1->num)

{

p->next=p1->next;

free(p1);

break;

}

else

{

p=p1;

p1=p1->next;

}

}

return head;

}

void printlink(struct student *head)

{

while(head!=NULL)

{

printf("%ld %.2f\n",head->num,head->score);

head=head->next;

}

}

void freelink(struct student *head)

{

student *p1=head;

student *p;

while(p1)

{

p=p1->next;

free(p1);

p1=p;

}

}

student *insertlink(student *head,student *stu)

{

student *p=head;

student *pe;

student *last=NULL;

student *newbase=(student *)malloc(sizeof(student));

newbase->num=stu->num;

newbase->score=stu->score;

newbase->next=NULL;

if(newbase==NULL)

exit(-1);

while(p->next!=NULL)

{

p=p->next;

}

last=p;

p=head;

if(newbase->numnum)

{

newbase->next=head;

head=newbase;

}

else if(head->numnum&&newbase->numnum)

{

while((p->num<=newbase->num)&&(p->next!=NULL))

{

pe=p;

p=p->next;

}

newbase->next=p;

pe->next=newbase;

}

else

last->next=newbase;

return head;

}

int main()

{

struct student *creatlink(void);

struct student *dellink(struct student *,long);

struct student *insertlink(struct student *,struct student *);

void printlink(struct student *);

void freelink(struct student *);

struct student *head,stu;

long del_num;

head=creatlink();

scanf("%ld",&del_num);

head=dellink(head,del_num);

scanf("%ld%f",&stu.num,&stu.score);

head=insertlink(head,&stu);

scanf("%ld%f",&stu.num,&stu.score);

head=insertlink(head,&stu);

printlink(head);

freelink(head);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值