0% found this document useful (0 votes)
16 views8 pages

Codes

The document contains implementations of various data structures in C++, including stacks and queues using both static and dynamic arrays. It covers basic operations such as push, pop, enqueue, and dequeue, along with handling overflow and underflow conditions. Additionally, it includes a circular queue implementation to efficiently manage elements in a fixed-size array.

Uploaded by

Anwar Nasir
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)
16 views8 pages

Codes

The document contains implementations of various data structures in C++, including stacks and queues using both static and dynamic arrays. It covers basic operations such as push, pop, enqueue, and dequeue, along with handling overflow and underflow conditions. Additionally, it includes a circular queue implementation to efficiently manage elements in a fixed-size array.

Uploaded by

Anwar Nasir
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/ 8

DSA CODES

STACK USING STATIC ARRAY


#include <iostream>
using namespace std;
#define MAX 5

class Stack {
int arr[MAX], top;
public:
Stack() : top(-1) {} // Constructor initializes top as -1

bool isEmpty() { return top == -1; }


bool isFull() { return top == MAX - 1; }

void push(int x) {
if (isFull()) cout << "Stack Overflow!\n";
else cout << arr[++top] = x, cout << x << " pushed into stack\n";
}

int pop() {
return isEmpty() ? (cout << "Stack Underflow!\n", -1) : arr[top--];
}

int peek() {
return isEmpty() ? (cout << "Stack is empty\n", -1) : arr[top];
}
};

int main() {
Stack s;
s.push(10); s.push(20); s.push(30); s.push(40); s.push(50); s.push(60);

cout << "Top element is: " << s.peek() << endl;
cout << s.pop() << " popped from stack\n";
cout << s.pop() << " popped from stack\n";
cout << "Top element after popping is: " << s.peek() << endl;

return 0;
}

Stack using Dynamic Array


#include <iostream>
using namespace std;

class Stack {
int* arr, top, capacity;

void increaseCapacity() {
int* newArr = new int[capacity *= 2];
for (int i = 0; i <= top; i++) newArr[i] = arr[i];
delete[] arr, arr = newArr;
}

public:
Stack(int size) : capacity(size), top(-1) { arr = new int[capacity]; }
~Stack() { delete[] arr; }

bool isEmpty() { return top == -1; }


bool isFull() { return top == capacity - 1; }

void push(int x) {
if (isFull()) increaseCapacity();
cout << arr[++top] = x, cout << x << " pushed into stack\n";
}

int pop() { return isEmpty() ? (cout << "Stack Underflow!\n", -1) : arr[top--]; }
int peek() { return isEmpty() ? (cout << "Stack is empty\n", -1) : arr[top]; }
};

int main() {
Stack s(3);
s.push(10); s.push(20); s.push(30); s.push(40);

cout << "Top element is: " << s.peek() << endl;
cout << s.pop() << " popped from stack\n";
cout << s.pop() << " popped from stack\n";
cout << "Top element after popping is: " << s.peek() << endl;

return 0;
}

Simple Queue through Static array

#include <iostream>
using namespace std;
#define MAX 5

class Queue {
int arr[MAX], front, back;

public:
Queue() : front(-1), back(-1) {}

bool isEmpty() { return front == -1 || front > back; }


bool isFull() { return back == MAX - 1; }

void enqueue(int x) {
if (isFull()) cout << "Queue Overflow!\n";
else cout << (front == -1 ? front = 0 : 0), arr[++back] = x, cout << x << " enqueued into queue\n";
}

int dequeue() {
if (isEmpty()) return cout << "Queue Underflow!\n", -1;
int removedValue = arr[front++];
if (front > back) front = back = -1;
return removedValue;
}

int getFront() { return isEmpty() ? (cout << "Queue is empty\n", -1) : arr[front]; }
};

int main() {
Queue q;
q.enqueue(10), q.enqueue(20), q.enqueue(30), q.enqueue(40), q.enqueue(50), q.enqueue(60);

cout << "Front element is: " << q.getFront() << endl;
cout << q.dequeue() << " dequeued from queue\n";
cout << q.dequeue() << " dequeued from queue\n";
cout << "Front element after dequeuing is: " << q.getFront() << endl;

return 0;
}

Simple Queue through dynamic array


#include <iostream>
using namespace std;

class Queue {
int *arr, front, back, capacity;

public:
Queue(int size) : capacity(size), front(-1), back(-1) { arr = new int[capacity]; }
~Queue() { delete[] arr, cout << "Queue memory released\n"; }

bool isEmpty() { return front == -1 || front > back; }


bool isFull() { return back == capacity - 1; }

void enqueue(int x) {
if (isFull()) cout << "Queue Overflow!\n";
else cout << (front == -1 ? front = 0 : 0), arr[++back] = x, cout << x << " enqueued into queue\n";
}

int dequeue() {
if (isEmpty()) return cout << "Queue Underflow!\n", -1;
int removedValue = arr[front++];
if (front > back) front = back = -1;
return removedValue;
}

int getFront() { return isEmpty() ? (cout << "Queue is empty\n", -1) : arr[front]; }
};

int main() {
Queue q(5);
q.enqueue(10), q.enqueue(20), q.enqueue(30), q.enqueue(40), q.enqueue(50), q.enqueue(60);

cout << "Front element is: " << q.getFront() << endl;
cout << q.dequeue() << " dequeued from queue\n";
cout << q.dequeue() << " dequeued from queue\n";
cout << "Front element after dequeuing is: " << q.getFront() << endl;

return 0;
}

Circular Queue through Static array


#include <iostream>
using namespace std;

#define MAX 5 // Maximum size of the queue

class CircularQueue {
int arr[MAX], front, back, size;

public:
CircularQueue() : front(-1), back(-1), size(0) {}

bool isEmpty() { return size == 0; }


bool isFull() { return size == MAX; }

void enqueue(int x) {
if (isFull()) return cout << "Queue Overflow!\n", void();
if (isEmpty()) front = 0;
back = (back + 1) % MAX, arr[back] = x, size++;
cout << x << " enqueued into queue\n";
}

int dequeue() {
if (isEmpty()) return cout << "Queue Underflow! Cannot dequeue\n", -1;
int removedValue = arr[front];
front = (front + 1) % MAX, size--;
if (size == 0) front = back = -1;
return removedValue;
}

int getFront() { return isEmpty() ? (cout << "Queue is empty\n", -1) : arr[front]; }
};

int main() {
CircularQueue q;
q.enqueue(10), q.enqueue(20), q.enqueue(30), q.enqueue(40), q.enqueue(50), q.enqueue(60);

cout << "Front element is: " << q.getFront() << endl;
cout << q.dequeue() << " dequeued from queue\n";
cout << q.dequeue() << " dequeued from queue\n";

q.enqueue(60), q.enqueue(70);
cout << "Front element after dequeuing and enqueuing is: " << q.getFront() << endl;

return 0;
}

Circular Queue through dynamic array

#include <iostream>
using namespace std;

class CircularQueue {
int *arr, front, back, size, capacity;

public:
CircularQueue(int cap) : capacity(cap), front(-1), back(-1), size(0) {
arr = new int[capacity];
}

bool isEmpty() { return size == 0; }


bool isFull() { return size == capacity; }

void enqueue(int x) {
if (isFull()) return cout << "Queue Overflow!\n", void();
if (isEmpty()) front = 0;
back = (back + 1) % capacity, arr[back] = x, size++;
cout << x << " enqueued into queue\n";
}

int dequeue() {
if (isEmpty()) return cout << "Queue Underflow!\n", -1;
int removedValue = arr[front];
front = (front + 1) % capacity, size--;
if (size == 0) front = back = -1;
return removedValue;
}

int getFront() { return isEmpty() ? (cout << "Queue is empty\n", -1) : arr[front]; }

~CircularQueue() { delete[] arr, cout << "Queue memory released\n"; }


};

int main() {
CircularQueue q(5);
q.enqueue(10), q.enqueue(20), q.enqueue(30), q.enqueue(40), q.enqueue(50), q.enqueue(60);

cout << "Front element is: " << q.getFront() << endl;
cout << q.dequeue() << " dequeued from queue\n";
cout << q.dequeue() << " dequeued from queue\n";

q.enqueue(60), q.enqueue(70);
cout << "Front element after dequeuing and enqueuing is: " << q.getFront() << endl;

return 0;
}

You might also like