Lab06 Quees (Array)
Lab06 Quees (Array)
Lab06 Quees (Array)
Activity No.1:
#include <iostream>
class Queue {
private:
int queue[MAX_SIZE];
int front;
int rear;
public:
Queue() {
front = -1;
rear = -1;
}
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;
}
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 Queue {
private:
int queue[MAX_SIZE];
int front;
int rear;
public:
Queue() {
front = -1;
rear = -1;
}
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:
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;
}
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;
}
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: