【发布时间】:2015-07-08 06:04:24
【问题描述】:
我正在接近使用此代码来构建单链表。我指出让我有点困惑的台词。
struct node
{
int data;
struct node *next;
}*start=NULL; // point 1
void creat()
{
char ch;
do
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data : ");
scanf("%d",&new_node->data); //point 2
new_node->next=NULL; // point 3
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
printf("\nDo you want to creat another : ");
ch=getche();
}while(ch!='n');
}
point1:将结构节点声明为指针有什么好处?
point2: scanf("%d",&new_node->data);
不明白数据如何在指针和结构的上下文中存储到“数据”字段中?
【问题讨论】:
-
缩进,缩进 =)
-
你知道指针的用途和用途吗?
-
谨防将
scanf("%d", …)与任何“获取字符”例程一起使用。scanf()在输入缓冲区中留下一个换行符以供读取。getche()例程特定于 Windows,我不确定它是否清除缓冲区并要求输入新的单个字符,但如果没有,您的代码似乎行为不端,而不是等待您的答案提示。 -
@JonathanLeffler:很好的报价提醒 :)
标签: c pointers linked-list structure