【发布时间】:2014-06-20 19:53:35
【问题描述】:
我已经开始用C编写链表了。我的代码如下:
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
struct node * create(struct node *);
display(struct node *);
int main()
{
struct node *head=NULL;
head = create(head);
display(head);
getch();
return 0;
}
struct node * create(struct node *Head)
{
struct node *temp;
char ch='y';
int i=1;
while(ch=='y')
{
temp=malloc(sizeof(struct node));
temp->data=i*10;
temp->next=Head;
Head=temp;
printf("\nAdded node : %d",Head->data);
printf("\nContinue to add more nodes ? : ");
scanf("%c",&ch);
}
return Head;
}
display(struct node *Head)
{
while(Head!=NULL)
{ printf("%d\t%d\n",Head->data,Head->next);
Head=Head->next;
}
}
我面临的问题是在进入函数“create”并只创建一个节点后,我的程序跳转回main,然后跳转到“display”函数,然后又跳转回函数“create”在询问我是否要继续的行。此时,当我输入“y”时,程序就退出了! 为什么会有这种行为?? 有人请解释一下控制是如何流动的以及为什么我的程序会出问题!!!
【问题讨论】:
标签: c function linked-list singly-linked-list control-flow