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

Queue Lab Manual

The lab manual covers the implementation of queues using arrays and linked lists in C++. It outlines the objectives, theory, algorithms, and C++ code for both implementations, including enqueue and dequeue operations. Additionally, it discusses the time and space complexity of queue operations for both array-based and linked-list-based implementations.

Uploaded by

Irfan Ul Haq
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)
26 views6 pages

Queue Lab Manual

The lab manual covers the implementation of queues using arrays and linked lists in C++. It outlines the objectives, theory, algorithms, and C++ code for both implementations, including enqueue and dequeue operations. Additionally, it discusses the time and space complexity of queue operations for both array-based and linked-list-based implementations.

Uploaded by

Irfan Ul Haq
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/ 6

Lab Manual:

Data Structures and Algorithms in C++


Topic:
Implementation of Queues (Using Arrays and Linked Lists)
Lab Objectives
1. Understand the concept of queues and their applications.
2. Implement queues using arrays.
3. Implement queues using linked lists.
4. Perform enqueue and dequeue operations.
5. Analyze the differences between array-based and linked-list-based queue
implementations.

Theory
Queue
A Queue is a linear data structure that follows the FIFO (First In, First Out) principle.

• Enqueue: Adding an element to the end of the queue.


• Dequeue: Removing an element from the front of the queue.

Types of Queue Implementation


1. Array-based Implementation
2. Linked List-based Implementation

Lab-1: Queue Implementation Using Arrays


Algorithm
Enqueue (Insert)

1. Check if the queue is full.


2. If not full, increment the rear and insert the element.

Dequeue (Remove)

1. Check if the queue is empty.


2. If not empty, return the element at front and increment front.

C++ Code: Array Implementation

#include <iostream>
#define SIZE 5
using namespace std;
class Queue {
int arr[SIZE];
int front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
bool isFull() {
return rear == SIZE - 1;
}
bool isEmpty() {
return front == -1 || front > rear;
}

void enqueue(int value) {


if (isFull()) {
cout << "Queue Overflow!" << endl;
return;
}
if (front == -1) front = 0;
arr[++rear] = value;
cout << "Enqueued: " << value << endl;
}
void dequeue() {
if (isEmpty()) {
cout << "Queue Underflow!" << endl;
return;
}
cout << "Dequeued: " << arr[front++] << endl;
}
void display() {
if (isEmpty()) {
cout << "Queue is empty!" << endl;
return;
}
cout << "Queue elements: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.dequeue();
q.display();
return 0;
}

Lab-2: Queue Implementation Using Linked Lists


Algorithm
Enqueue (Insert)

1. Create a new node.


2. If the queue is empty, set front and rear to the new node.
3. Otherwise, set rear->next to the new node and update rear.

Dequeue (Remove)

1. Check if the queue is empty.


2. If not empty, store the front node, move front to the next node, and delete the old node.
C++ Code: Linked List Implementation

#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
class Queue {
Node* front;
Node* rear;
public:
Queue() {
front = rear = nullptr;
}
bool isEmpty() {
return front == nullptr;
}
void enqueue(int value) {
Node* newNode = new Node(value);
if (isEmpty()) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
cout << "Enqueued: " << value << endl;
}
void dequeue() {
if (isEmpty()) {
cout << "Queue Underflow!" << endl;
return;
}
Node* temp = front;
cout << "Dequeued: " << front->data << endl;
front = front->next;
delete temp;
}
void display() {
if (isEmpty()) {
cout << "Queue is empty!" << endl;
return;
}
Node* temp = front;
cout << "Queue elements: ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.dequeue();
q.display();
return 0;
}

Time and Space Complexity of Queue Operations

1. Array-Based Queue Implementation

In an array-based queue, we use a fixed-size array to store elements and track the front and
rear pointers.

Time Complexity

Operation Time Explanation


Complexity
Enqueue O (1) Adding an element at the rear position is a constant-
time operation.
Dequeue O (1) Removing an element from the front position is also a
constant-time operation.
Peek/Front O (1) Accessing the element at the front is a direct access
operation.
IsEmpty/IsFull O (1) Checking if the queue is empty or full requires simple
comparison of pointers.

Space Complexity

• O(n): Space required for the array of size n. This is static and can lead to wasted space if
the queue isn't full.

2. Linked List-Based Queue Implementation

In a linked-list-based queue, each element is a node containing data and a pointer to the next
node. front and rear pointers are used to track the queue.
Time Complexity

Operation Time Explanation


Complexity
Enqueue O(1) Adding a node at the rear involves creating a new node and
updating the rear pointer.
Dequeue O(1) Removing a node from the front only involves updating the
front pointer.
Peek/Front O(1) Accessing the element at the front is a direct operation via
the front pointer.
IsEmpty O(1) Checking if the queue is empty only involves checking if
front is nullptr.

Space Complexity

• O(n): Space required for n nodes, where each node uses extra space for the next pointer.

You might also like