TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program To Delete A New Node From The Middle Of The Singly Linked List

Program To Delete A New Node From The Middle Of The Singly Linked List on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Program to delete a new node from the middle of the singly linked list

Explanation

In this program, we will create a singly linked list and delete a node from the middle of the list. To accomplish this task, we will calculate the size of the list and then divide it by 2 to get the mid-point of the list. Node temp will point to head node. We will iterate through the list till mid?point is reached. Now, the temp will point to middle node and node current will point to node previous to temp. We delete the middle node such that current's next node will point to temp's next node.

Program to delete a new node from the middle of the singly linked list

Consider the above example, mid-point of the above list is 2. Iterate temp from head to mid-point. Now, temp is pointing to mid node which needs to be deleted. In this case, Node is the middle node which needs to be deleted. The node can be deleted by making node 2's next (current) to point to node 3 (temp's next node). Set temp to null.

Algorithm

  1. Create a class Node which has two attributes: data and next. Next is a pointer to the next node in the list.
  2. Create another class DeleteMid which has three attributes: head, tail, and size which keep tracks of the number of nodes present in the list.
  3. addNode() will add a new node to the list:
    1. Create a new node.
    2. It first checks, whether the head is equal to null which means the list is empty.
    3. If the list is empty, both head and tail will point to the newly added node.
    4. If the list is not empty, the new node will be added to end of the list such that tail's next will point to the newly added node. This new node will become the new tail of the list.
  4. deleteFromMid() will delete a node from the middle of the list:
    1. It first checks whether the head is null (empty list) then, display the message "List is empty" and return.
    2. If the list is not empty, it will check whether the list has only one node.
    3. If the list has only one node, it will set both head and tail to null.
    4. If the list has more than one node then, calculate the size of the list and divide it by 2 to get the mid-point of the list.
    5. Declare a node temp which will point to head and node current will point to node previous to temp.
    6. Traverse through the list till temp points to a middle node. If current not point to null then, delete the middle node(temp) by making current's next to point to temp's next. Else, both head and tail will point to node next to temp and delete the middle node by setting the temp to null.
  5. display() will display the nodes present in the list:
    1. Define a node current which will initially point to the head of the list.
    2. Traverse through the list till current points to null.
    3. Display each node by making current to point to node next to it in each iteration.

Solution

Python

#Represent a node of the singly linked list
class Node:
    def __init__(self,data):
        self.data = data;
        self.next = None;
        
class DeleteMid:
    #Represent the head and tail of the singly linked list
    def __init__(self):
        self.head = None;
        self.tail = None;
        self.size = 0;
        
    #addNode() will add a new node to the list
    def addNode(self, data):
        #Create a new node
        newNode = Node(data);
        
        #Checks if the list is empty
        if(self.head == None):
            #If list is empty, both head and tail will point to new node
            self.head = newNode;
            self.tail = newNode;
        else:
            #newNode will be added after tail such that tail's next will point to newNode
            self.tail.next = newNode;
            #newNode will become new tail of the list
            self.tail = newNode;
        #Size will count the number of nodes present in the list
        self.size = self.size + 1;
            
    #deleteFromMid() will delete a node from the middle of the list
    def deleteFromMid(self):
        #Checks if list is empty
        if(self.head == None):
            print("List is empty");
            return;
        else:
            #Store the mid position of the list
            count = (self.size//2) if(self.size % 2 == 0)  else((self.size+1)//2);
            
            #Checks whether head is equal to tail or not, if yes then list has only one node.
            if( self.head != self.tail ):
                #Initially, temp will point to head
                temp = self.head;
                current = None;
                
                #Current will point to node previous to
                #If temp is pointing to node 2 then current will point to node 1.
                for i in range(0, count-1):
                    current = temp;
                    temp = temp.next;
                    
                if(current != None):
                    #temp is the middle that needs to be removed.
                    #So, current node will point to node next to temp by skipping temp.
                    current.next = temp.next;
                    #Delete temp
                    temp = None;
                #If current points to None then, head and tail will point to node next to temp.
                else:
                    self.head = self.tail = temp.next;
                    #Delete temp
                    temp = None;
            #If the list contains only one element
            #then it will remove it and both head and tail will point to None
            else:
                self.head = self.tail = None;
        self.size = self.size - 1;
                
    #display() will display all the nodes present in the list
    def display(self):
        #Node current will point to head
        current = self.head;
        if(self.head == None):
            print("List is empty");
            return;
        while(current != None):
            #Prints each node by incrementing pointer
            print(current.data),
            current = current.next;

sList = DeleteMid();
 
#Adds data to the list
sList.addNode(1);
sList.addNode(2);
sList.addNode(3);
sList.addNode(4);
 
#Printing original list
print("Original List: ");
sList.display();
 
while(sList.head != None):
    sList.deleteFromMid();
    #Printing updated list
    print("Updated List: ");
    sList.display();

Output:

Original List: 
1 2 3 4 
Updated List: 
1 3 4 
Updated List: 
1 4 
Updated List: 
4 
Updated List: 
List is empty

C

#include <stdio.h>
 
//Represent a node of the singly linked list
struct node{
    int data;
    struct node *next;
};    
 
int size;
//Represent the head and tail of the singly linked list
struct node *head, *tail = NULL;
 
//addNode() will add a new node to the list
void addNode(int data) {
    //Create a new node
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = data;
    newNode->next = NULL;
    
    //Checks if the list is empty
    if(head == NULL) {
        //If list is empty, both head and tail will point to new node
        head = newNode;
        tail = newNode;
    }
    else {
        //newNode will be added after tail such that tail's next will point to newNode
        tail->next = newNode;
        //newNode will become new tail of the list
        tail = newNode;
    }
    //Size will count the number of nodes present in the list
    size++;
}
 
//deleteFromMid() will delete a node from the middle of the list
void deleteFromMid() {
    struct node *temp, *current;
    
    //Checks if list is empty
    if(head == NULL) {
        printf("List is empty \n");
        return;
    }
    else {
        //Store the mid position of the list
        int count = (size % 2 == 0) ? (size/2) : ((size+1)/2);
        
        //Checks whether the head is equal to the tail or not, if yes then the list has only one node.
        if( head != tail ) {
            //Initially, temp will point to head
            temp = head;
            current = NULL;
            
            //Current will point to node previous to temp
            //If temp is pointing to node 2 then current will point to node 1.
            for(int i = 0; i < count-1; i++){
                current = temp;
                temp = temp->next;
            }                
            if(current != NULL) {
                //temp is the middle that needs to be removed.
                //So, current node will point to node next to temp by skipping temp.
                current->next = temp->next;
                //Delete temp
                temp = NULL;
            }
            //If current points to NULL then, head and tail will point to node next to temp.
            else {
                head = tail = temp->next;
                //Delete temp
                temp = NULL;
            }    
        }
        //If the list contains only one element 
        //then it will remove it and both head and tail will point to NULL
        else {
            head = tail = NULL;
        }
    }
    size--;
}
    
//display() will display all the nodes present in the list
void display() {
    //Node current will point to head
    struct node *current = head;
    if(head == NULL) {
        printf("List is empty\n");
        return;
    }
    while(current != NULL) {
        //Prints each node by incrementing pointer
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}
    
int main()
{
    //Adds data to the list
    addNode(1);
    addNode(2);
    addNode(3);
    addNode(4);
    
    //Printing original list
    printf("Original List: \n");
    display();
    
    while(head != NULL) {
        deleteFromMid();
        //Printing updated list
        printf("Updated List: \n");
        display();
    }
        
    return 0;
}

Output:

Original List: 
1 2 3 4 
Updated List: 
1 3 4 
Updated List: 
1 4 
Updated List: 
4 
Updated List: 
List is empty

JAVA

#include <stdio.h>
 
//Represent a node of the singly linked list
struct node{
    int data;
    struct node *next;
};    
 
int size;
//Represent the head and tail of the singly linked list
struct node *head, *tail = NULL;
 
//addNode() will add a new node to the list
void addNode(int data) {
    //Create a new node
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = data;
    newNode->next = NULL;
    
    //Checks if the list is empty
    if(head == NULL) {
        //If list is empty, both head and tail will point to new node
        head = newNode;
        tail = newNode;
    }
    else {
        //newNode will be added after tail such that tail's next will point to newNode
        tail->next = newNode;
        //newNode will become new tail of the list
        tail = newNode;
    }
    //Size will count the number of nodes present in the list
    size++;
}
 
//deleteFromMid() will delete a node from the middle of the list
void deleteFromMid() {
    struct node *temp, *current;
    
    //Checks if list is empty
    if(head == NULL) {
        printf("List is empty \n");
        return;
    }
    else {
        //Store the mid position of the list
        int count = (size % 2 == 0) ? (size/2) : ((size+1)/2);
        
        //Checks whether the head is equal to the tail or not, if yes then the list has only one node.
        if( head != tail ) {
            //Initially, temp will point to head
            temp = head;
            current = NULL;
            
            //Current will point to node previous to temp
            //If temp is pointing to node 2 then current will point to node 1.
            for(int i = 0; i < count-1; i++){
                current = temp;
                temp = temp->next;
            }                
            if(current != NULL) {
                //temp is the middle that needs to be removed.
                //So, current node will point to node next to temp by skipping temp.
                current->next = temp->next;
                //Delete temp
                temp = NULL;
            }
            //If current points to NULL then, head and tail will point to node next to temp.
            else {
                head = tail = temp->next;
                //Delete temp
                temp = NULL;
            }    
        }
        //If the list contains only one element 
        //then it will remove it and both head and tail will point to NULL
        else {
            head = tail = NULL;
        }
    }
    size--;
}
    
//display() will display all the nodes present in the list
void display() {
    //Node current will point to head
    struct node *current = head;
    if(head == NULL) {
        printf("List is empty\n");
        return;
    }
    while(current != NULL) {
        //Prints each node by incrementing pointer
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}
    
int main()
{
    //Adds data to the list
    addNode(1);
    addNode(2);
    addNode(3);
    addNode(4);
    
    //Printing original list
    printf("Original List: \n");
    display();
    
    while(head != NULL) {
        deleteFromMid();
        //Printing updated list
        printf("Updated List: \n");
        display();
    }
        
    return 0;
}

Output:

Original List: 
1 2 3 4 
Updated List: 
1 3 4 
Updated List: 
1 4 
Updated List: 
4 
Updated List: 
List is empty

C#

using System;
                    
public class CreateList
{
    //Represent a node of the singly linked list
    public class Node<T>{
        public T data;
        public Node<T> next;
        
        public Node(T value) {
            data = value;
            next = null;
        }
    }
        
    public class DeleteMid<T>{
        public int size;
        //Represent the head and tail of the singly linked list
        public Node<T> head = null;             
         public Node<T> tail = null;
    
        //addNode() will add a new node to the list
        public void addNode(T data) {
            //Create a new node
            Node<T> newNode = new Node<T>(data);
 
            //Checks if the list is empty
            if(head == null) {
                //If list is empty, both head and tail will point to new node
                head = newNode;
                tail = newNode;
            }
            else {
                //newNode will be added after tail such that tail's next will point to newNode
                tail.next = newNode;
                //newNode will become new tail of the list
                tail = newNode;
            }
            //Size will count the number of nodes present in the list
            size++;
        }
    
        //deleteFromMid() will delete a node from the middle of the list
        public void deleteFromMid() {
            Node<T> temp, current;
 
            //Checks if list is empty
            if(head == null) {
                Console.WriteLine("List is empty");
                return;
            }
            else {
                //Store the mid position of the list
                int count = (size % 2 == 0) ? (size/2) : ((size+1)/2);
 
                //Checks whether the head is equal to the tail or not, if yes then the list has only one node.
                if( head != tail ) {
                    //Initially, temp will point to head
                    temp = head;
                    current = null;
 
                    //Current will point to node previous to temp
                    //If temp is pointing to node 2 then current will point to node 1.
                    for(int i = 0; i < count-1; i++){
                        current = temp;
                        temp = temp.next;
                    }                
                    if(current != null) {
                        //temp is the middle that needs to be removed.
                        //So, current node will point to node next to temp by skipping temp.
                        current.next = temp.next;
                        //Delete temp
                      temp = null;
                    }
                    //If current points to null then, head and tail will point to node next to temp.
                    else {
                        head = tail = temp.next;
                        //Delete temp
                        temp = null;
                    }    
                }
                //If the list contains only one element 
                //then it will remove it and both head and tail will point to null
                else {
                    head = tail = null;
                }
            }
            size--;
        }
        
        //display() will display all the nodes present in the list
        public void display() {
            //Node current will point to head
            Node<T> current = head;
            if(head == null) {
                Console.WriteLine("List is empty");
                return;
            }
            while(current != null) {
                //Prints each node by incrementing pointer
                Console.Write(current.data + " ");
                current = current.next;
            }
            Console.WriteLine();
        }
    }
    
    public static void Main()
    {
        DeleteMid<int> sList = new DeleteMid<int>();
        
        //Adds data to the list
        sList.addNode(1);
        sList.addNode(2);
        sList.addNode(3);
        sList.addNode(4);
        
        //Printing original list
        Console.WriteLine("Original List: ");
        sList.display();
        
        while(sList.head != null) {
            sList.deleteFromMid();
            //Printing updated list
            Console.WriteLine("Updated List: ");
            sList.display();
        }
    }
}

Output:

Original List: 
1 2 3 4 
Updated List: 
1 3 4 
Updated List: 
1 4 
Updated List: 
4 
Updated List: 
List is empty

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//Represent a node of singly linked list
class Node{
    public $data;
    public $next;
    
    function __construct($data){
        $this->data = $data;
        $this->next = NULL;
    }
}
class DeleteMid{
    //Represent the head and tail of the singly linked list
    public $head;
    public $tail;
    function __construct(){
        $this->head = NULL;
        $this->tail = NULL;
        $this->size = 0;
    }
    
    //addNode() will add a new node to the list
    function addNode($data) {
        //Create a new node
        $newNode = new Node($data);
        
        //Checks if the list is empty
        if($this->head == null) {
            //If list is empty, both head and tail will point to new node
            $this->head = $newNode;
            $this->tail = $newNode;
        }
        else {
            //newNode will be added after tail such that tail's next will point to newNode
            $this->tail->next = $newNode;
            //newNode will become new tail of the list
            $this->tail = $newNode;
        }
        //Size will count the number of nodes present in the list
        $this->size++;
    }
    
    //deleteFromMid() will delete a node from the middle of the list
    function deleteFromMid() {
        
        //Checks if list is empty
        if($this->head == null) {
            print("List is empty <br>");
            return;
        }
        else {
            //Store the mid position of the list
            $count = ($this->size % 2 == 0) ? ($this->size/2) : (($this->size+1)/2);
            
            //Checks whether head is equal to tail or not, if yes then list has only one node.
            if( $this->head != $this->tail ) {
                //Initially, temp will point to head
                $temp = $this->head;
                $current = null;
                
                //Current will point to node previous to temp
                //If temp is pointing to node 2 then current will point to node 1.
                for($i = 0; $i < $count-1; $i++){
                    $current = $temp;
                    $temp = $temp->next;
                }                
                if($current != null) {
                    //temp is the middle that needs to be removed.
                    //So, current node will point to node next to temp by skipping temp.
                    $current->next = $temp->next;
                    //Delete temp
                    $temp = null;
                }
                //If current points to null then, head and tail will point to node next to temp.
                else {
                    $this->head = $this->tail = $temp->next;
                    //Delete temp
                    $temp = null;
                }    
            }
            //If the list contains only one element 
            //then it will remove it and both head and tail will point to null
            else {
                $this->head = $this->tail = null;
            }
        }
        $this->size--;
    }
    
    //display() will display all the nodes present in the list
    function display() {
        //Node current will point to head
        $current = $this->head;
        
        if($this->head == NULL) {
            print("List is empty <br>");
            return;
        }
        while($current != NULL) {
            //Prints each node by incrementing pointer
            print($current->data . " ");
            $current = $current->next;
        }
        print("<br>");
    }
}
 
$sList = new DeleteMid();
        
//Adds data to the list
$sList->addNode(1);
$sList->addNode(2);
$sList->addNode(3);
$sList->addNode(4);
 
//Printing original list
print("Original List: <br>");
$sList->display();
 
while($sList->head != null) {
    $sList->deleteFromMid();
    //Printing updated list
    print("Updated List: <br>");
    $sList->display();
}
?>
</body>
</html>

Output:

Original List: 
1 2 3 4 
Updated List: 
1 3 4 
Updated List: 
1 4 
Updated List: 
4 
Updated List: 
List is empty

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf