0% found this document useful (0 votes)
4 views6 pages

Question 23

The document defines a Queue class that can hold a maximum of 100 integers, allowing users to add integers from the rear and remove them from the front. It specifies the constructor and methods for adding (enqueue) and removing (dequeue) integers, along with checks for overflow and underflow conditions. Additionally, it includes methods to check if the queue is full or empty and to display the queue elements.

Uploaded by

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

Question 23

The document defines a Queue class that can hold a maximum of 100 integers, allowing users to add integers from the rear and remove them from the front. It specifies the constructor and methods for adding (enqueue) and removing (dequeue) integers, along with checks for overflow and underflow conditions. Additionally, it includes methods to check if the queue is full or empty and to display the queue elements.

Uploaded by

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

Question 23

Queue is an entity which can hold a maximum of 100 integers. The queue
enables the user to add integers from the rear and remove integers from the
front.
Define a class Queue with following details:
Class name: Queue
Data Members / instance variables:
Que[]: Array to hold the integer elements.
size : Stores the size of the array.
front: To point the index of the front.
rear: To point the index of the rear.
Member functions/methods:
Queue (int mm) : Constructor to initialize the data. size = mm, front = 0, rear =
0.
void addele (int v) : to add integers from the rear if possible else display the
message 'overflow".
int delele () : returns elements from front if possible, otherwise display the
message "Underflow" and return -9999.
void display (): displays the array elements.
Specify the class Queue giving details of ONLY the functions void addele (int)
and int delele ().
Assume that the other functions have been defined.
The main function and algorithm need not be written.
Program
class Queue
{
int[] queue;
int size;
int front;
int rear;

public Queue(int size)


{
this.size = size;
queue = new int[size];
front = rear = -1; // Initialize front and rear to -1 to indicate an empty
queue
}

public void enqueue(int value)


{
if (isFull())
{
System.out.println("Queue Overflow");
}
else
{
if (isEmpty())
{
front = rear = 0; // If queue is empty, set both front and rear to 0
}
else
{
rear = (rear + 1) % size; // Increment rear circularly
}
queue[rear] = value;
}
}

public int dequeue()


{
if (isEmpty())
{
System.out.println("Queue Underflow");
return -1;
}
else
{
int value = queue[front];
if (front == rear)
{
front = rear = -1; // If one element, reset front and rear
}
else
{
front = (front + 1) % size; // Increment front circularly
}
return value;
}
}

public boolean isFull()


{
return (rear + 1) % size == front; // Check if rear is one position behind
front circularly
}

public boolean isEmpty()


{
return front == -1; // Check if front is -1, indicating an empty queue
}

public void display()


{
if (isEmpty()) {
System.out.println("Queue is empty");
}
else
{
int i = front;
do {
System.out.print(queue[i] + " ");
i = (i + 1) % size; // Iterate through the queue circularly
}
while (i != rear + 1);
System.out.println();
}
}
}

Variable Description
Variable Data
Description
Name Type
queue Array to store the elements of the queue int[]
size Maximum capacity of the queue int
front Index of the front element int
rear Index of the rear element int
value Temporary variable to store values during enqueue int
and dequeue operations
i Loop counter for iterating through the queue int

Algorithm

1. Start.
2. Define the Queue class with the following instance variables
3. Implement the constructor to initialize the queue array and set front
and rear to -1.
4. Implement the enqueue method
5. Implement the dequeue method
6. Implement the isFull method
7. Implement the isEmpty method
8. Implement the display method.
9. Stop.

Output

You might also like