Assignment Name Student Name Class ID
Data Structures Lab Muhammad Ehtisham Amjad BSSE-IV-B
Faculty Name Ms Quratulain Zahid Lab Tasks 6
Lab Task 1
Understand and Execute code given in lab manual.
Explanation / Understanding
The first code implements a stack using an array in C++. It defines a fixed-size array
(MAX = 5) and a top pointer to keep track of the stack's current position. The push
function adds an element to the stack if it's not full, while the pop function removes the
top element if the stack is not empty. The display function prints all elements of the stack
from bottom to top. The main function demonstrates these operations by pushing values
(10, 20, 30), displaying the stack, popping an element, and displaying it again.
The second code implements a stack using a linked list in C++. Instead of using a fixed-
size array, it dynamically allocates memory for each stack element using a Node structure.
The push function creates a new node, assigns it a value, and links it to the previous top
node. The pop function removes the top node and updates the top pointer to the next
node. The display function iterates through the linked list to print all stack elements. The
main function tests these operations by pushing values (10, 20), displaying the stack,
popping an element, and displaying the updated stack.
Execution
Lab Task 2
#include <iostream>
using namespace std;
// Template class for Stack
template <typename T>
class Stack {
private:
static const int MAX = 10; // Define maximum size of stack
T arr[MAX]; // Array to store stack elements
int top; // Index of top element
public:
// Constructor to initialize stack
Stack() { top = -1; }
// Push function to add elements
void push(T value) {
if (top == MAX - 1) {
cout << "Stack Overflow\n";
return;
}
arr[++top] = value;
cout << "Pushed: " << value << endl;
}
// Pop function to remove elements
void pop() {
if (top == -1) {
cout << "Stack Underflow\n";
return;
}
cout << "Popped: " << arr[top--] << endl;
}
// Peek function to get top element
T peek() {
if (top == -1) {
throw runtime_error("Stack is empty!");
}
return arr[top];
}
// Function to check if stack is empty
bool isEmpty() {
return (top == -1);
}
// Display stack elements
void display() {
if (top == -1) {
cout << "Stack is empty\n";
return;
}
cout << "Stack elements: ";
for (int i = 0; i <= top; i++)
cout << arr[i] << " ";
cout << endl;
}
};
int main() {
// Stack for integers
Stack<int> intStack;
intStack.push(10);
intStack.push(20);
intStack.push(30);
intStack.display();
intStack.pop();
intStack.display();
// Stack for floating point numbers
Stack<float> floatStack;
floatStack.push(1.5);
floatStack.push(2.7);
floatStack.display();
// Stack for characters
Stack<char> charStack;
charStack.push('A');
charStack.push('B');
charStack.display();
charStack.pop();
return 0;
}
Execution
Lab Task 3
#include <iostream>
#include <stack>
using namespace std;
// Function to check if brackets are balanced
bool isBalanced(string expr) {
stack<char> s;
for (char ch : expr) {
// Push opening brackets onto stack
if (ch == '(' || ch == '{' || ch == '[') {
s.push(ch);
}
// Check for matching closing brackets
else if (ch == ')' || ch == '}' || ch == ']') {
if (s.empty()) return false; // No matching opening bracket
char top = s.top();
s.pop();
// Check for correct matching
if ((ch == ')' && top != '(') ||
(ch == '}' && top != '{') ||
(ch == ']' && top != '[')) {
return false;
}
}
}
// If stack is empty, parentheses are balanced
return s.empty();
}
int main() {
string expr;
cout << "Enter a string of parentheses: ";
cin >> expr;
if (isBalanced(expr))
cout << "The parentheses are balanced.\n";
else
cout << "The parentheses are NOT balanced.\n";
return 0;
}
Execution
Lab Task 4
#include <iostream>
#include <stack>
using namespace std;
// Function to reverse a string using a stack
string reverseString(string str) {
stack<char> s;
// Push all characters onto stack
for (char ch : str) {
s.push(ch);
}
// Pop characters to reverse the string
string reversed = "";
while (!s.empty()) {
reversed += s.top();
s.pop();
}
return reversed;
}
int main() {
string input;
cout << "Enter a string: ";
cin >> input;
string reversed = reverseString(input);
cout << "Reversed string: " << reversed << endl;
return 0;
}
Execution