簡體   English   中英

當我嘗試在 c++ 中實現我的鏈表時出現奇怪的行為

[英]Strange behaviour when I am trying to implement my linked list in c++

我正在嘗試實現我的單鏈表,但我遇到了這個問題:

當我嘗試 pushBack 鏈接列表中的某些元素時,它只會打印我添加的第一個元素。例如,如果我嘗試 pushBack 2、3、4 - 它只會打印 2。

如果我想 pushUp 鏈接列表中的一些元素,它只會打印我添加的第三個元素。 例如,如果我嘗試 pushUp 2,3,4 - 它只會打印 4。

這是我的代碼:

在此處輸入代碼

#include<iostream>
#include<vector>
using namespace std;

struct Node {
   int data;
   Node* next;
};

class LinkedList {
private:
    // Create pointers for head and tail
    Node *head , *tail;

public:
    LinkedList(){
        // Initiate them as null pointers
        head = NULL;
        tail = NULL;
    }

public:
    void pushBack(int value){
        // Should add a node at the end of the linked list
        
        Node* temp = new Node(); // temporary node which should be added
        temp->data = value; // value to store
        temp->next = NULL; // pointer to the next node

        if(head != NULL){
            // If there are some elements , then 
            temp->next = tail->next;
            tail = temp;
        }

        if(head == NULL){
            // If there are no elements , our node will be a head and a tail in the same time.
            head = temp; 
            tail = temp;
        }

    }

    void pushUp(int value){
        // Shound add a node at the beginning of the linked list
       Node* temp = new Node();
       temp->data = value;
       temp->next = NULL;

       if(head == NULL){
           // If there are no elements , our node will be a head and a tail in the same time.
           head = temp;
           tail = temp;
       }

       if(head != NULL){
           // If there are some elements , just make our node to be new head.
           temp->next = head->next;
           head = temp;
       }
    }

    void traversal(){
        Node *temp = new Node();
        temp = head;

        while(temp != NULL){
            cout << temp->data << " ";
            temp = temp->next;
        }
    }
 };


int main(){
// Pointer for our first node.
LinkedList a;

a.pushUp(2);
a.pushUp(124);
a.pushUp(3);

// a.pushBack(2);
// a.pushBack(124);
// a.pushBack(3); // Outputs only 2

a.traversal();  // Outputs only 3
}

您缺少邊緣情況。 當您添加第一個節點時,您可以通過 head 和 tail 指向它,但是您應該通過比較地址來檢查是否只有一個節點。 並且您應該為 function 考慮它,因為如果只有一個節點,頭部尾部將會改變,或者頭部將在您的代碼中被覆蓋。



class LinkedList {
private:
    // Create pointers for head and tail
    Node *head , *tail;

public:
    LinkedList(){
        // Initiate them as null pointers
        head = NULL;
        tail = NULL;
    }

public:
    void pushBack(int value){
        // Should add a node at the end of the linked list
        
        Node* temp = new Node(); // temporary node which should be added
        temp->data = value; // value to store
        temp->next = NULL; // pointer to the next node

        if(head != NULL){
            // If there are some elements , then 
            if(tail!=NULL){
                tail->next = temp;
            }else {
                tail = temp;
                head->next = tail;
            }
        }else {
            // If there are no elements , our node will be a head and a tail in the same time.
            head = temp; 
        }

    }

    void pushUp(int value){
        // Shound add a node at the beginning of the linked list
       Node* temp = new Node();
       temp->data = value;
       temp->next = NULL;

       if(head == NULL){
           // If there are no elements , our node will be a head and a tail in the same time.
           head = temp; 
       }else {
           // If there are some elements , just make our node to be new head.
           if(tail != NULL){
               temp->next = head;
               head = temp;
           }else {
               tail = head;
               head = temp;
               temp->next = tail;
           }
       }
    }

    void traversal(){
        Node *temp = new Node();
        temp = head;

        while(temp != NULL){
            cout << temp->data << " ";
            temp = temp->next;
        }
    }
 };


```

暫無
暫無

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

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