1
我的代碼編譯正確,但是當我執行時,insertLast被調用兩次,然後我的程序凍結。我看不出爲什麼它會工作兩次,但後來凍結。凍結添加到鏈接列表(C)
到節點發送到我的鏈表代碼:
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++;
}
}
有些奇怪的是,你不要在main中聲明'ii',而在'insertLast'中有一個本地'ii',它似乎沒有做任何事情。 – 2013-05-03 01:31:13