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