链表删除问题,新手请教!

yoshikis 2012-05-11 11:48:34
链表删除问题
建立一个链表,每个结点包括:学号, 姓名,性别,年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将该结点删除。
以下是代码:请大牛帮忙修改下,现在的主要问题是程序没办法输出删除后的数据,我想了很久还是找不出问题所在,借大牛慧眼帮忙看一下修改下。



#include<stdio.h>
#include<stdlib.h>

struct student
{
int number;
char name[30];
char sex;
int age;
struct student *next;
};

int main()
{
struct student *head,*p1,*p2;
p1=(struct student*)malloc(sizeof(struct student));
p2=(struct student*)malloc(sizeof(struct student));
int n,c_age;
head=p1;
scanf("%d",&n);//输入链表的长度n
while(n--)
{
scanf("%d%s%c%d",&p1->number,&p1->name,&p1->sex,&p1->age);//学号、姓名、性别、年龄
p1=p1->next;
p1=(struct student*)malloc(sizeof(struct student));
}
p1=head;
scanf("%d",&c_age);// 输入要删除的年龄
for(int i=0;i<n;i++) //寻找是否有与输入的年龄c_age相同的年龄,如果有,删除掉
{
p2->next=p1;
if(c_age==p1->age)
{
p2->next=p1->next;
}
p2=(struct student*)malloc(sizeof(struct student));
printf("%d %s %c %d\n",p2->next->number,p2->next->name,p2->next->sex,p2->next->age);//输出删除数据后的链表数据
p1=p1->next;
}
system("pause");
return 0;
}

范例:
Sample Input

4
101 Ma m 20
102 Li f 23
103 Zhang m 19
104 Wang m 19
19

Sample Output

101 Ma m 20
102 Li f 23
...全文
147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yoshikis 2012-05-12
  • 打赏
  • 举报
回复
请问要怎么改呢?
sxldfang 2012-05-12
  • 打赏
  • 举报
回复
p1=p1->next;
p1=(struct student*)malloc(sizeof(struct student));
都给p1赋值?错了吧~~~
yoshikis 2012-05-12
  • 打赏
  • 举报
回复
无比感谢。分给你了。
阿七哥 2012-05-12
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int xuehao,age;
char name[20],sex;
struct node *next;
}lnode,*link;
//////////////////////////////////////////////////////
link wei()
{
link l,p,r;
int x,y;
char a[20],s;
l=(lnode*)malloc(sizeof(lnode));
l->next=NULL;
r=l;
printf("请输入学生的学号,年龄,性别:\n");
scanf("%d,%d,%c",&x,&y,&s);
while(x!=0000)
{
p=(lnode*)malloc(sizeof(lnode));
p->xuehao=x;
p->age=y;
//p->name=a;
p->sex=s;
p->next=r->next;
r->next=p;
r=p;
scanf("%d,%d,%c",&x,&y,&s);
}
return l;
printf("\n");
}
/////////////////////////////////////////////////////
void print1(link l)
{
link p;
p=l->next;
printf("原来的链表为:\n");
while(p!=NULL)
{
printf("[%d,%d,%c]->",p->xuehao,p->age,p->sex);
p=p->next;
}
printf("\n");
}
/////////////////////////////////////////////////////
void shan(link l)
{
int y;
link p,pre,t;
pre=l;
p=l->next;
printf("请输入一个年龄:\n");
scanf("%d",&y);
while(p!=NULL)
{
if(p->age!=y)
{
pre=p;
p=p->next;
}
else
{
t=p->next;
pre->next=p->next;
p=t;
}
}
}
////////////////////////////////////////////////////////////////////////////
void print(link l)
{
link p;
p=l->next;
printf("删除后的链表变为:\n");
while(p!=NULL)
{
printf("[%d,%d,%c]->",p->xuehao,p->age,p->sex);
p=p->next;
}
printf("\n");
}
/////////////////////////////////////////////////////////////////////////////
void main()
{
link l;
l=wei();
print1(l);
shan(l);
print(l);
}
/////////////////////////////////////////////////////////////////////////////////
没添加姓名的节点,不过整体思路就是这样,楼主参考参考
W170532934 2012-05-12
  • 打赏
  • 举报
回复
楼上威武
sxldfang 2012-05-12
  • 打赏
  • 举报
回复
分值太低了,结贴吧~~~

不是让你抄,而是通过阅读理解加以提高~~~



#include<stdio.h>
#include<stdlib.h>

struct student
{
int number;
char name[30];
char sex;
int age;
struct student *next;
};

int main()
{
struct student head,*p1=&head,*p2;
int n,c_age;
scanf("%d",&n);//输入链表的长度n
head.next=NULL;
while(n-->0)
{
p1->next=(struct student*)malloc(sizeof(struct student));
p1=p1->next;
p1->next=NULL;
scanf("%d %s %c %d",&p1->number,p1->name,&p1->sex,&p1->age);//学号、姓名、性别、年龄
}
printf("\n\n请输入要删除的年龄:");
scanf("%d",&c_age);// 输入要删除的年龄

printf("\n\n删除后的显示结果:\n");
p1=head.next;
while(p1!=NULL) //寻找是否有与输入的年龄c_age相同的年龄,如果有,删除掉
{
if(c_age!=p1->age)
{
printf("%5d %10s %5c %6d\n",p1->number,p1->name,p1->sex,p1->age);//输出删除数据后的链表数据
}
p2=p1->next;
free(p1);
p1=p2;
}
return 0;
}


运行结果:

4
101 Ma m 20
102 Li f 19
103 Zhang m 27
104 Wang f 19


请输入要删除的年龄:19


删除后的显示结果:
101 Ma m 20
103 Zhang m 27
请按任意键继续. . .

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧