Program : Singly Linked List
import java.io.*; //To implement I/O operations
class link //The class link
{
int data; //data item
link next; //next link in list
link(int id) //constructor
{
data=id; //initialize data
}
void displaylink() //To display the list
{
System.out.println("{"+data+"}");
}
} //end of class link
class linklist //The class linklist
{
link first; //ref to first link on list
linklist() //constructor
{
first=null; //no links on list yet
}
void create(int id) //To insert into the list
{
link l=new link(id); //make new link
l.next=first;
first=l;
} //End of function create
public void delete(int dt) //To delete from the list
{
link ct=first; //point to the beginning of the list
link pre=first; //point to the beginning of the list
if(first==null) //Check if list is empty
System.out.println("EMPTY LIST");
else if (first.data==dt)
first=first.next; //move to next link
else
{
while (ct.data!=dt)
{
pre=ct;
ct=ct.next;
}
pre.next=ct.next;
}
} //End of function delete
public void insert(int el,int da)
{
link ct=first; //point to the beginning of the list
link l=new link(el);
if (first==null) //Check if list is empty
{
first=l;
}
else
{
while (ct.data!=da)
{
ct=ct.next;
}
l.next=ct.next;
ct.next=l;
}
}
public void search(int ele) //Function to search for an element
{
int i=0;
link ct=first; //point to the beginning of the list
while (ct!=null)
{
if(ct.data==ele)
i=1;
ct=ct.next;
}
if(i==1)
System.out.println("THE SEARCHED ITEM IS EXIST IN THE LIST");
else
System.out.println("THE SEARCHED ITEM IS NOT EXIST IN THE LIST");
}
void displaylist() //Function to display the linkedlist
{
link current=first; //point to the beginning of the list
if (current==null) //Check if list is empty
{
System.out.println("EMPTY LIST");
}
while (current!=null)
{
current.displaylink();
current=current.next; //move to next link
}
}
}
class singlylinkedlist //The main class singlylinkedlist
{
//The main function
public static void main(String args[])throws IOException
{
char cc;
int ch=0;
linklist list=new linklist();
try
{
do
{
BufferedReader f=new BufferedReader(new InputStreamReader(System.in));
//Print Menu
System.out.println("1.CREATE THE LIST");
System.out.println("2.DELETE AN ELEMENT");
System.out.println("3.DISPLAY THE LIST");
System.out.println("4.INSERT AN ELEMENT");
System.out.println("5.SEARCH AN ELEMENT");
System.out.println("ENTER YOUR CHOICE");
ch=Integer.parseInt(f.readLine());
//Accepts choice
if (ch==1)
{
System.out.println("ENTER THE NUMBER OF ELEMENTS TO BE INSERTED");
int n=Integer.parseInt(f.readLine());
for(int i=0;i<n;i++)
{
System.out.println("ENTER THE NUMBER");
int x=Integer.parseInt(f.readLine());
list.create(x);
}
}
if (ch==2)
{
System.out.println("ENTER THE NUMBER TO BE DELETED");
int d=Integer.parseInt(f.readLine());
list.delete(d);
}
else if (ch==3)
{
System.out.println("ELEMENTS IN THE LIST ARE");
list.displaylist();
}
else if (ch==4)
{
System.out.println("ENTER THE NUMBER AND POSITION TO BE INSERTED");
int d1=Integer.parseInt(f.readLine());
int d2=Integer.parseInt(f.readLine());
list.insert(d1,d2);
}
if (ch==5)
{
System.out.println("ENTER THE NUMBER TO BE SEARCHED");
int se=Integer.parseInt(f.readLine());
list.search(se);
}
System.out.println();
System.out.println("DO YOU WANT TO CONTINUE(Y/N)");
cc=(char) f.read();
}
while(cc=='y');
}
catch(Exception e) //To catch any exceptions
{
}
} //End of main function
} //End of main class SinglyLinkedlist
0 comments:
Post a Comment