Dsa Activity
Dsa Activity
#include <iostream>
using namespace std;
const int MAX_SIZE = 10;
class Stack {
private:
int arr[MAX_SIZE];
int top;
public:
Stack() {
top = -1;
}
void push(int data) {
if (top == MAX_SIZE - 1) {
cout << "Stack overflow" << endl;
return;
}
arr[++top] = data;
}
int pop() {
if (top == -1) {
cout << "Stack underflow" << endl;
return -1;
}
return arr[top--];
}
};
int main() {
Stack s;
s.push(1);
s.push(2);
s.push(3);
cout << s.pop() << endl;
cout << s.pop() << endl;
cout << s.pop() << endl;
return 0;
}
PROGRAM #2
#include <iostream>
using namespace std;
const int MAX_SIZE = 10;
class Queue {
private:
int arr[MAX_SIZE];
int front;
int rear;
public:
Queue() {
front = 0;
rear = MAX_SIZE - 1;
}
int dequeue() {
if (front == rear + 1) {
cout << "Queue underflow" << endl;
return -1;
}
int data = arr[front];
front = (front + 1) % MAX_SIZE;
return data;
}
};
int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
return 0;
}
PROGRAM #3
#include <iostream>
#include <list>
using namespace std;
class Stack {
private:
list<int> data;
public:
void push(int n) {
data.push_back(n);
}
int pop() {
if (data.empty()) {
cout << "Stack underflow" << endl;
return -1;
}
int n = data.back();
data.pop_back();
return n;
}
};
int main() {
Stack s;
s.push(1);
s.push(2);
s.push(3);
cout << s.pop() << endl;
cout << s.pop() << endl;
cout << s.pop() << endl;
return 0;
}
PROGRAM # 4
#include <iostream>
#include <list>
using namespace std;
class Queue {
private:
list<int> data;
public:
void enqueue(int n) {
data.push_back(n);
}
int dequeue() {
if (data.empty()) {
cout << "Queue underflow" << endl;
return -1;
}
int n = data.front();
data.pop_front();
return n;
}
};
int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
return 0;
}
PROGRAM #5
#include <iostream>
#include <stack>
int main() {
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout << s.top() << endl;
s.pop();
cout << s.top() << endl;
s.pop();
cout << s.top() << endl;
s.pop();
return 0;
}
PROGRAM #6
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
cout << q.front() << endl;
q.pop();
cout << q.front() << endl;
q.pop();
cout << q.front() << endl;
q.pop();
return 0;
}
EXERCISES
1. Given the following stack: [1, 2, 3, 4], write a function that removes the top
element from the stack.
2. Given the following queue: [1, 2, 3, 4], write a function that removes the front
element from the queue.
3. Given the following stack: [1, 2, 3, 4], write a function that adds the element
to the top of the stack.
4. Given the following queue: [1, 2, 3, 4], write a function that adds the element
5 to the back of the queue.
5. Given an array of integers, write a function that uses a stack to reverse the
order of the elements in the array.
A. INORDER TRAVERSAL
B. PRE-ORDER TRAVERSAL
C. POST- ORDER TRAVERSAL
A. STACKS
B. QUEUES
C. TREES