C语言链表问题...

糊糊糊糊糊糊萝卜 2011-12-16 08:49:12
#include <stdio.h>
#include <stdlib.h>
#include<string.h>

struct student
{
long num;
char name[20];
char sex[10];
char year[200];
struct student *next;
};


struct student *create()
{
struct student *head, *p1, *p2;
int i, len;

head=NULL;
len=sizeof(struct student);
for(i=0; i<5; i++)
{
p1=(struct student *)malloc(len);
scanf("%ld %s %s %s",&p1->num, p1->name,p1->sex,p1->year);
if(i==0) head=p2=p1;
else
{
p2->next=p1;
p2=p1;
}
if(i==4) p2->next=NULL;
}
return head;

}

void display(struct student *head)
{
struct student *p1;

p1=head;
while(p1!=NULL)
{
printf("%ld\n%s\n%s\n%s\n",p1->num,p1->name,p1->sex,p1->year);
p1=p1->next;
}

}

int Delete(struct student **head,char a[200])
{
struct student *p1, *p2;

p1=p2=*head;
while(p1!=NULL)
{
if (strcmp(p1->year,a)==0)
{
if (p1==*head)
*head=p1->next;
else
p2->next=p1->next;
free(p1);
return 1;
}

p2=p1;
p1=p1->next;
}
return 0;
}

void insert(struct student *head,char a[200])
{
struct student *p1,*q;
q=(struct student *)malloc(sizeof(struct student));
strcpy(q->name,"aaa");
strcpy(q->sex,"male");
strcpy(q->year,a);
p1=head;
while(p1->next!=NULL)
{
p1=p1->next;
}
p1->next=q;
q->next=NULL;
}
int main( )
{
struct student *head;
char a[200];

head=create();
scanf("%s",a);
Delete(&head,a);
if (Delete(&head,a)!=1)
insert(head,a);
display(head);
return 0;
}

题目如下:
描述: 建立一个链表,每个节点包括学生的学号、姓名、性别、年龄。先输入5个学生的数据,再输入一个年龄,如果链表中有年龄等于此年龄的记录,则删除所有年龄等于此年龄的记录,否则在链表的最后增加一个新节点,学号为180姓名为"aaa",性别为"male"。

输入: 创建链表时输入5个职工的职工号和工资,学号为大于100且小于200的整数,姓名为长度小于20的字符串,性别为长度小于10的字符串,年龄为大于等于0且小于200的整数。

输出: 按顺序输出链表中的所有数据,每个数字占一行。
输入样例: 107 maliu male 28
109 niuqi female 22
101 zhangsan male 30
103 lisi female 18
105 wangwu male 25
28
输出样例: 109
niuqi
female
22
101
zhangsan
male
30
103
lisi
female
18
105
wangwu
male
25


但我的输出后面多了 180; aaa; male; 28;
求教各位!!!!!!!!!
...全文
105 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaochizhen33 2011-12-17
  • 打赏
  • 举报
回复
直接复制代码。。看着头都大了
limit1980 2011-12-17
  • 打赏
  • 举报
回复

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

struct student
{
long num;
char name[20];
char sex[10];
char year[200];
struct student *next;
};


struct student *create()
{
struct student *head, *p1, *p2;
int i, len;

head=NULL;
len=sizeof(struct student);
for(i=0; i<5; i++)
{
p1=(struct student *)malloc(len);
scanf("%ld %s %s %s",&p1->num, p1->name,p1->sex,p1->year);
if(i==0) head=p2=p1;
else
{
p2->next=p1;
p2=p1;
}
if(i==4) p2->next=NULL;
}
return head;

}

void display(struct student *head)
{
struct student *p1;

p1=head;
while(p1!=NULL)
{
printf("%ld\n%s\n%s\n%s\n",p1->num,p1->name,p1->sex,p1->year);
p1=p1->next;
}

}

int Delete(struct student **head,char a[200])
{
struct student *p1, *p2;
int flag=0;
p1=p2=*head;
while(p1!=NULL)
{
//这个不符合题目要求,下面是我修改的程序,写的不是很好,但是能够执行
if (strcmp(p1->year,a)==0)
{
flag = 1;
if (p1==*head)
{
*head=p1->next;
free(p1);
p1 = *head;

continue;
}
else
{
p2->next=p1->next;
free(p1);
p1 =p2;

}


}

p2=p1;
p1=p1->next;
}
return (flag==1)? 1 : 0;
}

void insert(struct student *head,char a[200])
{
struct student *p1,*q;
q=(struct student *)malloc(sizeof(struct student));
q->num = 180;//这儿缺少这个
strcpy(q->name,"aaa");
strcpy(q->sex,"male");
strcpy(q->year,a);
p1=head;
while(p1->next!=NULL)
{
p1=p1->next;
}
p1->next=q;
q->next=NULL;
}
int main( )
{
struct student *head;
char a[200];

head=create();
scanf("%s",a);
//Delete(&head,a);这个是多余的,因为你第一次已经删除了,所以下面的判断会成功,执行了insert函数。就会多出来一行
if (Delete(&head,a)!=1)
insert(head,a);
display(head);
return 0;
}
zjs100901 2011-12-16
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include<string.h>

struct student
{
long num;
char name[20];
char sex[10];
char year[200];
struct student *next;
};

struct student *create()
{
struct student *head, *p1, *p2;
int i, len;

head=NULL;
len=sizeof(struct student);
for(i=0; i<5; i++)
{
p1=(struct student *)malloc(len);
scanf("%ld %s %s %s",&p1->num, p1->name,p1->sex,p1->year);
if(i==0) head=p2=p1;
else
{
p2->next=p1;
p2=p1;
}
if(i==4) p2->next=NULL;
}
return head;

}

void display(struct student *head)
{
struct student *p1;

p1=head;
while(p1!=NULL)
{
printf("%ld\n%s\n%s\n%s\n",p1->num,p1->name,p1->sex,p1->year);
p1=p1->next;
}

}

int Delete(struct student **head,char a[200])
{
struct student *p1, *p2;

p1=p2=*head;
while(p1!=NULL)
{
if (strcmp(p1->year,a)==0)
{
if (p1==*head)
*head=p1->next;
else
p2->next=p1->next;
free(p1);
return 1;
}

p2=p1;
p1=p1->next;
}
return 0;
}

void insert(struct student *head,char a[200])
{
struct student *p1,*q;
q=(struct student *)malloc(sizeof(struct student));
q->num = 180;//////////////////////////////这里
strcpy(q->name,"aaa");
strcpy(q->sex,"male");
strcpy(q->year,a);
p1=head;
while(p1->next!=NULL)
{
p1=p1->next;
}
p1->next=q;
q->next=NULL;
}

int main( )
{
struct student *head;
char a[200];

head=create();
scanf("%s",a);
//Delete(&head,a);//////////////////////////////这里
if (Delete(&head,a)!=1)
insert(head,a);
display(head);
return 0;
}

69,371

社区成员

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

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