C语言链表节点删除问题
题目:试编写一个程序完成:建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。
问题(错误):在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);
}
问题(错误):在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);
}
...全文
相关推荐
小甲鱼 约瑟夫环问题 数据结构 C语言循环链表
根据小甲鱼的视频 自己写的约瑟夫环。
用C语言实现约瑟夫环问题
用C语言实现的约瑟夫环问题,我是数据结构的课程设计,因此用的数据结构的知识,存储使用但循环链表实现的,当然数组也可以解决此问题
链表-使用C语言实现循环链表应用之解决约瑟夫问题.zip
链表 链表_使用C语言实现循环链表应用之解决约瑟夫问题
c语言约瑟夫问题实验报告
已知n个人(不妨分别以编号1,2,3,…,n 代表 )围坐在一张圆桌周围,编号为k 的人从1开始报数,数到m的那个人出列,他的下一个人又从 1开始继续报数,数到m的那个人出列,…,依此重复下去,直到圆桌周围的人全部出列。直到圆桌周围只剩一个人为止。
C语言实现链表,创建、插入、查找、删除、遍历等基本操作
C与语言实现链表的创建、插入(头插法、尾插法)、遍历、查找、删除操作


