链表问题

elegant87 2008-11-22 08:24:37

//看看哪有问题?运行的时候输入时候可以正常,输出的时候出问题了。
//当学号为0的时候停止
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#define NULL 0

typedef struct stu
{
long num;
char name[20];
char sex;
struct stu *next;
}*student;

void ceate(student &head)
{
student p1,p2;
head=(student)malloc(sizeof(stu));
head->next=NULL;
p2=head;
p1=(student)malloc(sizeof(stu));
printf("请输入学号: ");
scanf("%ld",&p1->num);
while(p1->num!=0)
{
printf("请输入姓名: ");
scanf("%s",p1->name);
printf("请输入性别: ");
scanf("%s",&p1->sex);
p2->next=p1;
p2=p1;
p1=(student)malloc(sizeof(stu));
printf("请输入学号: ");
scanf("%ld",&p1->num);
}
p2->next=NULL;
}

void print(student head)
{
student q;
q = head->next;
if (q!= NULL)
while (q!=NULL)
{
printf("%ld\t%s\t%s\n",q->num,q->name,q->sex);
q=q->next;
}
else
printf("void list!\n");
}

int main()
{
student head=NULL;
ceate(head);
print(head);
system("PAUSE");
return 0;
}
...全文
79 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
elegant87 2008-12-08
  • 打赏
  • 举报
回复
就当送分了!
njucs 2008-11-22
  • 打赏
  • 举报
回复
1. name需要预先memset(p1->name, '\0', 20);
2. printf("%ld\t%s\t%c\n",q->num,q->name,q->sex);

至少这样是可以的:)

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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