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

Implementation of Queue Using Array

The document provides implementations of a queue using both an array and a linked list in C++. It includes code for enqueueing, dequeueing, peeking, checking if the queue is empty or full, and getting the size of the queue. Exception handling is incorporated to manage overflow and underflow errors.
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)
4 views

Implementation of Queue Using Array

The document provides implementations of a queue using both an array and a linked list in C++. It includes code for enqueueing, dequeueing, peeking, checking if the queue is empty or full, and getting the size of the queue. Exception handling is incorporated to manage overflow and underflow errors.
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/ 11

IMPLEMENTATION OF

QUEUE USING ARRAY


AND LINKED LIST

Muzammil Khan
MC-403
IMPLEMENTATION OF QUEUE USING ARRAY
CODE:
#include <iostream>
#include <stdexcept> // For std::overflow_error and std::underflow_error

class QueueArray {
private:
int* arr;
int capacity;
int front_idx;
int rear_idx;
int count;

public:
QueueArray(int size) {
capacity = size;
arr = new int[capacity];
front_idx = 0;
rear_idx = -1;
count = 0;
}

~QueueArray() {
delete[] arr;
}

void enqueue(int item) {


if (isFull()) {
throw std::overflow_error("Queue is full");
}
rear_idx = (rear_idx + 1) % capacity;
arr[rear_idx] = item;
count++;
}

int dequeue() {
if (isEmpty()) {
throw std::underflow_error("Queue is empty");
}
int item = arr[front_idx];
front_idx = (front_idx + 1) % capacity;
count--;
return item;
}

int peek() {
if (isEmpty()) {
throw std::underflow_error("Queue is empty");
}
return arr[front_idx];
}

bool isEmpty() {
return count == 0;
}

bool isFull() {
return count == capacity;
}

int getSize() { // Renamed from size() to avoid potential conflicts


return count;
}
};

int main() {
try {
QueueArray q(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
std::cout << "Dequeued item: " << q.dequeue() << std::endl; //
Expected: 10
std::cout << "Front item: " << q.peek() << std::endl; // Expected: 20
std::cout << "Queue size: " << q.getSize() << std::endl; // Expected:
2

q.enqueue(40);
q.enqueue(50);
// q.enqueue(60); // This would throw std::overflow_error

std::cout << "Queue elements: ";


while (!q.isEmpty()) {
std::cout << q.dequeue() << " ";
}
std::cout << std::endl;

// q.dequeue(); // This would throw std::underflow_error


// q.peek(); // This would throw std::underflow_error

} catch (const std::exception& e) {


std::cerr << "Exception: " << e.what() << std::endl;
}

return 0;
}
RESULT:

IMPLEMENTATION OF QUEUE USING LINKED LIST:


CODE:
#include <iostream>
#include <stdexcept> // For std::underflow_error

struct Node {
int data;
Node* next;

Node(int val) : data(val), next(nullptr) {}


};

class QueueLinkedList {
private:
Node* front_node;
Node* rear_node;
int count;

public:
QueueLinkedList() : front_node(nullptr),
rear_node(nullptr), count(0) {}

~QueueLinkedList() {
while (!isEmpty()) {
dequeue();
}
}

void enqueue(int item) {


Node* newNode = new Node(item);
if (isEmpty()) {
front_node = newNode;
rear_node = newNode;
} else {
rear_node->next = newNode;
rear_node = newNode;
}
count++;
}

int dequeue() {
if (isEmpty()) {
throw std::underflow_error("Queue is empty");
}
Node* temp = front_node;
int item = temp->data;
front_node = front_node->next;
delete temp;
count--;
if (isEmpty()) { // If queue becomes empty after
dequeue
rear_node = nullptr;
}
return item;
}
int peek() {
if (isEmpty()) {
throw std::underflow_error("Queue is empty");
}
return front_node->data;
}

bool isEmpty() {
return count == 0; // Or front_node == nullptr
}

int getSize() { // Renamed from size() to avoid potential


conflicts
return count;
}
};

int main() {
try {
QueueLinkedList q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);

std::cout << "Dequeued item: " << q.dequeue() <<


std::endl; // Expected: 10
std::cout << "Front item: " << q.peek() <<
std::endl; // Expected: 20
std::cout << "Queue size: " << q.getSize() <<
std::endl; // Expected: 2

q.enqueue(40);
q.enqueue(50);

std::cout << "Queue elements: ";


while (!q.isEmpty()) {
std::cout << q.dequeue() << " ";
}
std::cout << std::endl;
// q.dequeue(); // This would throw
std::underflow_error
// q.peek(); // This would throw std::underflow_error

} catch (const std::exception& e) {


std::cerr << "Exception: " << e.what() << std::endl;
}

return 0;
}

RESULT:

You might also like