PRACTICE FOR QUIZ
STACK
CODE 1: SIMPLE STACK
#include<iostream>
#include<stack>
#include<stdlib.h>
using namespace std;
class Stack {
private:
int* arr;
int top;
const int n = 100;
public:
Stack() {
arr = new int[n];
top = -1;
}
void push(int x) {
if (top == n - 1) {
cout << "stack overflow." << endl;
return;
}
top++;
arr[top] = x;
}
void pop() {
if (top == -1) {
cout << "There is no element to pop." << endl;
return;
}
top--;
}
int Top() {
if (top == -1) {
cout << "There is no stack." << endl;
return -1;
}
return arr[top];
}
bool empty() {
return top == -1;
}
};
int main() {
Stack st;
[Link](0);
[Link](1);
[Link](2);
cout << "Stack top: " << [Link]() << endl;
[Link]();
cout << "After popping, stack top: " << [Link]() << endl;
[Link]();
[Link]();
[Link]();
cout << "After popping more times than the elements in stack, stack top: " << [Link]() << endl;
return 0;
}
___________________________________________________________________
CODE 2: STACK REVERSE
#include <iostream>
#include <stack>
#include <string>
using namespace std;
string ReverseStack(string s) {
stack<char> st;
for (char ch : s) {
[Link](ch);
}
string reversed;
while (![Link]()) {
reversed += [Link]();
[Link]();
}
return reversed;
}
int main() {
string s = "0123456789";
string reversed = ReverseStack(s);
cout << "Original: " << s << endl;
cout << "Reversed: " << reversed << endl;
return 0;
}
___________________________________________________________________
CODE 3: STACK REVERSE METHOD 2 (DIFFERENT OUTPUT)
#include<iostream>
#include<stack>
#include<string>
using namespace std;
void ReverseStack(string s) {
stack<string> st;
for (int i = 0; i < [Link](); i++) {
string word = "";
while (s[i] != ' ' && i < [Link]()) {
word += s[i];
i++;
}
[Link](word);
}
while (![Link]()) {
cout <<[Link]()<<" ";
[Link]();
}
cout << endl;
}
int main() {
string s = "Hello! My name is Sumayyah.";
cout << "Original: " << s << endl;
cout << "Reversed: ";
ReverseStack(s);
___________________________________________________________________
QUEUE
CODE 1: SIMPLE QUEUE
#include<iostream>
#include<queue>
using namespace std;
class Queue {
private:
int* arr;
int front, back;
const int n = 100;
public:
Queue(){
arr = new int[n];
front = back = -1;
}
void enqueue(int x) {
if (back == n - 1) {
cout << "stack overflow." << endl;
return;
}
back++;
arr[back] = x;
if (front == -1) {
front++;
}
}
void dequeue() {
if (front == -1 || front > back) {
cout << "No element in queue." << endl;
return;
}
front++;
cout << "1st Element removed." << endl;
}
int peek() {
if (front == -1 || front > back) {
cout << "No element in queue." << endl;
return -1;
}
return arr[front];
}
bool empty() {
if (front == -1 || front > back) {
return true;
}
return false;
}
void display() {
if (back == front) {
cout << "queue is empty." << endl;
return;
}
cout << "Elements in queue: ";
for (int i = front; i <= back; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int size() {
if (front == -1 || front > back) {
return 0;
}
return back - front + 1;
}
};
int main() {
Queue q;
[Link](1);
[Link](2);
[Link](3);
[Link]();
cout << "Queue size: " << [Link]() << endl;
cout << "Element in front: " << [Link]() << endl << endl;
[Link]();
[Link]();
cout << "Queue size: " << [Link]() << endl;
cout << "Element in front: " << [Link]() << endl << endl;
[Link]();
[Link]();
cout << "Queue size: " << [Link]() << endl;
cout << "Element in front: " << [Link]() << endl << endl;
[Link]();
[Link]();
cout << "Queue size: " << [Link]() << endl;
cout << "Element in front: " << [Link]() << endl << endl;
[Link]();
[Link]();
cout << "Queue size: " << [Link]() << endl;
return 0;
}
___________________________________________________________________
CODE 2: PRIORITY QUEUE
#include<iostream>
#include<algorithm>
using namespace std;
class PriorityQueue {
private:
int* arr;
int size;
const int n = 100;
public:
PriorityQueue() {
arr = new int[n];
size = 0;
}
void enqueue(int x) {
if (size == n) {
cout << "Queue is full. Can't insert." << endl;
return;
}
arr[size++] = x;
sort(arr, arr + size);
}
int dequeue() {
if (size == 0) {
cout << "Queue is empty. No element to delete." << endl;
return -1;
}
int HighestPriorityElement = arr[0];
for (int i = 0; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
return HighestPriorityElement;
}
int peek() {
if (size == 0) {
cout << "Queue is empty. No element to delete." << endl;
return -1;
}
return arr[0];
}
bool empty() {
return size == 0;
}
void display() {
if (size == 0) {
cout << "Queue is empty." << endl;
return;
}
cout << "Priority Queue: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main() {
PriorityQueue pq;
[Link](30);
[Link](20);
[Link](50);
[Link](10);
[Link](); // Output: 10 20 30 50 (10 has the highest priority)
cout << "Dequeued element: " << [Link]() << endl; // Output: 10
[Link](); // Output: 20 30 50
cout << "Element with highest priority: " << [Link]() << endl; // Output: 20
return 0;
}
___________________________________________________________________
CODE 3: CIRCULAR QUEUE
#include <iostream>
using namespace std;
class CircularQueue {
private:
int *arr;
int front;
int rear;
const int SIZE = 5;
public:
CircularQueue() {
arr = new int[SIZE];
front = -1;
rear = -1;
}
// Check if the queue is full
bool isFull() {
if ((front == 0 && rear == SIZE - 1) ||
(rear == (front - 1) % (SIZE))) {
return true;
}
return false;
}
// Check if the queue is empty
bool isEmpty() {
if (front == -1)
return true;
return false;
}
// Add an element to the queue
void enqueue(int value) {
if (isFull()) {
cout << "Queue is Full\n";
return;
}
if (front == -1) // Insert first element
front = 0;
rear = (rear + 1) % SIZE;
arr[rear] = value;
cout << "Inserted " << value << "\n";
}
// Remove an element from the queue
void dequeue() {
if (isEmpty()) {
cout << "Queue is Empty\n";
return;
}
cout << "Removed " << arr[front] << "\n";
if (front == rear) { // Queue has only one element
front = -1;
rear = -1;
}
else {
front = (front + 1) % SIZE;
}
}
// Display the elements of the queue
void display() {
if (isEmpty()) {
cout << "Queue is Empty\n";
return;
}
cout << "Queue elements: ";
if (rear >= front) {
for (int i = front; i <= rear; i++)
cout << arr[i] << " ";
}
else {
for (int i = front; i < SIZE; i++)
cout << arr[i] << " ";
for (int i = 0; i <= rear; i++)
cout << arr[i] << " ";
}
cout << "\n";
}
};
int main() {
CircularQueue q;
[Link](14);
[Link](22);
[Link](13);
[Link](-6);
[Link]();
[Link]();
[Link]();
[Link]();
[Link](9);
[Link](20);
[Link](5);
[Link](7); // This should indicate the queue is full
[Link]();
[Link]();
[Link](20);
[Link]();
return 0;
}
___________________________________________________________________