m2skills

delete node pointer java

Jan 28th, 2018
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Created by MOHIT on 24-01-2018.
  5.  */
  6.  
  7. class node{
  8.  
  9.     public int element;
  10.     public node link;
  11.  
  12.     //constructor that accepts only element
  13.     node(int element) {
  14.         this.element = element;
  15.         this.link = null;
  16.     }
  17.  
  18.     //constructor that accepts both link and element
  19.     node(int element, node link){
  20.         this.element = element;
  21.         this.link = link;
  22.     }
  23.  
  24.     //method to update the element
  25.     void updateData(int element){
  26.         this.element = element;
  27.     }
  28.  
  29.     //method to update or setup link
  30.     void updateLink(node link){
  31.         this.link = link;
  32.     }
  33.  
  34.     //method to get the element from the node
  35.     int getElement(){
  36.         return this.element;
  37.     }
  38.  
  39.     //method to get the next node
  40.     node getNextNode(){
  41.         return this.link;
  42.     }
  43. }
  44.  
  45. class LL {
  46.  
  47.     Scanner sc = new Scanner(System.in);
  48.     node head;
  49.  
  50.     void insertAtStart(int number){
  51.  
  52.         node newNode = new node(number);
  53.  
  54.         if(head == null){
  55.             head = newNode;
  56.         } else {
  57.             newNode.updateLink(head);
  58.         }
  59.         head = newNode;
  60.  
  61.         return;
  62.     }
  63.  
  64.     void insertAtEnd(int element){
  65.         if(head == null){
  66.             this.insertAtStart(element);
  67.             return;
  68.         }
  69.         node current = this.head;
  70.         while(current.getNextNode() != null){
  71.             current = current.getNextNode();
  72.         }
  73.  
  74.         node newNode = new node(element);
  75.         current.updateLink(newNode);
  76.         return;
  77.     }
  78.  
  79.     void display(){
  80.         node current = this.head;
  81.  
  82.         while(current!= null){
  83.             System.out.print(current.getElement());
  84.             if(current.getNextNode() != null){
  85.                 System.out.print("-->");
  86.             }
  87.             current = current.getNextNode();
  88.         }
  89.     }
  90.  
  91.     // deleting node with only that node pointer
  92.     public void deleteNode(node current){
  93.         if(current == null){
  94.             return;
  95.         }else{
  96.             // make a pointer to next node
  97.             node nextNode = current.getNextNode();
  98.  
  99.             // copy deteils of next node to current node
  100.             current.updateData(nextNode.getElement());
  101.             current.updateLink(nextNode.getNextNode());
  102.  
  103.             // delete next node
  104.             nextNode = null;
  105.         }
  106.     }
  107.  
  108. }
  109. public class LinkedList {
  110.     public static void main(String arg[]) {
  111.         System.out.println("Program to Delete a node from the Linked List.");
  112.         LL l1 = new LL();
  113.         l1.insertAtEnd(1);
  114.         l1.insertAtEnd(2);
  115.         l1.insertAtEnd(3);
  116.         l1.insertAtEnd(4);
  117.         l1.insertAtEnd(5);
  118.         l1.insertAtEnd(6);
  119.         l1.insertAtEnd(7);
  120.         l1.insertAtEnd(8);
  121.         l1.display();
  122.  
  123.         System.out.println("\nLinked List after deleting the 3rd node is : ");
  124.         l1.deleteNode(l1.head.getNextNode().getNextNode());
  125.         l1.display();
  126.     }
  127. }
Add Comment
Please, Sign In to add comment