delete complete linked list with program and Images,better understanding

By | February 24, 2017

If we pass head pointer to Delete Method,it should delete the complete linkedlist.Here Main Logic lies as below Steps

Step 1: Store Head element into current element

Step 2: Point “next Element” of current element to “next Pointer”

Step 3: delete current element

Step 4: store next pointer to current element

Step 5: move next to next element

Step 6: Loop through,untill we found next element as NULL.

Delete Complete linked list explained with beautiful images

Delete Complete linked list explained with beautiful images

Delete Complete linked list Step 1

                                                                                       Delete Complete linked list Step 1

In the above diagram,i am pointing current to -> 10 and Next to -> 20 as first step.

 

Delete Complete linked list Step 2

                                                                                      Delete Complete linked list Step 2

Here,we first delete the current element and store next to current element and

 

Delete Complete linked list Step 3

Delete Complete linked list Step 3

Delete Complete linked list Step 4

Delete Complete linked list Step 4

we keep moving the current element to next until we found no element in the linked list.

Let’s go to the code,compilation and running steps.

#include <bits/stdc++.h>
#include <string.h>
using namespace std;
// Structure of node.It contains two elements.one is string.
// Second one is Pointer to Node

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

//global variable
node *head;

/* Function to create a new node with given data */
node* newNode(int data)
{
 node *new_node = new node;
 new_node->data = data;
 new_node->next = NULL;
 return new_node;
}


void displayLinkedList(node *head)
{
 struct node *temp;
 
 if(head == NULL)
 {
 printf("I am sorry Bro,LinkedList is empty.\n\n");
 }
 else
 {
 temp = head;
 printf("Displaying linked List.\n\n");
 while(temp != NULL)
 {
 cout<<temp->data<<"=====>";
 temp = temp->next; 
 }
 
 printf("END\n\n"); 
 }
} 

// Main Logic 

void deleteLinkedList(node** head)
{
 //take current pointer
 struct node* current = *head;
 //take next pointer
 struct node* next;

 // this loop i most important to delete the linked list nodes and keep moving.
 while(current != NULL)
 {
 next = current->next;
 free(current);
 current = next;
 }
 //after this loop every node in the linked list should be deleted.
 
 *head = NULL;
 
}

//Main program
int main()
{
 
 printf("Creating Linked list \n");
 /*Create head element */
 head = newNode(10);
 /*Add Second element 10->20 */
 head->next = newNode(20);
 /*Add Third element 10->20->30*/
 head->next->next = newNode(30);
 /*Add Fourth element 10->20->30->40*/
 head->next->next->next = newNode(40);
 /*Add Fourth element 10->20->30->40->50*/
 head->next->next->next->next = newNode(50);
 /*Add Fourth element 10->20->30->40->50->60*/
 head->next->next->next->next->next = newNode(60); 
 
 //Display created linked list.
 printf("Displaying the Linked List Before Deletion\n");
 displayLinkedList(head);

 //call delete linked list on the pointer to the head.
 deleteLinkedList(&head);
 
 //Display after deletion.
 printf("Displaying the Linked List After Deletion\n");
 displayLinkedList(head);
 
 return 0;
}

Here is the way to compile the program and run it.

Deleting the complete linkedlist explained with compilation and running

Deleting the complete linkedlist explained with compilation and running

The following two tabs change content below.

SRINIVAS DARIPELLI

Myself SRINIVAS DARIPELLI. I have 15+ Years of Experience in Programming worked on multiple technologies.Apart from it,I am a blogger, writer, editor, artist and dad 🙂 .I believe in reality.I love to share the Helpful things around the Technology. Feel free to connect with me

One thought on “delete complete linked list with program and Images,better understanding

Leave a Reply