0% found this document useful (0 votes)
4 views6 pages

Dsa Activity

The document contains multiple C++ programs demonstrating the implementation of stacks and queues using both arrays and linked lists. It also includes exercises related to stack and queue operations, binary search tree traversals, and various functions to manipulate binary trees. Additionally, it suggests researching sample programs on stacks, queues, and trees.

Uploaded by

jeraldave4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Dsa Activity

The document contains multiple C++ programs demonstrating the implementation of stacks and queues using both arrays and linked lists. It also includes exercises related to stack and queue operations, binary search tree traversals, and various functions to manipulate binary trees. Additionally, it suggests researching sample programs on stacks, queues, and trees.

Uploaded by

jeraldave4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAM # 1

#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;
}

void enqueue(int data) {


if (front == rear + 1) {
cout << "Queue overflow" << endl;
return;
}
rear = (rear + 1) % MAX_SIZE;
arr[rear] = data;
}

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>

using namespace std;

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.

6. Discuss the idea , concepts , algorithm and give at least 10 examples of ff :

A. INORDER TRAVERSAL
B. PRE-ORDER TRAVERSAL
C. POST- ORDER TRAVERSAL

7. Research at least 5 sample programs using C++ about the following :

A. STACKS
B. QUEUES
C. TREES

8. Write a function to traverse a Binary Search Tree in-order.


9. Write a function to find the minimum value in a Binary Search Tree.
10.Write a function to find the maximum value in a Binary Search Tree.
11.Write a function to check if a value exists in a Binary Search Tree.
12.Write a function to calculate the height of a Binary Search Tree.
13.Write a function to calculate the height of a binary tree.
14.Write a function to count the number of leaves in a binary tree.
15.Write a function to count the number of nodes at a given level in a binary
tree.
16.Write a function to find the maximum element in a binary tree.
17.Write a function to find the minimum element in a binary tree.

You might also like