Line by Line Explanation for Linked List Program in C++

Yasir
3 min readMay 7, 2020

--

Line by Line Explanation of Linked List Program

Program For Linked List in C++

Line 3 :- Here we are creating a structure named as node , as each node has two things , first one is its own value and second thing the memory address for next node , so here we are taking two properties data and next , next points to the address of next pointer and its type is node itself & data its own value.

Line 9 :- Here we are creating a class “linked_list” which has two sections , first one is private and second one is public .

Let’s understand Public and Private first !

Public :- All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too.

Private :- The class members declared as private can be accessed only by the functions inside the class.

Line 11 :- In this private part we are declaring two pointers pointing head and tail of the linked list of type node .

Line 13 :- In this public part , there is some functions and a Constructor .

Let’s understand Constructor first !

Constructor :- It is a member function of a class which initialises objects of a class. Every time when member are used , those constructor called automatically.

Line 14 :- This is a Constructor named as “linked_list” and having two initialised head and tail both having NULL value .

Line 20 :- Here is a method named as “add_node” which is an application of linked list for adding a new node . In this method we are using a keyword “new” and an “->” .

Let’s understand “new” and “->” first !

New :- The nodes of the linked list are allocated in the heap memory. We can use new operator in C++ for dynamic memory allocation and delete operator to deallocate the allotted memory.

Line 22 :- Here is a pointer named as tmp and We are allocating the space required for a node by the new operator.

Line 23 :- We are giving a value to the data of tmp as passed to the function.

Line 24 :- We have given the value to data in the previous line and a value of the pointer next (NULL) in this line and thus making our node tmp complete.

Line 26 :- If head is NULL, our current node (tmp) is the first node of the linked list and this it will be head and tail both (as it is also the last element right now).If head is not NULL, it means that we have a linked list and we just have to add the node at the end of the linked list.

Line 31 :- The new node (tmp) will go after the tail and then we are changing the tail because the new node is the new ‘tail’.

Line 39 :- In main function , we are creating a variable a of type linked-list which is a class . And accessing their method by dot and passing parameters of that methods .

Congratulations !! , this was the whole easy line by line explanation for this Topic .

Don’t forget to CLAP if you like it , Comment your suggestions also !!!

--

--

Yasir
Yasir

Written by Yasir

Exploring Swift, SwiftUI, and Apple Universe.

No responses yet