Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

A question about single linked list in C++.

+4 votes
asked Nov 8, 2021 by Areeb Sherjil (1,960 points)
Hi, as a C++ beginner, I am trying to learn about linked lists. I made a basic linked list but I am not sure how to print it?

code:

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

class linked_list
{
private:
    node *head,*tail;
public:
    linked_list()
    {
        head = NULL;
        tail = NULL;
    }

    void add_node(int n)
    {
     
        node *tmp = new node;
        tmp->data = n;
        tmp->next = NULL;

        if(head == NULL)
        {
            head = tmp;
            tail = tmp;
        }
        else
        {
            tail->next = tmp;
            tail = tail->next;
        }
        
    }
    
    void print()const;
};

void linked_list::print() const
{
    std::cout<<head->data<<std::endl;
    std::cout<<tail->data<<std::endl;
}

int main()
{
    linked_list a;
    a.add_node(1);
    a.add_node(2);
    a.add_node(3);
    
    a.print();
}

Now my question is where does the '2' disappear and how can I print it?

1 Answer

+1 vote
answered Nov 8, 2021 by Peter Minarik (84,720 points)
selected Nov 9, 2021 by Areeb Sherjil
 
Best answer

Where's 2?

It's in the middle of your linked list and in your print() function you only print the head and the tail. So there's no wonder it is not displayed in the standard output.

 [1] → [2] → [3]
  ↑           ↑
[HEAD]      [TAIL]

To print all the elements, iterate starting from the head and go until you reach nullptr and print elements one by one.

Further Notes

Use Standard Containers

In general, it is a good idea to use the standard containers offered by the language. They have been widely tested. Custom containers take time to implement and are prone to have bugs.

As mentioned earlier, you can just use the std::list if you need a doubly-linked list or std::forward_list if you need a singly-linked list.

Write Your Own Proper Generic Container

If you want to write your own container, you should do it properly. It is a good idea to write it in a way that it is widely usable and not just suitable for a very specific task (e.g. only supports ints).

So here are some tips:

  • Implement the destructor to prevent memory leaking (release the resources!)
  • Use generics (templates) to allow any kind of item to be stored in your linked list
  • Instead of NULL, use the nullptr C++ keyword.

When Sharing Your Code

It is also a good idea to share your code in a state that it compiles. Have all the #include, #define, typedef, etc statements included with your code. This one someone could just simply take your code, run it and see what's going on, they won't need to spend time fixing it to be able to run it.

commented Nov 8, 2021 by Areeb Sherjil (1,960 points)
Thanks man, next time I will share my full working code, it's just that people at stack overflow only want a code snippet.

Also, how can I print out 2 from the code I just gave, i.e how to access the middle element?
commented Nov 9, 2021 by Peter Minarik (84,720 points)
edited Nov 9, 2021 by Peter Minarik
This is how you'd normally iterate through all the elements of a linked list.

void linked_list::print() const
{
    for (const node * n = head; n; n = n->next)
        std::cout << n->data << std::endl;
}
commented Nov 9, 2021 by Areeb Sherjil (1,960 points)
thanks man! I get it now
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...