C语言链表节点删除问题

Zhngbn斌 Zhngbn斌 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
题目:试编写一个程序完成:建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。
问题(错误):在struct student *cancel()的定义中出现了10几个错误。
1.struct student *head,*p1,*p2;这里missing ';' before 'type'
2.head=p2=p1=p; ,有'head' : undeclared identifier
3.'p1' : undeclared identifier
left of '->ages' must point to struct/union
left of '->next' must point to struct/union
'p2' : undeclared identifier
left of '->next' must point to struct/union
left of '->next' must point to struct/union
left of '->next' must point to struct/union
left of '->next' must point to struct/union
希望大家能帮忙解决,已经想了好久了,都找不到问题所在,希望大家能帮我改改代码,指出问题所在。感激不尽
源代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
char sex;
int ages;
struct student *next;
};
int n;

struct student *creat() //不需要传入,只需返回一个head地址
{
struct student *head,*p1,*p2;
n=0;
head=NULL;
p1=p2=(struct student *)malloc(LEN);
scanf("%d%s%c%d",&p1->num,p1->name,&p1->sex,*&p1->ages);
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%d%s%c%d",&p1->num,p1->name,&p1->sex,&p1->ages);
}
p2->next=NULL;
return(head);
}

struct student *cancel(struct stduent *p)
{
int getage;
scanf("%d",&getage);
struct student *head,*p1,*p2;
head=p2=p1=p; //把p的地址传给前者

while(p1!=0) //p1是p2前面的表头
{
if(p1->ages==getage)
{
if(head==p1)
head=p1->next;
else
p2->next=p1->next;
p1=p1->next;
}
else
{
p2=p1;
p1=p1->next;
}
}
return head;
}

void print(struct student *p)
{
while(p1!=0)
{
printf("%-5d %-8s %c %2d\n",p->num,p->name,p->sex,p->ages);
p1=p1->next;
}
}

void main()
{
struct student *head; //返回的head地址传给head指针变量
struct stduent *head1;
head=creat();
head1=cancel(head);
print(head1);
}
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
Zhngbn斌
C语言 70,040 成员 243,237 内容