1

After compiling -when the cmdl pops up- it doesn't terminate and waits just as awaiting input

#include <bits/stdc++.h>

using namespace std;

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

class LinkedList{
private:
    struct node *head;
    struct node *tail;
public:
    LinkedList(int val1,int val2){
        head->next = tail;
        head->data = val1;
        //tail->next = NULL;
        //tail->data = val2;
    }
    add(int val){
        struct node *n = new node;
        n->next = head->next;
        head->next = n;
        n->data = val;
    }
    display(){
        struct node *ptr = head;
        while(ptr->next!=NULL){
            cout<<ptr->data;
            ptr = ptr->next;
        }
    }
};

int main(){
    LinkedList l(1,3);
    for(int i = 0;i<5;i++) l.add(i);
    l.display();
}

What prevents the code from executing as expected? I tried some built-in functions to test the code however none of them responded to the calls and had effect.

Mat
  • 188,820
  • 38
  • 367
  • 383
emirhan422
  • 75
  • 5

2 Answers2

2

I get an access violation when running this code.

adding

head = new node;

at the beginning of your constructor fixes that.

I would also explicitly initialize head and tail to null like this

private:
     struct node *head = NULL;
     struct node *tail = NULL;

Otherwise they're filled with garbage values and you'll potentially get an infinite loop in your display code.

iverss
  • 106
  • 7
  • 1
    Also worth asking "Why is my constructor adding a node?" Having to supply a an item to the constructor will make this liked list less useful as you now must know what the first item will be when creating the node, and this is a luxury you often do not have. – user4581301 Mar 16 '20 at 15:17
-1

You should initialize both ‘head’ and ‘tail’ as null in the constructor because you have not yet added any element to your linked list and thus both are NULL. Best practices that you add some return type to avoid warnings.

#include <iostream>

using namespace std;

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

class LinkedList{
private:
    node *head = NULL; //initialize head 
    node *tail = NULL; //initialize tail
public:
    LinkedList(int val1,int val2){
        head->next = tail;
        head->data = val1; //??? I guess you mean tail->data = val1;?
        //tail->next = NULL;
        //tail->data = val2;
    }
//specify what type of return if it has no return add void
    void add(int val){ 
        struct node *n = new node;
        n->next = head->next;
        head->next = n;
        n->data = val;
    }
//also here
    void display(){
        node *ptr = head;
        while(ptr->next!=NULL){
            cout<<ptr->data;
            ptr = ptr->next;
        }
    }
};



int main()
{
  LinkedList l(1, 3);
    for(int i = 0;i<5;i++) l.add(i);
    l.display();

}
  • It's recommended by the community guidelines that clarifying questions be submitted in the comment section rather than the answer section. Best of luck! – iverss Mar 16 '20 at 14:57
  • 1
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Dharman Mar 16 '20 at 15:14
  • With the current suggestion `head->next = tail;` would attempt to deference a null pointer. The asker is only marginally better off because the undefined behaviour invoked will almost certainly crash the program. Program will still be wrong. – user4581301 Mar 16 '20 at 18:52