-2

I'm trying to implement my singly linked list , and I have this problem:

When I'm trying to pushBack some elements in my linked list , it will print only the first one which I added.For example , if I try to pushBack 2,3,4 - it will print only 2.

In case if I want to pushUp some elements in my linked list , it will print only the third one which I added. For example , if I try to pushUp 2,3,4 - it will print only 4.

This is my code:

enter code here

#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
}
  • 2
    Your `(head != NULL)` test in pushUp will always pass since you just assigned something to head in the previous test. Also `temp->next = tail->next;` in pushBack seems wrong to me. – GazTheDestroyer Mar 09 '21 at 09:11
  • `head != NULL` must be an `else`. And in traversal, why do you do `new Node`? – Devolus Mar 09 '21 at 09:13
  • 3
    Take out pencil and paper and draw what happens. (There are several problems.) Then work out, with pencil and paper, what *should* happen. Then translate that procedure into code. – molbdnilo Mar 09 '21 at 09:14
  • 1
    I'd recommend you to step through your code line by line [with the debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) line by line. You'll quckly observe what was said in the comment before, and see what's going wrong. – πάντα ῥεῖ Mar 09 '21 at 09:14

1 Answers1

0

You are missing edge cases. When you add the first node you are pointing it via head and tail ok but then you should check if there is only one node by comparing the address. And you should consider it for both function because if there is only one node head tail will change or head will be overwritten in your code.



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


```
Savrona
  • 173
  • 1
  • 10