我的代码编译正确,但是当我执行时, insertLast 被调用两次,然后我的程序冻结了。我不明白为什么它会工作两次但然后冻结。
将节点发送到我的链表的代码:
int main ()
{
LinkedList* canQueue=createList();
for(ii = 0; ii < 10; ii++)
{
TinCan* tempCan = (TinCan*) malloc(sizeof(TinCan));
insertLast(canQueue, tempCan);
}
return 0;
}
我使用的链表方法:
LinkedList* createList() /*creates empty linked list*/
{
LinkedList* myList;
myList = (LinkedList*)malloc(sizeof(LinkedList));
myList->head = NULL;
return myList;
}
void insertLast(LinkedList* list, TinCan *newData)
{
int ii = 1;
LinkedListNode* newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
newNode->data = newData;
newNode->next = NULL;
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;
ii++;
}
}