当我试图在 C++ 中实现我的链表时出现奇怪的行为

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

我正在尝试实现我的单向链表,但我遇到了这个问题:

当我尝试 pushBack 我的链表中的某些元素时,它只会打印 第一个 我 added.For 示例,如果我尝试 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 指向它,但是您应该通过比较地址来检查是否只有一个节点。并且您应该考虑这两个功能,因为如果只有一个节点,头部尾巴将改变或者头部将在您的代码中被覆盖。



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;
        }
    }
 };


```