【发布时间】:2013-04-26 06:35:18
【问题描述】:
我有一个链表,其中包含一个遍历链表并打印出链表中结构值的方法。
void testLinkedList(LinkedList* list)
{
int count = 1;
LinkedListNode* current = list->head;
while (current != NULL)
{
printf("%d: Label is is %d\n", count, current->data->label);
current = current->next;
count++;
}
}
我在循环中做错了吗?它应该在到达最后一个节点时结束,但只要我允许,它将继续循环并打印出幻数。
编辑:这是我用来发送到链表的 insertlast() 函数:
void insertLast(LinkedList* list, TinCan* newData)
{
int ii = 1;
LinkedListNode* newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
newNode->data = newData;
//check if queue empty
if(list->head == NULL)
{
list->head = newNode;
newNode->next=NULL;
}
else
{
LinkedListNode* current = list->head;
while (current->next != NULL)
{
current = current->next;
}
current->next = newNode;
printf("%d", ii);
ii++;
}
}
【问题讨论】:
-
您在创建/插入列表时可能搞砸了,最后一项没有将其 next 指针设置为 null。
-
你能发布你的 LinkListNode 结构吗?
-
旁注:你的计数是错误的第一个sn-p。即使在具有 NULL 头的列表上,它也会评估为 1。希望它的价值不重要。
标签: c loops data-structures linked-list