0

I am implementing a linked list but it doesn't display any output.

implementing a linked list with 4 elements and making fuction insert and insertathead and display.

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

// class node to make node element.
class node
{
public:
    int data;
    node *next;
    node{
    }
    // constructor
    node(int val)
    {
        data = val;
        next = NULL;
    }
     
    // function to insert element at head.
    void insertAthead(node *&head, int val)
    {
        node *n = new node(val);
        n->next = head;
        head = n;
        return;
    }

    // function to insert in linked list.
    void insert(node *head, int val)
    {
        node *temp = head;
        if (head == NULL)
        {
            insertAthead(head, val);
            return;
        }
        node *n = new node(val);
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        temp->next = n;
    }

    // function to display each element in a linked list.
    void display(node *head)
    {
        while (head->next != NULL)
        {
            cout << head->data;
            head = head->next;
        }
        cout << head->data;
    }
};

//main function 

int main()
{

    node *head;    // line at which I think there is a problem
    head->insert(head, 1);
    head->insert(head, 2);
    head->insert(head, 3);
    head->insert(head, 4);
    head->display(head);
    return 0;
}

I think the problem is at line 1

node *head;

when I change this line with node *head=new node(any integer value) the code runs fine.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Accessing `head->...` without initializing `head` is UB. – wohlstad Jul 16 '22 at 16:30
  • 1
    [Why should I always enable compiler warnings?](https://stackoverflow.com/questions/57842756/) Because your compiler wants to tell you what the problem is, in less time than it takes to post a question. – JaMiT Jul 16 '22 at 16:48
  • Just as a side note: [Why should I not #include ?](https://stackoverflow.com/q/31816095/12149471) – Andreas Wenzel Jul 16 '22 at 16:53

1 Answers1

1

First you want to move insert, insertAtHead and display out of the Node class. These are list functions have have no need of special access to Node.

Also you should initialise head to NULL (representing the empty list).

This means changing main like this

node *head = NULL;
insert(head, 1);
insert(head, 2);
insert(head, 3);
insert(head, 4);
display(head);

Then you want to change

void insert(node *head, int val)

to

void insert(node *&head, int val)

Just like you have done (correctly) with insertAtHead.

If you wanted to take this further you could create a List class, along these lines

class List
{
public:
    List() { root = NULL; }
    void insert(int val);
    void insertAtHead(int val);
    void display();
private:
    Node* root;
};

then main would look like this

int main()
{
    List l;
    l.insert(1);
    l.insert(2);
    l.insert(3);
    l.insert(4);
    l.display();
}
john
  • 85,011
  • 4
  • 57
  • 81
  • And if you are appending to the list then the list should keep track of the tail and not search for it on every insert. – Goswin von Brederlow Jul 16 '22 at 16:48
  • I have a question can i Initialize head to NULL with constructor. Like earlier my code was node *head; can i make a constructor that automatically initializes this head to NULL. – Mohammed faiz Khan Jul 16 '22 at 17:57
  • 1
    @MohammedfaizKhan You cannot write a constructor for a pointer because a pointer is not a class. But you can put a pointer in a class and write a constructor that sets the pointer inside the class to NULL. That's what I did with my List class above. – john Jul 16 '22 at 19:08
  • Thanks john, thanks for helping I'm kinda new to C++ and I'm finding this pointer part very difficult. – Mohammed faiz Khan Jul 17 '22 at 04:52
  • 1
    @MohammedfaizKhan That's a very common feeling. It clicks eventually. – john Jul 17 '22 at 06:37