Task 1: Understanding Queue Basics
Q1. Define a queue and explain how it differs from a
stack.
A queue is a linear data structure that stores elements in a sequential manner,
following the FIFO (First In, First Out) principle.
In contrast, a stack follows the LIFO (Last In, First Out) principle, meaning the last
inserted element is removed first.
Q2. Explain FIFO (First In, First Out) principle with a
real-life example.
The FIFO principle means that the element inserted first will be removed first.
Example:
A queue in a bank or ticket counter — the person who arrives first is served first.
Q3. Describe the role of front and rear pointers in a
queue.
Front: Indicates the position of the first element in the queue (used for
deletion).
Rear: Indicates the position of the last element in the queue (used for
insertion).
Q4. List the operations of a queue and briefly describe
each.
1.
Enqueue: Add an element to the rear of the queue.
2.
3.
Dequeue: Remove an element from the front of the queue.
4.
5.
Peek: View the element at the front without removing it.
6.
7.
Display: Show all elements from front to rear.
8.
9.
IsEmpty / IsFull: Check if the queue is empty or full.
10.
Task 2: Declare a Queue Structure
#include <iostream>using namespace std;
#define SIZE 5
class Queue {
int items[SIZE];
int front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
};
Task 3: Implement enqueue() Operation
void enqueue(int element) {
if (rear == SIZE - 1) {
cout << "Queue Overflow! Cannot insert more elements.\n";
} else {
if (front == -1) front = 0;
items[++rear] = element;
cout << element << " inserted into queue.\n";
}
}
Task 4: Implement dequeue() Operation
void dequeue() {
if (front == -1 || front > rear) {
cout << "Queue Underflow! No elements to delete.\n";
} else {
cout << items[front] << " deleted from queue.\n";
front++;
}
}
Task 5: Implement display() Function
void display() {
if (front == -1 || front > rear) {
cout << "Queue is Empty!\n";
} else {
cout << "Queue elements are: ";
for (int i = front; i <= rear; i++) {
cout << items[i] << " ";
}
cout << endl;
}
}
Task 6: Complete Queue Program
#include <iostream>using namespace std;
#define SIZE 5
class Queue {
int items[SIZE];
int front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
void enqueue(int element) {
if (rear == SIZE - 1) {
cout << "Queue Overflow! Cannot insert more elements.\n";
} else {
if (front == -1) front = 0;
items[++rear] = element;
cout << element << " inserted into queue.\n";
}
}
void dequeue() {
if (front == -1 || front > rear) {
cout << "Queue Underflow! No elements to delete.\n";
} else {
cout << items[front] << " deleted from queue.\n";
front++;
}
}
void display() {
if (front == -1 || front > rear) {
cout << "Queue is Empty!\n";
} else {
cout << "Queue elements are: ";
for (int i = front; i <= rear; i++) {
cout << items[i] << " ";
}
cout << endl;
}
}
void peek() {
if (front == -1 || front > rear) {
cout << "Queue is Empty!\n";
} else {
cout << "Front element is: " << items[front] << endl;
}
}
int count() {
if (front == -1 || front > rear)
return 0;
return (rear - front + 1);
}
};
int main() {
Queue q;
int choice, value;
while (true) {
cout << "\n===== Queue Menu =====\n";
cout << "1. Enqueue\n2. Dequeue\n3. Display\n4. Peek\n5.
Count Elements\n6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
[Link](value);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
[Link]();
break;
case 5:
cout << "Total elements: " << [Link]() << endl;
break;
case 6:
cout << "Exiting program. Goodbye!\n";
return 0;
default:
cout << "Invalid choice! Try again.\n";
}
}
}