簡體   English   中英

我很困惑為什么這個鏈表實現有效

[英]I'm confused on why this Linked List implementation works

我最近剛剛進入鏈接列表,並且正在添加我自己的“添加到結尾”功能。

void insert_at_end(Node** head, Node* node)
{
    Node* temp;

    temp = *head;

    if (temp == NULL) // linked list is empty
    {
        *head = node;
    }
    else
    {
        while (temp->pNext != NULL)
        {
            temp = temp->pNext;
        }
    temp->pNext = node;
    }
}

最初我在做temp != NULL而不是temp->pNext != NULL因為我認為這樣它會帶我到最后一個節點,因為當 temp 在達到 NULL 之前仍有數據時它會停止循環。 temp->pNext != NULL不會停在倒數第二個節點嗎? 因為當它意識到最后一個節點的 next 指針為 NULL 時它停止循環,它不會傳播到那個節點?

謝謝大家,如果我需要從嘔吐這個詞中清除任何內容,請告訴我。

temp->pNext != NULL不會停在倒數第二個節點嗎?

不。為了便於理解,這里是圖示。

使用temp != NULLtemp將指向NULL

 +---+    +---+    +---+
 | 1 |--->| 2 |--->| 3 |---> NULL
 +---+    +---+    +---+      ^
                              |
                             temp

正如您所看到的temp正在迭代直到它變為NULL ,此時您無法將任何內容附加到最終節點,因為您已經過去了。


使用temp->pNext != NULLtemp將指向最后一個節點。

 +---+    +---+    +---+
 | 1 |--->| 2 |--->| 3 |---> NULL
 +---+    +---+    +---+
                     ^
                     |
                    temp

這里temp將迭代,直到它的下一個節點為NULL 這意味着您指向最后一個節點,您可以使用該指針調整該節點以指向一個具有temp->pNext = node的新temp->pNext = node

 +---+    +---+    +---+    +---+
 | 1 |--->| 2 |--->| 3 |--->| 4 |---> NULL
 +---+    +---+    +---+    +---+
                     ^
                     |
                    temp

順便說一句,您可能還想增加額外的安全性,以確保您獲得指向NULL的節點, NULL調用者可能忘記這樣做。 這就像將這一行添加到函數的開頭一樣簡單:

node->pNext = NULL;

讓我們打個比方。

  • 每個節點都是一個站。
  • NULL是您的停靠站。
  • while (temp->pNext != NULL) { temp = temp->pNext; } while (temp->pNext != NULL) { temp = temp->pNext; }表示開車並訪問每個站點,直到到達停止站點。
  • 到達停靠站時,需要通過temp->pNext = node;添加一個額外的站temp->pNext = node;

現在,前一個停靠站和新添加的停靠站(隱式新的NULL不是停靠站,而是要訪問的倒數第二個站。

有一個簡單鏈表概念的參考

讓我們舉個例子

考慮

struct node          
{
  int data;// data which you want to feed.
  struct node *next;// gona hold the address of the next data, so that link can be established
}*root,*p; // root is the starting reference to your linked list

情況 1:您沒有插入任何元素並且您的鏈表為空。 現在,當您插入新數據時 10.它會檢查全局指針引用中指向您的鏈表的任何數據,如果指針似乎為 NULL,則表示您喜歡的列表為空。內存堆棧將被創建如下這個。

10

1000

10 是數據,1000 是它的地址。

情況2:在它的后面添加一個元素。你想將數據20添加到你的鏈表中。你將首先使用全局鏈表地址引用(它將是一個指針變量)查看鏈表是否為空,根據您的代碼,持有您的鏈表第一個地址)

現在當我們看到 root->data is not null 1000->data is 10

我們去 root->next ,它是空的,我們知道這是最后一個節點。 1000->下一個是NULL

並且我們將新節點地址插入到 root->next 中,以便創建到新插入節點的鏈接。

堆棧將像這樣創建

10 20

1000 1004

案例3:現在您想再次添加另一個數據,例如 30 到列表的末尾。 只需按照與案例 2 相同的方法操作即可。

檢查當前為空的節點,即 root->next == NULL,為此您使用循環來查找當前位於最后的節點,如下所示。

 struct node *temp; // creating a temp node which will be the new node we will insert
 temp = (struct node *)malloc(sizeof(struct node));// allocating the size for the temp node, same as the size of previous nodes.
 p = root;// i am giving my starting address to pointer p, i traverse the node with pointer p only, since i dont want to loose my root starting address.
 while(p->next != NULL)// checking for the last node
    p = p->next;// if last node not found, just move to next node, and see whether it is last node or not
 p->next = temp;// if last node is found, put the address of newly created temp node to the node previously found last in the linked list.
 temp->data = element;// feeding data to the temp node.
 temp->next = NULL;// keeping temp node as last, it is necessary to say temp wont has any more node connected.

注意不要認為這只是一個臨時節點,一旦我們退出函數就會消失,它正在玩指針,所以它不會銷毀,直到所有者不會銷毀它。

流量將是

新創建的臨時地址是 1008

1000->接下來是1004

1004->下一個是NULL

所以,1004->next 現在將持有 1008

那么,1008->數據將是30

然后,1008->next 將為 NULL

堆棧將像這樣創建

10 20 30

1000 1004 1008

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM