Saturday, 10 February 2018

A complete working Java program to demonstrate deletion of node in singly linked list

Delete a Linked List node,Delete a Linked List node at a given position,Delete a Linked List node at a given position in java,delete a node in data structure, how to delete a node,how to delete a node in java, A complete working Java program to demonstrate deletion of node in singly linked list, demonstrate deletion of node,A complete working Java program to demonstrate deletion of node,

Demonstrate Deletion of Node

   In this tutorial We are going to see  A complete working Java program to demonstrate deletion of node in singly linked list.
         if you are getting how it's working then please see once our previous post of this topic..

package com.dashzin.deletion;

public class LinkedList {
   Node head;
 static class Node{
      int data;
      Node next;
     Node(int data){
          this.data=data;
          next=null;
     }
 }

 /* Given a key, deletes the first occurrence of key in linked list */
 void deleteNode(int key){
      Node temp=head, prev=null;
     // If head node itself holds the key to be deleted
      if (temp != null && temp.data == key)
     {
         head = temp.next; // Changed head
         return;
     }
     // Search for the key to be deleted,
      while (temp != null && temp.data != key)
     {
         prev = temp;
         temp = temp.next;
     }
     // If key was not present in linked list
     if (temp== null) return;
  // Unlink the node from linked list
     prev.next = temp.next;
 }


 /* Inserts a new Node at front of the list. */
 public void insert(int new_data){
      Node new_node=new Node(new_data);
      new_node.next=head;
      head=new_node;
 }


 /* This function prints contents of linked list starting from
 the given node */
public void printList()
{
 Node tnode = head;
 while (tnode != null)
 {
     System.out.print(tnode.data+" ");
     tnode = tnode.next;
 }
}

 public static void main(String args[]){
      LinkedList lkl=new LinkedList();
      lkl.insert(5);
      lkl.insert(10);
      lkl.insert(20);
      lkl.insert(100);
      lkl.insert(30);
      
      lkl.printList();
      
      lkl.deleteNode(10);
      System.out.println(".................");
      
      lkl.printList();
 }
}
if you are getting how it's working then please see once our previous post of this topic..







DashZin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment