【发布时间】:2015-11-01 09:39:54
【问题描述】:
我正在尝试插入链接列表,但在调用 display() 方法时没有得到正确的输出。向链表插入数据时一切正常。
insert() 方法中的 printf 语句打印:
a int
b int
c int
但是当调用 display() 方法时,它会打印:
c
c
c
结构的数据类型成员根本不打印。而且,我认为 identifierName 成员每次都会被覆盖。在我对我的代码进行 sn-p 之后:
struct symbol
{
char* identifierName;
char* datatype;
struct symbol* next;
};
void insert(struct symbol** headRef,char* identifier,char* type)
{
struct symbol* newnode = (struct symbol*) malloc(sizeof(struct symbol));
newnode->identifierName = identifier;
newnode->datatype = type;
newnode->next = (*headRef);
(*headRef) = newnode;
printf("%s %s\n",newnode->identifierName,newnode->datatype); //debugging
}
void display(struct symbol* node)
{
while(node!=NULL)
{
printf("%s %s\n",node->identifierName,node->datatype);
node = node->next;
}
}
【问题讨论】:
-
该错误很可能出现在对
insert的调用中。显示你的整个代码。 -
在调试器下运行时发现了什么?
标签: c pointers linked-list structure