大家帮我看看这个是是不是VC6.0的BUG???

来源:百度知道 编辑:UC知道 时间:2024/06/05 19:56:53
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
int n=0;
typedef struct student
{
char *data;
struct student *next;
}stu_st,*stu_pst;

stu_pst InsertStu(stu_pst head,char *m)
{
stu_pst p,stu1;
stu1=(stu_pst)malloc(sizeof(stu_st));
stu1->data=(char *)malloc(sizeof(char)*50);
strcpy(stu1->data,m);
p=head;
while(p->next)
p=p->next;
p->next=stu1;
stu1->next=NULL;
n++;
return head;
}
void main()
{
stu_pst Head,q;
Head=(stu_pst)malloc(sizeof(stu_st));
Head->next=NULL;
int i=0,flag=0;
char *Str;
Str=(char *)malloc(sizeof(char)*50);
while(1)
{
gets(Str);
Head=InsertStu(Head,Str);
printf("输入了%d个\n",n);
if(*Str=='z')
break;
}
q=Head;
for(i=0;i<n;i++)
{

没有bug,你的程序有问题,在最后输出那里...
q=Head;
for(i=0;i<n;i++)
{
//这里有问题,你的链表是头结点没有数据的,所以应该为 printf("%s\n",q->next->data);
printf("%s\n",q->data);

q=q->next;
}