Dsa Lab Manual
Dsa Lab Manual
ALGORITHMS LAB
LAB MANUAL
YASHALL FALLAK
BSCS BATCH 18
BSCS 07
LAB INSTRUCTOR: JAVERIA JALIL
KNOWLEDGE UNIT OF SYSTEMS AND
TECHNOLOGY
UMT SIALKOT
Contents
QUESTION#1.............................................................................................................. 2
INPUT:..................................................................................................................... 3
OUTPUT:.................................................................................................................. 9
QUESTION#2......................................................................................................... 14
INPUT:................................................................................................................... 14
OUTPUT:................................................................................................................ 18
QUESTION #3........................................................................................................ 21
INPUT:................................................................................................................... 22
OUTPUT:................................................................................................................ 24
QUESTION #4........................................................................................................... 24
INPUT:................................................................................................................... 24
OUTPUT:................................................................................................................ 29
QUESTION#1............................................................................................................ 32
INPUT:................................................................................................................... 32
OUTPUT:................................................................................................................ 35
QUESTION #2........................................................................................................... 36
INPUT:................................................................................................................... 36
OUTPUT:................................................................................................................ 39
QUESTION #3........................................................................................................... 40
INPUT:................................................................................................................... 40
OUTPUT:................................................................................................................ 44
MANUAL #1
QUESTION#1
1. Implement a stack using an array in C++ with the following
functions:
• push(): Adds an item to the top of the stack.
• pop(): Removes and returns the top item from the stack. •
traverse(): Displays all items in the stack.
INPUT:
#include <iostream>
#include <algorithm>
#include <vector>
class Stack {
private:
int arr[capacity];
int top;
public:
Stack() : top(-1) {}
return;
}
arr[++top] = item;
int pop() {
if (top < 0) {
return -1;
return arr[top--];
if (top < 0) {
return;
if (top < 0) {
return;
}
if (!ascending) {
vector<int> indexes;
if (arr[i] == item) {
indexes.push_back(i);
if (!indexes.empty()) {
cout << "Value " << item << " found at index(es): ";
} else {
cout << "Value " << item << " not found in stack.\n";
return;
minValue = arr[i];
cout << "Minimum value in stack: " << minValue << endl;
if (top < 0) {
return;
maxValue = arr[i];
cout << "Maximum value in stack: " << maxValue << endl;
};
int main() {
Stack stack;
do {
switch (choice) {
case 1:
stack.push(value);
break;
case 2:
break;
case 3:
stack.traverse();
break;
case 4:
stack.sort(true);
break;
case 5:
stack.sort(false);
break;
case 6:
stack.search(value);
break;
case 7:
stack.minVal();
break;
case 8:
stack.maxVal();
break;
case 9:
break;
default:
return 0;
}
OUTPUT:
OPTION 1
OPTION 2
OPTION 3
OPTION 4
OPTION 5
OPTION 6
OPTION 7 AND 8
OPTION 9
QUESTION#2
2. Write a function to identify and display any duplicate data in the
stack along with their indices.
INPUT:
#include <iostream>
#include <unordered_map>
#include <vector>
class Stack {
private:
int arr[capacity];
int top;
public:
Stack() : top(-1) {}
return;
arr[++top] = item;
}
int pop() {
if (top < 0) {
return -1;
return arr[top--];
if (top < 0) {
return;
indices[arr[i]].push_back(i);
}
bool hasDuplicates = false;
if (entry.second.size() > 1) {
hasDuplicates = true;
cout << "Value " << entry.first << " found at indices: ";
if (!hasDuplicates) {
};
int main() {
Stack stack;
do {
switch (choice) {
case 1:
stack.push(value);
break;
case 2:
break;
case 3:
stack.traverse();
break;
case 4:
stack.findDuplicates();
break;
case 5:
break;
default:
return 0;
}
OUTPUT:
OPTION 1
OPTION 2
OPTION 3
OPTION 4
OPTION 5
QUESTION #3
3. Write a C++ program that:
INPUT:
#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
void displayStack(stack<int> s) {
while (!s.empty()) {
s.pop();
int main() {
stack<int> inputStack;
stack<int> sortedStack;
vector<int> elements;
int n, value;
cin >> n;
cout << "Enter " << n << " elements:\n";
inputStack.push(value);
elements.push_back(value);
sort(elements.begin(), elements.end());
sortedStack.push(elements[i]);
displayStack(inputStack);
displayStack(sortedStack);
return 0;
}
OUTPUT:
QUESTION #4
4. Write a C++ program that simulates three real-world scenarios
where stacks are useful. Implement each scenario using stack
operations.
INPUT:
#include <iostream>
#include <stack>
#include <string>
void plateStack() {
stack<string> plates;
cout << "\nPlate Stack - Enter 'add <plate>', 'remove', or 'exit': ";
getline(cin, command);
plate = command.substr(4);
plates.push(plate);
if (!plates.empty()) {
plates.pop();
} else {
} else {
void browserHistory() {
stack<string> history;
string url, command;
while (true) {
getline(cin, command);
if (command == "back") {
if (!history.empty()) {
cout << "Going back to: " << history.top() << endl;
history.pop();
} else {
} else {
history.push(command);
void undoFunction() {
stack<string> actions;
string command;
while (true) {
getline(cin, command);
if (command == "undo") {
if (!actions.empty()) {
actions.pop();
} else {
} else {
actions.push(command);
int main() {
int choice;
while (true) {
switch (choice) {
case 1:
plateStack();
break;
case 2:
browserHistory();
break;
case 3:
undoFunction();
break;
case 4:
return 0;
default:
return 0;
}
OUTPUT:
OPTION 1
OPTION 2
OPTION 3
OPTION 4
THE END!
MANUAL #2
QUESTION#1
1.Write code for converting an infix expression
to a postfix expression using a stack. This
program will prompt the user to enter an infix
expression.
INPUT:
#include <iostream>
#include <stack>
int prec(char c) {
if (c == '^') return 3;
return -1;
stack<char> st;
for (char c : s) {
if (c == '(') {
st.push(c);
} else if (c == ')') {
return st.empty();
string infixtopostfix(string s) {
stack<char> st;
string res;
if (isalpha(s[i])) {
res += s[i];
st.push(s[i]);
res += st.top();
st.pop();
if (!st.empty()) {
st.pop();
} else {
res += st.top();
st.pop();
st.push(s[i]);
while (!st.empty()) {
res += st.top();
st.pop();
return res;
int main() {
string expression;
char choice;
do {
getline(cin, expression);
if (!areParenthesesBalanced(expression)) {
cout << "Error: The expression contains unbalanced parentheses." << endl;
} else {
cout << "Do you want to convert another expression? (y/n): ";
cin.ignore();
return 0;
OUTPUT:
DISPLAYING ERROR IF PARANTHESIS ARE UNBALANCED
#include <stack>
#include <algorithm>
int prec(char c) {
if (c == '^') return 3;
return -1;
stack<char> st;
for (char c : s) {
if (c == '(') {
st.push(c);
} else if (c == ')') {
st.pop();
}
return st.empty();
string infixtoprefix(string s) {
reverse(s.begin(), s.end());
stack<char> st;
string res;
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) {
res += s[i];
st.push(s[i]);
res += st.top();
st.pop();
if (!st.empty()) {
} else {
res += st.top();
st.pop();
st.push(s[i]);
while (!st.empty()) {
res += st.top();
st.pop();
reverse(res.begin(), res.end());
return res;
int main() {
string expression;
char choice;
do {
getline(cin, expression);
if (!areParenthesesBalanced(expression)) {
cout << "Error: The expression contains unbalanced parentheses." << endl;
} else {
string prefix = infixtoprefix(expression);
cout << "Do you want to convert another expression? (y/n): ";
cin.ignore();
return 0;
OUTPUT:
DISPLAYING ERROR AS THE PARANTHESIS ARE UNBALANCED
INPUT:
#include <iostream>
#include <stack>
#include <algorithm>
int prec(char c) {
if (c == '!') return 1;
return -1;
stack<char> st;
for (char c : s) {
if (c == '(') {
st.push(c);
} else if (c == ')') {
st.pop();
return st.empty();
string infixtoprefix(string s) {
reverse(s.begin(), s.end());
stack<char> st;
string res;
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) {
res += s[i];
st.push(s[i]);
res += st.top();
st.pop();
}
if (!st.empty()) {
st.pop();
} else {
res += st.top();
st.pop();
st.push(s[i]);
while (!st.empty()) {
res += st.top();
st.pop();
reverse(res.begin(), res.end());
return res;
int main() {
string expression;
char choice;
do {
cout << "Enter an infix expression: ";
getline(cin, expression);
if (!areParenthesesBalanced(expression)) {
cout << "Error: The expression contains unbalanced parentheses." << endl;
} else {
cout << "Do you want to convert another expression? (y/n): ";
cin.ignore();
return 0;
OUTPUT:
THE END!