关于链表的基础问题。我照着书写的代码没有结果。大神来看下这是为什么?

__103__ 2013-05-16 01:37:43
这是题目:
Description用结构体建立学生信息,学生信息包括学号、姓名、成绩,建立一个有 n 名学生的链
表, 并将链表输出。

Input一次输入学生信息包括学号、姓名。0 0 0结束程序

Output从链表表头到表位依次输出。


Sample Input

C1001 Li 70
M1002 He 89
E1003 Xie 83
M1004 Wu 92
E1005 Bao 80

Sample Output

C1001 Li 70
M1002 He 89
E1003 Xie 83
M1004 Wu 92
E1005 Bao 80


这是我的代码:
#include<stdio.h>
#include<stdlib.h>
# define len sizeof(struct stu)
struct stu
{
char num[20];
char name[20];
int scr;
struct stu *next;
};
int n;
struct stu *lib()
{
struct stu *head;
struct stu *p1,*p2;
n=0;
p1=p2=(struct stu *)malloc(len);
scanf("%s %s %d",p1->num,p1->name,&p1->scr);
head=NULL;
while(p1->num!="0"||p1->name!="0"||p1->scr!=0)
{
n+=1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct stu *)malloc(len);
scanf("%s %s %d",p1->num,p1->name,&p1->scr);
getchar();
}
p2->next=NULL;
return (head);
}

void prt(struct stu *head)
{
struct stu *p;
p=head;
if(head!=NULL)
{
while(p!=NULL)
{
printf("%s %s %d\n",p->num,p->name,p->scr);
p=p->next;
}
}
}
void main()
{
struct stu *head;
head=lib();
prt(head);
return ;
}


...全文
74 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-05-16
  • 打赏
  • 举报
回复
不要使用 while (条件) 更不要使用 while (组合条件) 要使用 while (1) { if (条件1) break; //... if (条件2) continue; //... if (条件3) return; //... } 因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。 典型如: 下面两段的语义都是当文件未结束时读字符 whlie (!feof(f)) { a=fgetc(f); //... b=fgetc(f);//可能此时已经feof了! //... } 而这样写就没有问题: whlie (1) { a=fgetc(f); if (feof(f)) break; //... b=fgetc(f); if (feof(f)) break; //... } 类似的例子还可以举很多。
__103__ 2013-05-16
  • 打赏
  • 举报
回复
引用 3 楼 Idle_Cloud 的回复:
while(p1->num!="0"||p1->name!="0"||p1->scr!=0) 字符串不可以这样比较,要用strcmp
原来是这样啊。我改了之后就对了,太感谢了。分数全给你。。
xiaoliang1201 2013-05-16
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include <string.h>

# define len sizeof(struct stu)

struct stu
{
char num[20];
char name[20];
int scr;
struct stu *next;
};
int n;

struct stu *lib()
{
	struct stu *head;
	struct stu *p1,*p2;
	n=0;
	p1=p2=(struct stu *)malloc(len);
	scanf("%s %s %d",p1->num,p1->name,&p1->scr);
	p1->next = NULL;
	head=NULL;
	//while(p1->num!="0"||p1->name!="0"||p1->scr!=0)  //字符串比较不能用“==”和“!=”直接比较
	while(0 != strcmp(p1->num,"0") || 0 != strcmp(p1->name,"0") || 0 != p1->scr)
	{
		n+=1;
		if(n==1)
			head=p1;
		else
			p2->next=p1;
		
		p2=p1;
		p1=(struct stu *)malloc(len);
		p1->next = NULL;    //设置为节点的指针域为空
		scanf("%s %s %d",p1->num,p1->name,&p1->scr);
		getchar();
	}
	p2->next=NULL;
	return (head);
}

void prt(struct stu *head)
{
	struct stu *p;
	p=head;
	if(head!=NULL)
	{
		while(p!=NULL)
		{
			printf("%s %s %d\n",p->num,p->name,p->scr);
			p=p->next;
		}
	}
}
void main()
{
	struct stu *head=NULL;
	head=lib();
	prt(head);
	return ;
}
Carl_CCC 2013-05-16
  • 打赏
  • 举报
回复
while(p1->num!="0"||p1->name!="0"||p1->scr!=0) 字符串不可以这样比较,要用strcmp
__103__ 2013-05-16
  • 打赏
  • 举报
回复
引用 1 楼 zjq9931 的回复:
单步跟踪调试吧。
调试过了,是第一个函数中while 循环不能终止。即使输入0 0 0;但是我不知道怎么改。
  • 打赏
  • 举报
回复
单步跟踪调试吧。

69,373

社区成员

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

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