DSA :Queue implementation using Linked List
package javaapplication7;
import java.io.*;
import java.lang.*;
class node
{ int data;
node next;
node(int n)throws Exception
{
next=null;
data=n;
}
void display()throws Exception
{
System.out.println(data);
}
}
class Qu
{
node head=null,tail=null;
void enQ(int dd)throws Exception
{
node n1=new node(dd);
if(head==null)
{
head=n1;
tail=n1;
}
else
{
tail.next=n1;
tail=n1;
}
}
node dq()throws Exception
{
node item;
if(head==null)
{
item=null;
System.out.println("Q IS EMPTY");
}
else
{
item=head;
head=head.next;
}
return item;
}
void display()throws Exception
{
System.out.println("Q IS...");
node current=head;
while(current!=null)
{
System.out.println(current.data);
current=current.next;
}
}
}
class javaapplication7
{
public static void main(String[] args)throws Exception
{
int t;
Qu q=new Qu();
do{
System.out.println(" enter your choice \n1.ENQ\n2. DQ \n 3 display \t");
InputStreamReader isr1=new InputStreamReader(System.in);
BufferedReader br1=new BufferedReader(isr1);
int op=Integer.parseInt(br1.readLine());
switch(op)
{
case 1:System.out.println("enter the element to be EN Q");
InputStreamReader isr2=new InputStreamReader(System.in);
BufferedReader br2=new BufferedReader(isr2);
int j=Integer.parseInt(br2.readLine());
q.enQ(j);
break;
case 2:
node nn= q.dq();
if(nn!=null)
System.out.println(" Element"+nn.data+" is dQ`ed");
break;
case 3:q.display();
break;
default:System.out.println("INVALID OPTION");
}
System.out.println("do you want to continue \n1=>yes \n0=>no");
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
t=Integer.parseInt(br.readLine());
}while(t==1);
}
}
No comments:
Post a Comment