我有一個鏈表之後,我需要的頭之後創建一個節點..添加一個節點到鏈表頭
這意味着我有這樣的事情:
node *head = NULL;
,並在結束我的鏈表應該是這樣的:
head -> node -> NULL
...
但是當我使用一個正常的ADDNODE福nction,它給了我一個運行時錯誤(不知道哪個,我調試有問題)...
這是我寫的:
void addNode(node *head)
{
node *temp = head; // a temp to not move the head
node *newNode = (node*)malloc(sizeof(node)); // The new node
while (temp -> next != NULL)
{
temp = temp -> next // Getting to the last node
}
temp -> next= newNode; // Adding the new node into the linked list insted of the NULL
newNode -> next = NULL; // Adding the NULL after the new node
}
此代碼的偉大工程給我時,我有一個鏈表已經有1個或更多的節點,但如果鏈表只有一個頭,它會對我造成問題......我該如何解決這個問題?
(如果你不明白我的問題 - 隨着ADDNODE功能我寫到這裏,我得到一個運行時錯誤添加一個新的節點進入指向已經NULL頭)..
謝謝,阿米特:)
那麼如何添加頭後的新節點,如果頭爲空? – AmitM9S6
非常感謝,我可以在head後添加一個新節點,當head = null時,使用這兩行代碼: 'head =(node *)malloc(sizeof(node)); head - > next = null',它現在可以工作:) – AmitM9S6
沒問題。我的榮幸:)如果它解決了您的問題,請點擊帖子下的標記。人們會知道問題解決了。 – Blood