KEMBAR78
Comp | PDF | Queue (Abstract Data Type) | Software Development
0% found this document useful (0 votes)
10 views3 pages

Comp

Uploaded by

senspandan2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Comp

Uploaded by

senspandan2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Question:- - A class CirQueue is declared to add an elements at the rear and remove an element

from the front. The description of the class is as follows:


Class name: CirQueue
Data members :
cap : to store capacity of the array
front,rear : to store index of the first and last element in the array
Member methods:-
CirQueue(int max): parameterised constructor to assign cap=max, front=rear=0 and allocate
memory.
void push(int v): add the element v if it is possible, otherwise display the message” QUEUE IS FULL”
int pop(): return the element in front if possible, otherwise return -9999
void show(): display the elements in the list.
Write a main() which displays the menu
1. PUSH
2. POP
3. DISPLAY
Depending on the choice the program will continue.

Code:-
import java.util.Scanner;

class CirQueue
{
int cq[];
int cap; // Keeps track of available capacity
int front;
int rear;

public CirQueue(int max)


{
cap = max; // Initialize capacity with the size of the queue
cq = new int[cap];
front = 0;
rear = 0;
}
public boolean isFull()
{
return cap == 0;
}
public boolean isEmpty()
{
return cap == cq.length;
}
public void push(int v)
{
if (isFull())
{
System.out.println("QUEUE IS FULL");
} else
{
cq[rear] = v;
rear = (rear + 1) % cq.length; // Move rear circularly
cap--; // Reduce available capacity
}
}
public int pop()
{
if (isEmpty())
{
return -9999; // Special value to indicate empty queue
} else
{
int v = cq[front];
front = (front + 1) % cq.length; // Move front circularly
cap++; // Increase available capacity
return v;
}
}
public void show()
{
if (isEmpty())
{
System.out.println("QUEUE IS EMPTY");
} else {
int count = cq.length - cap; // Number of elements in the queue
int i = front;
while (count > 0)
{
System.out.print(cq[i] + " ");
i = (i + 1) % cq.length; // Move circularly
count--;
}
System.out.println();
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Circular queue size: ");
int size = in.nextInt();
CirQueue obj = new CirQueue(size);

while (true)
{
System.out.println("\n1. PUSH");
System.out.println("2. POP");
System.out.println("3. DISPLAY");
System.out.println("4. EXIT");
System.out.print("Enter your choice: ");
int choice = in.nextInt();

switch (choice)
{
case 1:
System.out.print("Element to be pushed: ");
int d = in.nextInt();
obj.push(d);
break;

case 2:
d = obj.pop();
if (d == -9999)
{
System.out.println("QUEUE IS EMPTY");
} else
{
System.out.println(d + " POPPED");
}
break;

case 3:
obj.show();
break;

case 4:
System.out.println("Bye!");
in.close();
return;

default:
System.out.println("Invalid choice! Try again.");
}
}
}
}

Variable Description Table(VDT):-


Variable name Data Type Purpose
cq int[] Stores the elements of the circular queue.
cap int Tracks the remaining capacity of the queue.
front int Indicates the index of the front element in the queue for
pop operations.
rear int Indicates the index where the next element will be
added in the queue.
size int Stores the size of the circular queue entered by the
user.
choice int Stores the user’s menu option.
d int Temporarily stores the element to be pushed or the
popped element.

You might also like