Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab06 Quees (Array)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Queues using Arrays

Activity No.1:
#include <iostream>

const int MAX_SIZE = 100;

class Queue {
private:
int queue[MAX_SIZE];
int front;
int rear;

public:
Queue() {
front = -1;
rear = -1;
}

void enqueue(int item) {


if (rear == (MAX_SIZE - 1)) {
std::cout << "Queue Overflow! Cannot enqueue item.\n";
return;
}

if (front == -1)
front = 0;

queue[++rear] = item;
std::cout << "Enqueued item: " << item << std::endl;
}

int dequeue() {
if (front == -1 || front > rear) {
std::cout << "Queue Underflow! Cannot dequeue item.\n";
return -1;
}

int dequeuedItem = queue[front++];


std::cout << "Dequeued item: " << dequeuedItem << std::endl;
if (front > rear) {
front = -1;
rear = -1;
}

return dequeuedItem;
}
};

int main() {
Queue myQueue;

myQueue.enqueue(10);
myQueue.enqueue(20);
myQueue.enqueue(30);

myQueue.dequeue();
myQueue.dequeue();
myQueue.dequeue();

return 0;
}

Output:
Activity No.2:
#include <iostream>

const int MAX_SIZE = 100;

class Queue {
private:
int queue[MAX_SIZE];
int front;
int rear;

public:
Queue() {
front = -1;
rear = -1;
}

void enqueue(int item) {


if (isFull()) {
std::cout << "Queue Overflow! Cannot enqueue item.\n";
return;
}
if (isEmpty())
front = 0;
rear = (rear + 1) % MAX_SIZE;
queue[rear] = item;
std::cout << "Enqueued item: " << item << std::endl;
}

int dequeue() {
if (isEmpty()) {
std::cout << "Queue Underflow! Cannot dequeue item.\n";
return -1;
}
int dequeuedItem = queue[front];
if (front == rear) {
front = -1;
rear = -1;
} else
front = (front + 1) % MAX_SIZE;
std::cout << "Dequeued item: " << dequeuedItem << std::endl;
return dequeuedItem;
}

bool isFull() {
return (front == (rear + 1) % MAX_SIZE);
}

bool isEmpty() {
return (front == -1 && rear == -1);
}
};

int main() {
Queue myQueue;

myQueue.enqueue(10);
myQueue.enqueue(20);
myQueue.enqueue(30);

myQueue.dequeue();
myQueue.dequeue();
myQueue.dequeue();

return 0;
}

Output:

Queue using Linked List


Activity No.1:
#include <iostream>

class Node {
public:
int data;
Node* next;

Node(int value) {
data = value;
next = nullptr;
}
};

class Queue {
private:
Node* front;
Node* rear;

public:
Queue() {
front = nullptr;
rear = nullptr;
}

void enqueue(int item) {


Node* newNode = new Node(item);
if (front == nullptr) {
front = newNode;
rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
std::cout << "Enqueued item: " << item << std::endl;
}

int dequeue() {
if (front == nullptr) {
std::cout << "Queue Underflow! Cannot dequeue item.\n";
return -1;
}
Node* temp = front;
int dequeuedItem = temp->data;
front = front->next;
if (front == nullptr)
rear = nullptr;
delete temp;
std::cout << "Dequeued item: " << dequeuedItem << std::endl;
return dequeuedItem;
}
};

int main() {
Queue myQueue;

myQueue.enqueue(10);
myQueue.enqueue(20);
myQueue.enqueue(30);

myQueue.dequeue();
myQueue.dequeue();
myQueue.dequeue();

return 0;
}
Output:

Activity No.2:
#include <iostream>
class Node {
public:
int data;
Node* next;

Node(int value) {
data = value;
next = nullptr;
}
};

class Queue {
private:
Node* front;
Node* rear;

public:
Queue() {
front = nullptr;
rear = nullptr;
}

void enqueue(int item) {


Node* newNode = new Node(item);
if (isEmpty()) {
front = newNode;
rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
std::cout << "Enqueued item: " << item << std::endl;
}

int dequeue() {
if (isEmpty()) {
std::cout << "Queue Underflow! Cannot dequeue item.\n";
return -1;
}
Node* temp = front;
int dequeuedItem = temp->data;
front = front->next;
if (front == nullptr)
rear = nullptr;
delete temp;
std::cout << "Dequeued item: " << dequeuedItem << std::endl;
return dequeuedItem;
}

bool isEmpty() {
return (front == nullptr);
}
};

int main() {
Queue myQueue;

myQueue.enqueue(10);
myQueue.enqueue(20);
myQueue.enqueue(30);

myQueue.dequeue();
myQueue.dequeue();
myQueue.dequeue();

return 0;
}

Output:

You might also like