Tweet
import java.io.*;
abstract class que
{
abstract void insert(int val);
abstract void del();
abstract void display();
}
public class cq extends que
{
int a[],front,rear,n;
cq(int cap)
{
n=cap;
front=-1;
rear=-1;
a=new int[n];
}
void insert(int val)
{
if(front==0 && rear==n-1 || rear==front-1)
System.out.println("Queue overflow... \n");
else if(front==-1 && rear==-1)
{
front=0;
a[++rear]=val;
}
else if(rear==n-1)
{
rear=0;
a[rear]=val;
}
else
a[++rear]=val;
}
void del()
{
if(front==-1)
System.out.println("Queue underflow... \n");
else if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==n-1)
front=0;
else front++;
}
void display()
{
int i;
if(front < 0)
System.out.println("Queue underflow...\n");
else if(rear>=front)
{
System.out.println("Queue : ");
for(i=front;i<=rear;i++)
System.out.print(a[i] + " ");
System.out.println();
}
else
{
System.out.println("Queue : ");
for(i=front;i<n;i++)
System.out.print(a[i] + " ");
for(i=0;i<=rear;i++)
System.out.print(a[i] + " ");
}
}
public static void main(String args[])throws IOException
{
int ch,val,cap;
System.out.println("\nProgram to implement circular queue in java : \n");
System.out.println("Enter queue capacity : ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
cap=Integer.parseInt(br.readLine());
cq obj=new cq(cap);
do
{
System.out.print("\n[1] Insert [2] Delete [3] Display [4] Exit \n");
System.out.print("\nEnter your choice : ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter value to insert : ");
val=Integer.parseInt(br.readLine());
obj.insert(val);
obj.display();
break;
case 2:
obj.del();
obj.display();
break;
case 3:
obj.display();
break;
case 4: break;
default : System.out.println("Enter correct choice .... \n");
break;
}
}while(ch!=4);
}
}
Circular Queue in Java using abstract class
Posted by
LAHAUL SETH
~
Circular Queue in Java using abstract class
2012-03-31T12:35:00+05:30
LAHAUL SETH
Java
|
Queue Implementation
|
Comments
Circular Queue in Java using abstract class
2012-03-31T12:35:00+05:30
LAHAUL SETH
Java
|
Queue Implementation
|