0% found this document useful (0 votes)
4 views

Lab Sheet 5- Queue Operations

Uploaded by

zakwan1292003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab Sheet 5- Queue Operations

Uploaded by

zakwan1292003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab Sheet 5: Queue Operations

Lab Objective:
To understand and implement a queue data structure using Java. The aim is to perform
basic queue operations such as Enqueue, Dequeue, Display, and to check whether the queue
is empty or full.

LO1: Construct a queue using an array and implement functions to perform enqueue and
dequeue operations efficiently.

LO2: Apply robust error handling to manage queue overflow and underflow, ensuring the
queue operates reliably under various conditions

Theory:

Queue:
A queue is a linear data structure that follows the First In First Out (FIFO) principle. This
means that the element that is added first is removed first.

Operations in Queue:
1. Enqueue: Add an element to the rear of the queue.
2. Dequeue: Remove and return the element from the front of the queue.
3. isEmpty: Check if the queue is empty.
4. isFull: Check if the queue is full.
5. Display: Display the elements in the queue.

Procedure:

Step 1: Define the Queue Class


Create a queue class that manages the queue using an array. Include the following
operations:
- enqueue(): Add an element to the queue.
- dequeue(): Remove an element from the queue.
- isEmpty(): Check if the queue is empty.
- isFull(): Check if the queue is full.
- display(): Display the elements of the queue.
Step 2: Define the User Interface
Create a user interface using the QueueOperations class, which allows the user to:
- Enter the size of the queue.
- Perform operations like Enqueue, Dequeue, Display, and check if the queue is full or
empty.

Code Implementation:

Queue Class:

class Queue {
int queue[];
int rear;
int front;
int size;

public Queue(int size) {


rear = -1;
front = 0;
this.size = size;
queue = new int[size * 3]; // The array size is made larger for demonstration
}

public boolean isEmpty() {


return front > rear;
}

public boolean isFull() {


return rear - front >= size - 1;
}

public void enqueue(int element) {


if (isFull()) {
System.out.println("Overflow");
} else {
rear++;
queue[rear] = element;
}
}

public int dequeue() {


int delement;
if (isEmpty()) {
System.out.println("Underflow");
return -1;
} else {
delement = queue[front];
front++;
return delement;
}
}

public void display() {


if (isEmpty()) {
System.out.println("No Elements");
} else {
System.out.println("Elements:");
for (int i = front; i <= rear; i++) {
System.out.print(" " + queue[i]);
}
System.out.println();
}
}
}

QueueOperations Class:

import java.util.Scanner;

public class QueueOperations {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int size;
int element;
int choice = 0;
System.out.println("Enter the size:");
size = scanner.nextInt();
Queue queue = new Queue(size);

while (choice < 6) {


System.out.println("1. EnQueue 2. DeQueue 3. Display 4. isEmpty 5. isFull 6. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
if (queue.isFull()) {
System.out.println("Overflow");
} else {
System.out.println("Element:");
element = scanner.nextInt();
queue.enqueue(element);
}
break;
case 2:
System.out.println("Deleted Element is: " + queue.dequeue());
break;
case 3:
queue.display();
break;
case 4:
System.out.println("is Empty: " + queue.isEmpty());
break;
case 5:
System.out.println("is Full: " + queue.isFull());
break;
case 6:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice!");
}
}
scanner.close();
}
}

Sample Input/Output:

Test Case 1: Enqueue and Display


Input:
Enter the size:
3
1. EnQueue 2. DeQueue 3. Display 4. isEmpty 5. isFull 6. Exit
1
Element:
10
1. EnQueue 2. DeQueue 3. Display 4. isEmpty 5. isFull 6. Exit
1
Element:
20
1. EnQueue 2. DeQueue 3. Display 4. isEmpty 5. isFull 6. Exit
3

Output:
Elements:
10 20

Test Case 2: Dequeue and Display


Input:
1. EnQueue 2. DeQueue 3. Display 4. isEmpty 5. isFull 6. Exit
2
1. EnQueue 2. DeQueue 3. Display 4. isEmpty 5. isFull 6. Exit
3

Output:
Deleted Element is: 10
Elements:
20

Test Case 3: Check Full and Empty Conditions


Input:
1. EnQueue 2. DeQueue 3. Display 4. isEmpty 5. isFull 6. Exit
5

Output:
is Full: false

Post-Lab Questions:
1. What is the difference between a linear queue and a circular queue?
-2. How does the program handle queue overflow and underflow situations?
3. What happens if the dequeue operation is performed on an empty queue?
4. Modify the program to handle dynamic resizing of the queue.

You might also like