Program To Delete Node Before Given Node Singly Linked List

Description

To delete a node before given node singly linked list - 
Case 1: check if list is empty 
	Show message that list is empty.
Case 2: If list has only one element
	Show message that no node available before that node to delete.
Case 3: If to delete a node before second node
	Step 1: Delete first node
	Step 2: Make second node as first node
	Step 3: Free the memory occupied by first node
Case 4: If to delete any other node..
	Step 1: Traverse the list to reach that node before which a node to delete
	Step 2: Maintain the two node which contain the address of previous node
	(preAddress) and previous to previous node(tempAddress). 
	Step 3: Assign the address of node before which node to delete to the address
	field of previous to previous node.
	Step 4: Free the memory occupied by node.

Note: Here head contains the address of first node

C/C++

/* C Program To Delete a Node before given node In singly linked list */
//Save it as delete_node_before_given_node_singly_linked_list.c

#include<stdio.h>
#include<malloc.h>
struct node *create(int);
void display(struct node *);
struct node *delete_node_before_given_node(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 a node before a given node
    head=delete_node_before_given_node(head);
    printf("\nThe linked list after deletion of a node before given node - \n");
    
    //Calling function to display list after deletion of a node before given node
    display(head);

    return 0;
}

struct node *create(int n){
    int i;
    struct node *head=NULL;
    struct node *ptr,*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*/
            ptr=head;
            while(ptr->next!=NULL){
                ptr=ptr->next;
            }
            ptr->next=newNode;
        }
    }
    return head;
}

void display(struct node *head){
    struct node *ptr;
    
    //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*/
        ptr=head;
        while(ptr!=NULL){
            printf("The node value = %d and their address = %u\n",ptr->data,ptr);
            ptr=ptr->next;
        }
    }
}

//Function to delete a node before a given node
struct node *delete_node_before_given_node(struct node *head){
    int val;
    struct node *address, *preAddress, *tempAddress;

    printf("\nEnter the value before which node to delete : ");
    scanf("%d",&val);
    
    //If list is empty, show message
    if(head==NULL){
        printf("\nNo node available to delete!!");
    }
    
    //If list has only one node
    else if(head->next==NULL){
        printf("No any previous node available to delete!!");
    }
    
    //If list has more than one elements
    else{
        address=head;
        tempAddress=head;
        preAddress=head;
        
        //Traverse the list to reach the given node
        while(address->data!=val){
            tempAddress=preAddress;
            preAddress=address;
            address=address->next;
        }
        
        //If need to delete node before second node means to delete first node
        if(address==head->next){
            head=address;
            free(preAddress);
        }
        
        //If trying to delete first node.
        else if(address==head){
            printf("No any previous node available to delete!!");
        }
        
        //For rest of the condition
        else{
            tempAddress->next=address;
            free(preAddress);
        }
    }
    return head;
}
Input:
Enter number of nodes : 4
Enter the value of 1 node : 2
Enter the value of 2 node : 5
Enter the value of 3 node : 3
Enter the value of 4 node : 7
The node value = 2 and their address = 9899360
The node value = 5 and their address = 9899392
The node value = 3 and their address = 9899408
The node value = 7 and their address = 9899424

Enter the value before which node to delete : 7

Output:
The linked list after deletion of a node before given node -
The node value = 2 and their address = 9899360
The node value = 5 and their address = 9899392
The node value = 7 and their address = 9899424

Java

/* Java Program To Delete a Node before given node In singly linked list */
//Save it as DeleteNodeBeforeGivenNodeSinglyLinkedList.java

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

public class DeleteNodeBeforeGivenNodeSinglyLinkedList {
    
    // 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);

        DeleteNodeBeforeGivenNodeSinglyLinkedList list = new DeleteNodeBeforeGivenNodeSinglyLinkedList();

        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();

        System.out.println("\nEnter the value before which node to delete : ");
        int val = scanner.nextInt();

        list.delete_node_before_given_node(val);
        
        // Calling function to display list after deletion of a node before given node
        System.out.println("\nAfter deletion of a node before given node - ");
        list.display();
    }

    private void delete_node_before_given_node(int val) {
        
        Node address, preAddress, tempAddress;
        
        //If list is empty, show message
        if(head==null) {
            System.out.println("No node available to delete!!");
        }
        
        //If list has only one node
        else if(head.next==null){
            System.out.println("No any previous node available to delete!!");
        }
        
        //If list has more than one elements
        else{
            address=head;
            tempAddress=head;
            preAddress=head;
            
            //Traverse the list to reach the given node
            while(address.data!=val){
                tempAddress=preAddress;
                preAddress=address;
                address=address.next;
            }
            
            //If need to delete node before second node means to delete first node
            if(address==head.next){
                head=address;
            }
            
            //If trying to delete first node.
            else if(address==head){
                System.out.println("No any previous node available to delete!!");
            }
            
            //For rest of the condition
            else{
                tempAddress.next=address;
            }
        }
    }
}
Input:
Enter number of node you want to enter : 
4
Enter value of 1 node : 
2
Enter value of 2 node : 
5
Enter value of 3 node : 
3
Enter value of 4 node : 
7
The nodes of lists are : 
2 5 3 7 
Enter the value before which node to delete : 
5

Output:
After deletion of a node before given node - 
The nodes of lists are : 
5 3 7

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 After Given Node Singly Linked List
10) Program To Delete All Nodes Of 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