Queue Part I:
Queue by Array:
#include <iostream>
#define SIZE 5 // Define maximum size of the queue
class Queue {
private:
int arr[SIZE];
int front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
bool isEmpty() {
return front == -1;
}
bool isFull() {
return (rear + 1) % SIZE == front;
}
void enqueue(int value) {
if (isFull()) {
std::cout << "Queue is full" << std::endl;
return;
}
if (isEmpty()) {
front = 0;
}
rear = (rear + 1) % SIZE;
arr[rear] = value;
std::cout << value << " enqueued to queue" << std::endl;
}
void dequeue() {
if (isEmpty()) {
std::cout << "Queue is empty" << std::endl;
return;
}
std::cout << arr[front] << " dequeued from queue" << std::endl;
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % SIZE;
}
}
void display() {
if (isEmpty()) {
std::cout << "Queue is empty" << std::endl;
return;
}
int i = front;
std::cout << "Queue elements: ";
while (i != rear) {
std::cout << arr[i] << " ";
i = (i + 1) % SIZE;
}
std::cout << arr[rear] << std::endl;
}
};
int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.display();
q.dequeue();
q.dequeue();
q.display();
q.enqueue(6);
q.enqueue(7);
q.display();
return 0;
}
Queue by List< >:
#include <iostream>
#include <vector>
class Queue {
private:
std::vector<int> arr;
int front, rear;
public:
Queue() {
front = rear = -1;
}
bool isEmpty() {
return front == -1;
}
void enqueue(int value) {
if (isEmpty()) {
front = rear = 0;
} else {
rear++;
}
arr.push_back(value);
std::cout << value << " enqueued to queue" << std::endl;
}
void dequeue() {
if (isEmpty()) {
std::cout << "Queue is empty" << std::endl;
return;
}
std::cout << arr[front] << " dequeued from queue" << std::endl;
arr.erase(arr.begin()); // Remove the front element
if (front == rear) {
front = rear = -1;
}
}
void display() {
if (isEmpty()) {
std::cout << "Queue is empty" << std::endl;
return;
}
std::cout << "Queue elements: ";
for (int i = front; i <= rear; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
};
int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.display();
q.dequeue();
q.dequeue();
q.display();
q.enqueue(6);
q.enqueue(7);
q.display();
return 0;
}
Queue by Linked List:
#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;
}
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;
}
std::cout << value << " enqueued to queue" << std::endl;
}
void dequeue() {
if (isEmpty()) {
std::cout << "Queue is empty" << std::endl;
return;
}
Node* temp = front;
std::cout << temp->data << " dequeued from queue" << std::endl;
front = front->next;
delete temp;
if (front == nullptr) {
rear = nullptr;
}
}
void display() {
if (isEmpty()) {
std::cout << "Queue is empty" << std::endl;
return;
}
std::cout << "Queue elements: ";
Node* current = front;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
};
int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.display();
q.dequeue();
q.dequeue();
q.display();
q.enqueue(6);
q.enqueue(7);
q.display();
return 0;
}
Queue by STL:
#include <iostream>
#include <queue>
int main() {
std::queue<int> q;
// Enqueue elements
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
// Display queue elements
std::cout << "Queue elements: ";
while (!q.empty()) {
std::cout << q.front() << " ";
q.pop(); // Dequeue elements
}
std::cout << std::endl;
return 0;
}
Queue by SDL_II:
#include<iostream>
#include<stack>
using namespace std;
main() {
// syntax stack<datatype>name;
stack <int>st;
st.push(10);
st.push(20);
st.push(30);
st.push(40);
cout<<"Number of item in stak is:"<<st.size() <<endl;
cout<<"Top element of stack is:"<<st.top()«<endl;
while(!st.empty(){
cout<<st.top() <<endl;
st.pop();
}
}
Queue by Stack:
#include <iostream>
#include <stack>
class Queue {
private:
std::stack<int> enqStack; // For enqueue operations
std::stack<int> deqStack; // For dequeue operations
public:
void enqueue(int value) {
enqStack.push(value);
std::cout << value << " enqueued to queue" << std::endl;
}
void dequeue() {
if (deqStack.empty()) {
if (enqStack.empty()) {
std::cout << "Queue is empty" << std::endl;
return;
}
// Transfer elements from enqueue stack to dequeue stack
while (!enqStack.empty()) {
deqStack.push(enqStack.top());
enqStack.pop();
}
}
int front = deqStack.top();
deqStack.pop();
std::cout << front << " dequeued from queue" << std::endl;
}
void display() {
if (enqStack.empty() && deqStack.empty()) {
std::cout << "Queue is empty" << std::endl;
return;
}
std::cout << "Queue elements: ";
// Display elements in dequeue stack (in reversed order)
while (!deqStack.empty()) {
std::cout << deqStack.top() << " ";
deqStack.pop();
}
// Display elements in enqueue stack (in correct order)
std::stack<int> temp;
while (!enqStack.empty()) {
temp.push(enqStack.top());
enqStack.pop();
}
while (!temp.empty()) {
std::cout << temp.top() << " ";
temp.pop();
}
std::cout << std::endl;
}
};
int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.display();
q.dequeue();
q.dequeue();
q.enqueue(4);
q.display();
q.dequeue();
q.dequeue();
q.dequeue();
q.display();
return 0;
}
Stack by Queue:
I think the code for Stack by Queue is missing. can you please add the code for stack by queue as well for practice?
ReplyDelete