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

CS301P-Assignment 2 Solution Fall 2024 by M.junaid Qazi

Uploaded by

yousafalim775
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)
198 views6 pages

CS301P-Assignment 2 Solution Fall 2024 by M.junaid Qazi

Uploaded by

yousafalim775
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/ 6

CS301P-Data Structure Total marks = 20

Assignment No.2 Solution Deadline


Semester: Fall 2024 th
22 of NOV 2024

Please carefully read the following instructions before attempting the assignment Solution.

NOTE
Don't copy-paste the same answer.
Make sure you can make some changes to your solution file before submitting copy
paste solution will be marked zero.
If you found any mistake then correct yourself and inform me.
Before submitting an Assignment GDB checks your assignment requirement file.

For any query, feel free to Contact at


WhatsApp: +923074960034

Provide by M.JUNAID QAZI

Code:
#include <iostream>
using namespace std;

// Node class to represent each element in the stack


class Node {
public:
char data;
Node* next;

Node(char data) : data(data), next(NULL) {}


};

CONTACT ON WHATSAPP
+923074960034
// Stack class for stack operations
class Stack {
private:
Node* top;

public:
Stack() : top(NULL) {}

~Stack() {
while (!isEmpty()) {
pop();
}
}

void push(char data) {


Node* newNode = new Node(data);
newNode->next = top;
top = newNode;
}

void pop() {
if (isEmpty()) {
cout << "Stack is empty, cannot pop." << endl;
return;
}
Node* temp = top;
top = top->next;
delete temp;

CONTACT ON WHATSAPP
+923074960034
}

char peek() {
if (isEmpty()) {
cout << "Stack is empty." << endl;
return '\0'; // Return a null character if stack is empty
}
return top->data;
}

bool isEmpty() {
return top == NULL;
}
};

// Non-member function to check if brackets are balanced


bool isBalanced(const string& str) {
Stack stack;

for (int i = 0; i < str.length(); i++) {


char ch = str[i];

// Check for invalid input


if (ch != '(' && ch != ')' && ch != '{' && ch != '}' && ch != '[' && ch != ']') {
cout << "Wrong input: only brackets are allowed." << endl;
return false;
}

// Push opening brackets onto the stack

CONTACT ON WHATSAPP
+923074960034
if (ch == '(' || ch == '{' || ch == '[') {
stack.push(ch);
} else {
// If stack is empty and a closing bracket is found
if (stack.isEmpty()) {
cout << "Brackets are not balanced." << endl;
return false;
}

// Check for matching brackets


char top = stack.peek();
if ((ch == ')' && top == '(') ||
(ch == '}' && top == '{') ||
(ch == ']' && top == '[')) {
stack.pop();
} else {
cout << "Brackets are not balanced." << endl;
return false;
}
}
}

// If stack is empty at the end, brackets are balanced


if (stack.isEmpty()) {
cout << "The brackets are balanced." << endl;
return true;
} else {
cout << "Brackets are not balanced." << endl;
return false;

CONTACT ON WHATSAPP
+923074960034
}
}

int main() {
string input;
cout << "=========================" << endl;
cout << "My VUID is BC12345678" << endl;
cout << "=================================" << endl;
cout << "Enter a string of brackets: ";
cin >> input;
isBalanced(input);
return 0;
}

OUTPUT:

CONTACT ON WHATSAPP
+923074960034
Every Assignment/GDB is change due to unique Student ID so don’t copy
That is truly perfect step by step idea solution get help easily.

Wish you the very best of Luck!

CONTACT ON WHATSAPP
+923074960034

You might also like