Program To Delete All Nodes Of Singly Linked List

Description

To delete all elements of singly linked list - 
Case 1: check if list is empty 
	Show message that list is empty.
Case 2: If list has elements, call the delete_beg(which will delete first node 
	    of list) function recursively until it delete all elements of list.

Note: Here head contains the address of first node

C/C++

/* C Program To Delete All Nodes Of singly linked list */
//Save it as delete_all_nodes_singly_linked_list.c

#include<stdio.h>
#include<malloc.h>
struct node *create(int);
void display(struct node *);
struct node *delete_list(struct node *);
struct node *delete_beg(struct node *);

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

int main(){
    int n;
    struct node *head;
    printf("Enter number of nodes : ");
    scanf("%d",&n);
    
    //Calling function to create node
    head=create(n);
    
    //Calling function to display list
    display(head);
    
    //Code to delete all nodes from singly linked list
    head=delete_list(head);
    printf("\nThe linked list after deletion of all node - \n");
    
    //Calling function to display list after deletion of all nodes from list
    display(head);

    return 0;
}

struct node *create(int n){
    int i;
    struct node *head=NULL;
    struct node *address,*newNode;

    for(i=0;i<n;i++){
        
        //Creating a new node
        newNode=(struct node*)malloc(sizeof(struct node*));
        
        //Assigning data to newly created node
        printf("Enter the value of %d node : ",i+1);
        scanf("%d",&newNode->data);
        newNode->next=NULL;
        
        /*If list is empty assign the address of newly created node to head*/
        if(head==NULL){
            head=newNode;
        }else{
            
            /* If list already have few elements then loop through
            list and add newly created node after given node*/
            address=head;
            while(address->next!=NULL){
                address=address->next;
            }
            address->next=newNode;
        }
    }
    return head;
}

void display(struct node *head){
    struct node *address;
    
    //If list is empty
    if(head==NULL){
        printf("The linked list is empty");
    }else{
        
        /*If list has elements then loop through the loop and
        print elements one by one in sequential manner*/
        address=head;
        while(address!=NULL){
            printf("The node value = %d and their address = %u\n",address->data,address);
            address=address->next;
        }
    }
}

//Function to delete all elements of list
struct node *delete_list(struct node *head){
    struct node *address;
    
    //If list has elements
    if(head!=NULL){
        address=head;
        
        //Traversing untill list has element
        while(address!=NULL){
            printf("Deleting the node %d...\n", address -> data);
            
            //Calling function to delete first node
            head = delete_beg(address);
            address = head;
        }
    }else{
        printf("\nNo node to delete!!");
    }
    return head;
}

//Function to delete a node from beginning
struct node *delete_beg(struct node *head){
    
    //No node is available to delete
    if(head==NULL){
        printf("Link list is empty..No node to delete!!");
    }else{
        
        //Assigning the address first node to temporary variable
        struct node *address=head;
        head=address->next;
        
        //Free the memory after deletion of node
        free(address);
    }
    return head;
}
Input:
Enter number of nodes : 5
Enter the value of 1 node : 1
Enter the value of 2 node : 2
Enter the value of 3 node : 3
Enter the value of 4 node : 4
Enter the value of 5 node : 5

Output:
The node value = 1 and their address = 3935576
The node value = 2 and their address = 3935608
The node value = 3 and their address = 3935624
The node value = 4 and their address = 3935640
The node value = 5 and their address = 3935656
Deleting the node 1...
Deleting the node 2...
Deleting the node 3...
Deleting the node 4...
Deleting the node 5...

The linked list after deletion of all node -
The linked list is empty

Java

/* Java Program To Delete All Nodes Of singly linked list */
//Save it as DeleteAllNodesSinglyLinkedList.java

import java.io.*;
import java.util.Scanner;

public class DeleteAllNodesSinglyLinkedList {
    
    // Declaring node
    class Node {
        int data;
        Node next;

        public Node(int data) {
            this.data = data;
            
            // Assigning null value to the next field
            this.next = null;
        }
    }

    public Node head = null;
    
    // Function to add node
    public void addNode(int data) {
        
        // Creating a new node
        Node newNode = new Node(data);
        
        /* If list is empty assign the address of newly created node to head */
        if (head == null) {
            head = newNode;
        } else {
            
            /*
             * If list already have few elements then loop through list and
             */
            Node ptr = head;
            while (ptr.next != null) {
                ptr = ptr.next;
            }
            ptr.next = newNode;
        }
    }

    public void display() {
        Node ptr = head;
        
        // If list is empty
        if (ptr == null) {
            System.out.println("The list is empty");
            return;
        }
        System.out.println("The nodes of lists are : ");
        
        /*
         * If list has elements then loop through the loop and print elements one by one
         * in sequential manner
         */
        while (ptr != null) {
            System.out.print(ptr.data + " ");
            ptr = ptr.next;
        }
    }

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        DeleteAllNodesSinglyLinkedList list = new DeleteAllNodesSinglyLinkedList();

        System.out.println("Enter number of node you want to enter : ");
        int n = scanner.nextInt();

        for (int i = 0; i < n; i++) {
            System.out.println("Enter value of " + (i + 1) + " node : ");
            int val = scanner.nextInt();
            list.addNode(val);
        }
        
        // Calling function to display list
        list.display();

        list.delete_list();
        
        // Calling function to display list after deletion of all nodes
        System.out.println("\nAfter deletion of all nodes - ");
        list.display();
    }
    
    //Method to delete all nodes from list
    private void delete_list() {
        Node address;
        
        //If list has nodes
        if (head != null) {
            address = head;
            
            //Traversing untill list has element
            while (address != null) {
                System.out.print("\nDeleting the node " + address.data + "...");
                
                //Calling function to delete first node
                head = delete_beg(address);
                address = head;
            }
        } else {
            System.out.println("No node to delete!!");
        }
    }
    
    //Method to delete node from beginning
    private Node delete_beg(Node address) {
        
        //No node is available to delete
        if(head==null){
            System.out.println("Link list is empty..No node to delete!!");
        }else{
            
            //Assigning the address first node to temporary variable
            address=head;
            head=address.next;
        }
        return head;
    }
}
Input:
Enter number of node you want to enter : 
5
Enter value of 1 node : 
2
Enter value of 2 node : 
7
Enter value of 3 node : 
1
Enter value of 4 node : 
9
Enter value of 5 node : 
3
The nodes of lists are : 
2 7 1 9 3 

Output:
Deleting the node 2...
Deleting the node 7...
Deleting the node 1...
Deleting the node 9...
Deleting the node 3...
After deletion of all nodes - 
The list is empty

Related programs

1) Program To Create And Display Singly Linked List
2) Program To Add A Node In Beginning Of Singly Linked List
3) Program To Add A Node In The End Of Singly Linked List
4) Program To Add A Node Before A Given Node Singly Linked List
5) Program To Add A Node After A Given Node Singly Linked List
6) Program To Delete Node From Beginning Singly Linked List
7) Program To Delete Node From End Singly Linked List
8) Program To Delete Given Node Singly Linked List
9) Program To Delete Node Before Given Node Singly Linked List
10) Program To Delete Node After Given Node Singly Linked List
11) Program To Sort Elements Of Singly Linked List
12) Program To Reverse Elements Singly Linked List
13) Program to Find Maximum And Minimum Value Node In Singly Linked List
14) Program to Search an Element In Singly Linked List
15) Program To Remove Duplicate Elements From Singly Linked List
16) Program To Create And Display Circular Singly Linked List
17) Program To Add A Node At The Beginning Of Circular Linked List
Share Me

Leave a Reply