当前位置: 代码迷 >> C语言 >> 求助,动态链表的问题
  详细解决方案

求助,动态链表的问题

热度:186   发布时间:2006-08-08 16:49:12.0
求助,动态链表的问题

#include<stdio.h>
#define LEN sizeof(struct student)
struct student
{int data;
struct student *next;
};


struct student *creat(struct student *head)
{struct student *p1,*newp;
head=(struct student *)malloc(LEN);
p1=(struct student *)malloc(LEN);
scanf("%d",&p1->data);
head=p1;
p1->next=NULL;
newp=(struct student*)malloc(LEN) ;
while(1)
{printf("please input the data");
scanf("%d",&newp->data);
if(newp->data==0)
break;
newp->next=NULL;
p1->next=newp;
p1=newp;

newp=(struct student*)malloc(LEN) ;
}
return head;
}
void Print(struct student *head)
{struct student *p;
p=head;
while(p->next!=NULL)
printf("%d\n",p->data);
p=p->next; }
main()
{struct student *head;
creat(head);
Print(head);
}
虽然通过了编译可是输出结果不对,一直输出很多数字,难道是跳不出while()循环

忘不吝赐教!

搜索更多相关的解决方案: 链表  student  struct  动态  LEN  

----------------解决方案--------------------------------------------------------

#include<stdio.h>
#define LEN sizeof(struct student)
struct student
{
int data ;
struct student*next ;
};


struct student*creat(struct student*head)
{
struct student*p1,*newp ;
head=(struct student*)malloc(LEN);
scanf("%d",&head->data);
head->next=NULL ;
p1=head;
while(1)
{
newp=(struct student*)malloc(LEN);
printf("please input the data");
scanf("%d",&newp->data);
if(newp->data==0)
break ;
newp->next=NULL ;
p1->next=newp ;
p1=newp ;

}
return head ;
}
void Print(struct student*head)
{
struct student *p ;
p=head ;
while(p)
{
printf("%d\n",p->data);
p=p->next ;
}
}
void Free(struct student *head)
{
struct student *p;
while(head)
{
p=head;
head=head->next;
free(p);
}
}
main()
{
struct student*head=NULL ;
Print(creat(head));
Free(head);
getch();
}



错误1:重复申请空间给head;
错误2:节点申请的空间没有释放;
警告:节点的连接显得很麻烦。


----------------解决方案--------------------------------------------------------
以下是引用soft_wind在2006-8-8 20:09:20的发言:

#include<stdio.h>
#define LEN sizeof(struct student)
struct student
{
int data ;
struct student*next ;
};


struct student*creat(struct student*head)
{
struct student*p1,*newp ;
head=(struct student*)malloc(LEN);
scanf("%d",&head->data);
head->next=NULL ;
p1=head;
while(1)
{
newp=(struct student*)malloc(LEN);
printf("please input the data");
scanf("%d",&newp->data);
if(newp->data==0)
break ;
newp->next=NULL ;
p1->next=newp ;
p1=newp ;

}
return head ;
}
void Print(struct student*head)
{
struct student *p ;
p=head ;
while(p)
{
printf("%d\n",p->data);
p=p->next ;
}
}
void Free(struct student *head)
{
struct student *p;
while(head)
{
p=head;
head=head->next;
free(p);
}
}
main()
{
struct student*head=NULL ;
Print(creat(head));
Free(head);
getch();
}



错误1:重复申请空间给head;
错误2:节点申请的空间没有释放;
警告:节点的连接显得很麻烦。

谢谢!
受教了!
----------------解决方案--------------------------------------------------------

有的书上head只是一个指向头节点的指针
而有的书head却直接是头节点。
做链表的程序总是在生成的时候出错。
不知道哪种思想简单一些


----------------解决方案--------------------------------------------------------
head直接做头结点,但它只有空间,没有实际的意义,即它不参加遍历,只做链表的首地址.
----------------解决方案--------------------------------------------------------

在两位的指点下终于程序终于能运行了。


----------------解决方案--------------------------------------------------------
  相关解决方案