我有一个链表,其中包含一种遍历列表并打印出链表中结构值的方法。
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++;
}
}