About

Kannur University btech CSE study materials, question papers, syllabus . . .

Thursday, July 4, 2013

S4 - DSA LAB - CIRCULAR QUEUE

   /*         CIRCULAR SQUEUE        */   

import java.io.*;
class Q
{
int front,rear,item,a[],n;
DataInputStream nn=new DataInputStream(System.in);
void read()
{try{
System.out.print("enter the limit : ");
n=Integer.parseInt(nn.readLine());
a=new int[n];
front=0;
rear=0;
item=0;
}
catch(Exception e)
{}}
void EnQ()
{
try
{
if(item==n)
System.out.println("Q is full");
else
{
System.out.print("Enter the number:  ");
int d=Integer.parseInt(nn.readLine());
if(rear==n)
rear=0;
a[rear]=d;
rear++;
item++;
}
}
catch(Exception e)
{}
}
void DeQ()
{
if(item==0)
System.out.print("Q is empty");
else
{
if(front==n)
front=0;
int d=a[front];
front++;
item--;
System.out.print("deleted element is "+ d);
}
}
void display()
{
if(item==0)
System.out.print("no elements ");
else
{
int count=0;
for(int i=front;count<item;i++)
{
System.out.println(a[i%n]);
count++;
}}}
}
class CQ
{
public static void main(String args[])
{
try
{DataInputStream get=new DataInputStream(System.in);
int p;
Q obj=new Q();
obj.read();
do
{
System.out.println("\n1.EnQ\n2.DeQ\n3.Display");
System.out.print("Enter the choice:  ");
int ch=Integer.parseInt(get.readLine());
p=ch;
switch(ch)
{
case 1: obj.EnQ();break;
case 2: obj.DeQ();break;
case 3: obj.display();
}
}while(p<4);
}
catch(Exception e)
{}}
}

OUTPUT


enter the limit : 3

1.EnQ
2.DeQ
3.Display
Enter the choice:  3
no elements
1.EnQ
2.DeQ
3.Display
Enter the choice:  2
Q is empty
1.EnQ
2.DeQ
3.Display
Enter the choice:  1
Enter the number:  1

1.EnQ
2.DeQ
3.Display
Enter the choice:  1
Enter the number:  2

1.EnQ
2.DeQ
3.Display
Enter the choice:  1
Enter the number:  3

1.EnQ
2.DeQ
3.Display
Enter the choice:  1
Q is full

1.EnQ
2.DeQ
3.Display
Enter the choice:  3
1
2
3

1.EnQ
2.DeQ
3.Display
Enter the choice:  2
deleted element is 1
1.EnQ
2.DeQ
3.Display
Enter the choice:  3
2
3

1.EnQ
2.DeQ
3.Display
Enter the choice:  2
deleted element is 2
1.EnQ
2.DeQ
3.Display
Enter the choice:  2
deleted element is 3
1.EnQ
2.DeQ
3.Display
Enter the choice:  2
Q is empty
1.EnQ
2.DeQ
3.Display
Enter the choice:  4



0 comments:

Post a Comment