Header Ads

Java Program to implement Singly Liked List

Problem Statement:-

Write a Java program which will demonstrate a concept of Interfaces and packages: In this assignment design and use of customized interfaces and packages for a specific application are expected.



Code:-

CommonList.java


package common;
//Add this java class file in folder named "common"
public interface CommonList {

 void add(int x);
 void display();
 boolean isEmpty();
 int size();
 boolean delete(int x);
}


CommonList.java
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package specificlist;
//Add this file in folder named "specificlist"
import java.util.Scanner;

import common.CommonList;

public class Node {

 public int data;
 public Node link;
 
 public Node()
 {
  link=null;
  data=0;
  
 }
 public Node(int d,Node l)
 {
  data=d;
  link=l;
 }
}

public class SLL implements CommonList {

 Node start;
 Node end;
 int size;
 
 public SLL()
 {
  start=end=null;
  size=0;
 }
 
 public void add(int x) {
  
  Node p=new Node(x,null);
  size++;
  
  if(isEmpty())
  {
   start=p;
   end=p;
  }
  else
  {
   end.link=p;
   end=p;
  }
  
 }

 public void display() {
  System.out.println("\nSingly Linked List:\n");
  if(isEmpty())
  {
   System.out.println("\nEmpty. No Data in SLL.");
   return;
  }
  else if(start.link==null)
  {
   System.out.println(start.data);
   return;
  }

  Node ptr=start;
  while(ptr.link!=null)
  {
   System.out.println(ptr.data);
   ptr=ptr.link;
  }
  System.out.println(ptr.data);
 }

 public boolean isEmpty() {
  
  return start==null;
 }

 public int size() {
  
  return size;
 }
 
 public boolean delete(int x)
 {
  if(isEmpty())
  {
   System.out.println("\nUnderflow.");
   return false;
  }
  if(start.data==x )//only one node in list
  {
   start.data=0;
   start=start.link;
   //start=end=null;
   size--;
   return true;
  }
  Node p=start;
  Node par=start;
  while(p.data!=x && p.link!=null)
  {
   par=p;
   p=p.link;
  }
  if(p.data==x && p.link!=null)
  {
   par.link=p.link;
   p.data=0;
   //p.link=null;
   size--;
   return true;
  }
  if(p.data==x && p.link==null)
  {
   par.link=null;
   p.data=0;
   end=par;
   size--;
   return true;
  }
  return false;
  
 }
 //=======================MAIN METHOD ========================
 public static void main(String[] args) {
  SLL s=new SLL();
  int choice;
  @SuppressWarnings("resource")
  Scanner sc=new Scanner(System.in);
  
  do
  {
   System.out.println("\n=====Singly Linked List =========");
   System.out.println("\n1.Insert\n2.Display\n3.Size\n4.Delete\n0.Exit\nEnter Your Choice:");
   choice=sc.nextInt();
   switch(choice)
   {
   case 1:
    System.out.println("\nEnter Element: ");
    s.add(sc.nextInt());
    break;
   case 2:
    s.display();
    break;
   case 3:
    System.out.println("\nTotal Element Count: "+s.size());
    break;
    
   case 4:
    System.out.println("\nEnter Element to delete: ");
    if(s.delete(sc.nextInt()))
    {
     System.out.println("Element Deleted.");
    }
    else
    {
     System.out.println("\nElement Not Found.");
    }
    break;
   default:
    System.out.println("\nWrong Choice..");
   }
   
  }while(choice!=0);
 }

}


Output:-


No comments:

Powered by Blogger.