簡體   English   中英

鏈表:在末尾插入一個節點

[英]Linked List: Inserting a node at the end

我一直在學習數據結構,目前正在使用鏈表。 我試圖在鏈表的末尾添加一個節點,但無法找出正確的邏輯。 我試過在開頭插入一個節點,它工作正常。

這是代碼:

#include <bits/stdc++.h>
using namespace std;

class Node {
    public:
        int data;
        Node* next;
};

Node* head; // global
void Insert(int data) {
    Node* temp = new Node();
    temp -> data = data;
    temp -> next = head;
    head = temp;
} // insert an integer

void Print(){
    Node* temp = head;
    cout << "List is: ";
    while (temp != NULL) {
        cout << temp -> data << " ";
        temp = temp -> next;
    }
    cout << endl;
} // print all elements in the list

void Delete(int n){
    Node* temp1 = head;
    if(n == 1) {
        head = temp1 -> next; // head now points to second node
        delete temp1;
        return;
    }
    int i;
    for(i = 0; i < n-2; i++)
        temp1 = temp1 -> next;
        // temp1 points to (n-1)th Node
    Node* temp2 = temp1 -> next; // nth Node
    temp1 -> next = temp2 -> next; // (n+1)th Node
    delete temp2; // delete temp2
} // Delete node at position n

int main() {
    head = NULL; // empty list
    Insert(2);
    Insert(4);
    Insert(6);
    Insert(5); // List: 2,4,6,5
    Print();
    int n;
    cout << "Enter a postion: " << endl;
    cin >> n;
    Delete(n);
    Print();
}

這段代碼刪除了第 n 個 position 處的節點。這里的節點是從頭開始添加的,我試圖找出從最后插入它的邏輯。

對此的任何建議和意見都會非常有幫助。

提前謝謝你。

玩代碼。

void insert_end(int data) {
    Node* temp = new Node();  // 1
    temp->data = data;
    temp -> next = nullptr;
    
    Node* n = head;
    if (!n) {         // 2
        head = temp;
        return;
    }
    while(n->next) {  // 3
        n = n->next;
    }
    n->next = temp;
}

該方法的簡短說明:

1:您創建一個新Node並設置數據。

2:檢查列表是否為空。 如果是,則在頭部插入新元素。

3:如果列表不為空,則讀取列表的下一個元素,直到列表中有最后一個節點。 如果你在這里寫while(n)... ,你會到達列表的末尾,這意味着nullptr並且代碼會中斷。

我一直在學習數據結構,目前正在使用鏈表。 我試圖在鏈表的末尾添加一個節點,但無法找出正確的邏輯。 我試過在開頭插入一個節點,它工作正常。

這是代碼:

#include <bits/stdc++.h>
using namespace std;

class Node {
    public:
        int data;
        Node* next;
};

Node* head; // global
void Insert(int data) {
    Node* temp = new Node();
    temp -> data = data;
    temp -> next = head;
    head = temp;
} // insert an integer

void Print(){
    Node* temp = head;
    cout << "List is: ";
    while (temp != NULL) {
        cout << temp -> data << " ";
        temp = temp -> next;
    }
    cout << endl;
} // print all elements in the list

void Delete(int n){
    Node* temp1 = head;
    if(n == 1) {
        head = temp1 -> next; // head now points to second node
        delete temp1;
        return;
    }
    int i;
    for(i = 0; i < n-2; i++)
        temp1 = temp1 -> next;
        // temp1 points to (n-1)th Node
    Node* temp2 = temp1 -> next; // nth Node
    temp1 -> next = temp2 -> next; // (n+1)th Node
    delete temp2; // delete temp2
} // Delete node at position n

int main() {
    head = NULL; // empty list
    Insert(2);
    Insert(4);
    Insert(6);
    Insert(5); // List: 2,4,6,5
    Print();
    int n;
    cout << "Enter a postion: " << endl;
    cin >> n;
    Delete(n);
    Print();
}

此代碼刪除第 n 個 position 的節點。 這里的節點是從頭開始添加的,我試圖找出從最后插入它的邏輯。

對此的任何建議和建議都將非常有幫助。

提前謝謝你。

暫無
暫無

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

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