m2skills

simple LL java

Apr 18th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.25 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. class ListElement{
  5.  
  6.     public int data;
  7.     public ListElement link;
  8.     public ListElement list1,list2;
  9.  
  10.     Scanner sc = new Scanner(System.in);
  11.     //construtor
  12.     ListElement(int data){
  13.         this.data = data;
  14.         this.link = null;
  15.     }
  16.  
  17.     //method to update data in List element
  18.     void updatedata(int data){
  19.         this.data = data;
  20.     }
  21.  
  22.     //method to setup the Link
  23.     void setLink(ListElement node){
  24.         this.link = node;
  25.     }
  26.  
  27.     int getdata(){
  28.         return this.data;
  29.     }
  30.  
  31.     ListElement getNextNode(){
  32.         return this.link;
  33.     }
  34.  
  35.     ListElement insert(ListElement head,ListElement node,int position){
  36.  
  37.         ListElement previousNode = new ListElement(head.data);
  38.         previousNode.setLink(head.link);
  39.  
  40.         if(head == null){
  41.             return node;
  42.         }
  43.         else{
  44.             int size = length();
  45.             if(position > size+1){
  46.                 System.out.println("Invalid position.");
  47.                 System.out.println("Valid positions are from 0 to " + size);
  48.                 return head;
  49.             }
  50.             if(position == 1){
  51.                 node.setLink(head);
  52.                 return node;
  53.             }
  54.             else if(position == 2){
  55.                 ListElement temp = new ListElement(-11);
  56.                 temp.setLink(head.link);
  57.                 head.setLink(node);
  58.                 node.setLink(temp.getNextNode());
  59.             }
  60.             else{
  61.                 int pos = 1;
  62.                 while(pos != position-1){
  63.                     previousNode = previousNode.getNextNode();
  64.                     pos++;
  65.                 }
  66.                 //ListElement temp = head.getNextNode();
  67.  
  68.                 previousNode.setLink(node);
  69.                 node.setLink(null);
  70.             }
  71.         }
  72.         return head;
  73.     }
  74.  
  75.     ListElement insertAtEnd(ListElement node){
  76.         ListElement head = this;
  77.         int size = head.length();
  78.  
  79.         ListElement temp = this;
  80.         while(temp.link!=null){
  81.             temp = temp.getNextNode();
  82.         }
  83.         temp.setLink(node);
  84.         return head;
  85.     }
  86.  
  87.     void display(ListElement head){
  88.         ListElement node = head;
  89.         int position = 1;
  90.         while(node!= null){
  91.             System.out.println("Position " + position + " = " + node.data);
  92.             position++;
  93.             node = node.getNextNode();
  94.         }
  95.     }
  96.  
  97.     ListElement makeDummyList() {
  98.         ListElement head = this;
  99.         for(int i=2 ; i < 10 ; i++){
  100.             ListElement node = new ListElement(i);
  101.             head.insert(head,node,i);
  102.         }
  103.         return head;
  104.     }
  105.  
  106. }
  107. public class LinkedList {
  108.  
  109.  
  110.  
  111.     public static void main(String args[]){
  112.         Scanner sc = new Scanner(System.in);
  113.         ListElement head = new ListElement(1);
  114.         head.setLink(null);
  115.         int cont =1;
  116.         head = head.makeDummyList();
  117.         while(cont == 1){
  118.             System.out.println("Program to implement linked list");
  119.             System.out.println("The Following choices are availiable : ");
  120.             System.out.println("1.Insert element in Linked List");
  121.             System.out.println("2.Delete element from Linked List");
  122.             System.out.println("3.Display Linked List");
  123.  
  124.             System.out.println("0.Exit");
  125.             System.out.print("Enter Your choice :");
  126.  
  127.             int choice = sc.nextInt();
  128.  
  129.             switch(choice){
  130.  
  131.                 case 1:
  132.                     System.out.print("Enter data : ");
  133.                     int data = sc.nextInt();
  134.                     System.out.print("Enter position : ");
  135.                     int position = sc.nextInt();
  136.  
  137.                     ListElement node = new ListElement(data);
  138.                     if(head == null){
  139.                         head = node;
  140.                     }
  141.                     else{
  142.                         int length = head.length();
  143.                         head = head.insert(head,node,position);
  144.                     }
  145.                     break;
  146.  
  147.                 case 2:
  148.                     System.out.println("There are two choices :");
  149.                     System.out.println("1.Delete by data");
  150.                     System.out.println("2.Delete by position");
  151.                     System.out.print("Enter Your choice :");
  152.                     int choices = sc.nextInt();
  153.                     switch (choices){
  154.                         case 1:
  155.                             System.out.print("Enter the data to be deleted : ");
  156.                             int deleteData = sc.nextInt();
  157.                             head = head.deleteByData(head,deleteData);
  158.                             break;
  159.  
  160.                         case 2:
  161.                             System.out.print("Enter position of element to be deleted : ");
  162.                             int pos = sc.nextInt();
  163.                             head = head.deleteByPosition(head, pos);
  164.                             break;
  165.                     }
  166.                     break;
  167.  
  168.                 case 3:
  169.                     head.display(head);
  170.                     break;
  171.  
  172.                 case 0:
  173.                         cont = 0;
  174.                         break;
  175.  
  176.             }
  177.         }
  178.     }
  179.  
  180. }
Add Comment
Please, Sign In to add comment