Linked List implementation in C++

The more detailed explanation of linked list and its implementation in C, C++, Java, and Pythion is given here.

Algorithm for adding an element  (data) to the front of the list.

1. Declare and initialize necessary variables
2. Create an empty node and assign it to node pointer p.
3. Take input data and assign it to info field of new node
i.e. p->info = data;
4. Set next field. i.e. p->next = list;
5. Set list pointer to p i.e. list = p;
6. For next insertion operation, goto step 2

Insertion new node at the end.

1. Declare neccessary variables.
2. Create an empty node and assign it to node pointer p.
3. Insert the data item to info field of newly created node,
i.e. p->info = data;
4. Assign NULL to the next field of newly created node
i.e. p->next = NULL;
5. If list == NULL;
then list = p;
Else
Find the last node of the current list say l.
then l->next = p;
6. For next insertion, goto step 2.

Deletion of the first node

1. Declare the necessary variables
2. If list = NULL
print “Empty list”
Else
p = list
list = p -> next
3. Remove the node p i.e. delete(p)
4. For next deletion, goto step 2

Deletion of the last node

1. Declare necessary variable
2. If list = NULL
print “Empty List”
Else
set p = list
if p->next = NULL
list = NULL
delete(p)
Else
while(p->next!=NULL)
temp = p
p = p->next
temp->next = NULL
delete(p)
3. For next deletion goto step 2

The source code in C++ for whole process include inserting and deleting elements before and after the given node is given below:

SHARE Linked List implementation in C++

You may also like...

5 Responses

  1. Anonymous says:

    Hi Bibek, greeting.. your programming techniques are simply nice..

  2. Anonymous says:

    Why you allocate memory for temporary variables?. Finally those temporary variables is not deleted.. I am sure this will leads to memory leak.

  3. Anonymous says:

    Bibek your program is not working (((

Leave a Reply

Your email address will not be published. Required fields are marked *

Share