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

Practice For Quiz

Uploaded by

itznek0bruh
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)
30 views

Practice For Quiz

Uploaded by

itznek0bruh
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/ 9

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;
st.push(0);
st.push(1);
st.push(2);
cout << "Stack top: " << st.Top() << endl;
st.pop();
cout << "After popping, stack top: " << st.Top() << endl;
st.pop();
st.pop();
st.pop();
cout << "After popping more times than the elements in stack, stack top: " << st.Top() << 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) {
st.push(ch);
}

string reversed;
while (!st.empty()) {
reversed += st.top();
st.pop();
}
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 < s.length(); i++) {
string word = "";
while (s[i] != ' ' && i < s.length()) {
word += s[i];
i++;
}
st.push(word);
}

while (!st.empty()) {
cout <<st.top()<<" ";
st.pop();
}
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;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);

q.display();
cout << "Queue size: " << q.size() << endl;
cout << "Element in front: " << q.peek() << endl << endl;

q.dequeue();
q.display();
cout << "Queue size: " << q.size() << endl;
cout << "Element in front: " << q.peek() << endl << endl;

q.dequeue();
q.display();
cout << "Queue size: " << q.size() << endl;
cout << "Element in front: " << q.peek() << endl << endl;

q.dequeue();
q.display();
cout << "Queue size: " << q.size() << endl;
cout << "Element in front: " << q.peek() << endl << endl;

q.dequeue();
q.display();
cout << "Queue size: " << q.size() << 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;

pq.enqueue(30);
pq.enqueue(20);
pq.enqueue(50);
pq.enqueue(10);

pq.display(); // Output: 10 20 30 50 (10 has the highest priority)

cout << "Dequeued element: " << pq.dequeue() << endl; // Output: 10
pq.display(); // Output: 20 30 50

cout << "Element with highest priority: " << pq.peek() << 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;

q.enqueue(14);
q.enqueue(22);
q.enqueue(13);
q.enqueue(-6);
q.display();

q.dequeue();
q.dequeue();
q.display();

q.enqueue(9);
q.enqueue(20);
q.enqueue(5);
q.enqueue(7); // This should indicate the queue is full
q.display();

q.dequeue();
q.enqueue(20);
q.display();

return 0;
}

___________________________________________________________________

You might also like