急:建立动态链表的问题
根据动态链表建立的算法,我写下了如下的程序,可是在输出的时候不对?不知道问题出在哪里?请各位耐心回答,谢谢
程序如下:
//CreatList.c
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct NODE{
struct NODE *next;
int n;
}node;
node *creat();
int main()
{
node *p;
p=creat();
while(p!=NULL)
{
printf("%d\n",p->n);
p=p->next;
}
return 0;
}
node *creat()
{
node *head=NULL,*new_node,*current; //new_node:新建结点的指针 current:已经建好的链表的最后结点
printf("Begin to creat a list:\n");
current=new_node=(node*)malloc(sizeof(node));
scanf("%d",&new_node->n);
head=new_node;
int flag=1;
while(flag)
{
char c;
printf("do you want to go on to build it?(y,n) ");
scanf("%c",&c);
if(c=='y')
{
current->next=new_node;
current=new_node;
new_node=(node*)malloc(sizeof(node));
scanf("%d",&new_node->n);
}
else if(c=='n') flag=0;
}
current->next=NULL;
return head;
}
[ 本帖最后由 yqw1122 于 2009-11-5 21:48 编辑 ]