0% found this document useful (0 votes)
30 views10 pages

Data Structure Lab 5 Name: Roll No: Section: Teacher: Task 1

Uploaded by

f223879
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)
30 views10 pages

Data Structure Lab 5 Name: Roll No: Section: Teacher: Task 1

Uploaded by

f223879
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/ 10

DATA STRUCTURE LAB 5

Name : Muhammad Asaad


Roll No : 22F-3879
Section : 3D
Teacher: Mam Rabia Anwar

Task 1:
#include <iostream>
using namespace std;

class IntStack {
private:
int stackArray[10]; // Array for stack elements
int top; // Index of top element in the stack

public:
IntStack() {
top = -1; // Initializing top to -1 for indicating an empty stack..
}

bool isEmpty() {
return top == -1;
}

bool isFull() {
return top == 9; // Since stack size is 10
}

// Adds new element at top of stack


void push(int element) {
if (isFull()) {
cout << "Stack Overflow! Cannot push element " << element << endl;
return;
}
stackArray[++top] = element;
}

// Removes the top element


int pop() {
if (isEmpty()) {
cout << "Stack Underflow! Cannot pop from an empty stack." << endl;
return -1; // Returning -1 to indicate underflow..
}
return stackArray[top--]; // Returning top element and decrementing top
}

// Returns top element of the stack


int peek() {
if (isEmpty()) {
cout << "Stack is empty." << endl;
return -1; // Returning -1 for an empty stack
}
return stackArray[top]; // Returns the top element
}
};

int main() {

IntStack stack;
stack.push(10);
stack.push(20);
stack.push(30);

cout << "Top element: " << stack.peek() << endl;


cout << "Popping element: " << stack.pop() << endl;
cout << "Top element after pop: " << stack.peek() << endl;
system("pause");
return 0;
}

Output:
Task 2:
#include <iostream>
#include <string>

using namespace std;

bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}

int getPrecedence(char op) {


if (op == '^')
return 3;
else if (op == '*' || op == '/')
return 2;
else if (op == '+' || op == '-')
return 1;
else
return -1; // Assuming all other characters are operands
}

string infixToPostfix(string expression) {


string postfix = "";
string stack = "";

for (char& c : expression) {


// If current character is an operand, we append it to postfix
if (isalnum(c)) {
postfix += c;
}
// If current character is (, we push it to the stack
else if (c == '(') {
stack.push_back(c);
}
// If current character is ), we pop and append operators from stack until ( is encountered
else if (c == ')') {
while (!stack.empty() && stack.back() != '(') {
postfix += stack.back();
stack.pop_back();
}
stack.pop_back(); // Popping '(' from the stack
}
// If current character is operator
else {
while (!stack.empty() && getPrecedence(stack.back()) >= getPrecedence(c)) {
postfix += stack.back();
stack.pop_back();
}
stack.push_back(c);
}
}

// Append remaining operators from the stack to postfix


while (!stack.empty()) {
postfix += stack.back();
stack.pop_back();
}

return postfix;
}

string postfixToInfix(string expression) {


string stack = "";

for (char& c : expression) {


// If current character is an operand, push it to the stack
if (isalnum(c)) {
stack = "(" + string(1, c) + ")" + stack;
}
// If current character is an operator
else {
string operand2 = stack.substr(1, stack.find(')') - 1);
stack = stack.substr(stack.find(')') + 1); // Remove the first operand from the stack
string operand1 = stack.substr(1, stack.find(')') - 1);
stack = stack.substr(stack.find(')') + 1); // Remove the second operand from the stack
string result = "(" + operand1 + c + operand2 + ")";
stack = result + stack;
}
}

return stack;
}

string prefixToPostfix(string expression) {


string stack = "";

// Iterating through the expression in reverse order


for (int i = expression.size() - 1; i >= 0; i--) {
// If current character is an operand, we push it to the stack
if (isalnum(expression[i])) {
stack = string(1, expression[i]) + stack;
}
// If current character is operator
else {
string operand1 = stack.substr(0, 1);
stack = stack.substr(1); // Removing the first operand from the stack
string operand2 = stack.substr(0, 1);
stack = stack.substr(1); // Removing the second operand from the stack
string result = operand1 + operand2 + expression[i];
stack = result + stack;
}
}

return stack;
}

int main() {
string infixExp = "a+b*(c^d-e)^(f+g*h)-I";
cout << "Infix expression: " << infixExp << endl;
string postfixExp = infixToPostfix(infixExp);
cout << "Postfix expression: " << postfixExp << endl;

string postfixExp2 = "abc++";


cout << "\nPostfix expression: " << postfixExp2 << endl;
string infixExp2 = postfixToInfix(postfixExp2);
cout << "Infix expression: " << infixExp2 << endl;

string prefixExp = "*+AB-CD";


cout << "\nPrefix expression: " << prefixExp << endl;
string postfixExp3 = prefixToPostfix(prefixExp);
cout << "Postfix expression: " << postfixExp3 << endl;

system("pause");
return 0;
}

Output:

Task 3:
#include <iostream>
using namespace std;

// Defining maximum size of the stack


const int MAX_SIZE = 100;

class Stack {
private:
char arr[MAX_SIZE];
int top;

public:
Stack() {
top = -1;
}

void push(char value) {


if (top >= MAX_SIZE - 1) {
cout << "Stack Overflow\n";
return;
}
arr[++top] = value;
}

char pop() {
if (top < 0) {
cout << "Stack Underflow\n";
return '\0';
}
return arr[top--];
}

bool isEmpty() {
return top < 0;
}

// For getting top element of stack


char peek() {
if (top < 0) {
cout << "Stack is empty\n";
return '\0';
}
return arr[top];
}

void reverse() {
int start = 0;
int end = top;
while (start < end) {
char temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

void display() {
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
};

int main() {
Stack stack;
char arr[] = { 'a', 'b', 'c', 'd', 'e' };
int size = sizeof(arr) / sizeof(arr[0]);

// Pushing elements onto the stack


for (int i = 0; i < size; i++) {
stack.push(arr[i]);
}
cout << "Original stack content: ";
stack.display();

stack.reverse();
cout << "Reversed stack content: ";
stack.display();

system("pause");
return 0;
}

Output:

Task 4:
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

class StackLL {
private:
Node* top;

public:
StackLL() {
top = nullptr;
}

void Push(int elem) {


Node* newNode = new Node;
newNode->data = elem;
newNode->next = top;
top = newNode;
}
int Pop() {
if (IsEmpty()) {
cout << "Stack Underflow\n";
return -1; // Assuming -1 represents an empty stack or error
}
int poppedValue = top->data;
Node* temp = top;
top = top->next;
delete temp;
return poppedValue;
}

bool IsEmpty() {
return top == nullptr;
}
};

int main() {
StackLL stack;

stack.Push(10);
stack.Push(20);
stack.Push(30);

cout << "Popped Element: " << stack.Pop() << endl;


cout << "Popped Element: " << stack.Pop() << endl;

// Checking if the stack is empty


cout << "Is stack empty? " << (stack.IsEmpty() ? "Yes" : "No") << endl;

system("pause");
return 0;
}

Output:

Task 5:
#include <iostream>
#include <string>

using namespace std;

struct Node {
char data;
Node* next;
};

class StackLL {
private:
Node* top;

public:
StackLL() {
top = nullptr;
}

void push(char elem) {


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

char pop() {
if (isEmpty()) {
cout << "Stack Underflow\n";
return '\0'; // Assuming '\0' represents an empty stack or error
}
char poppedValue = top->data;
Node* temp = top;
top = top->next;
delete temp;
return poppedValue;
}

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

bool isBalanced(string exp) {


StackLL stack;

for (char& c : exp) {


if (c == '(' || c == '{' || c == '[') {
stack.push(c);
}
else if (c == ')' || c == '}' || c == ']') {
if (stack.isEmpty()) {
return false; // Unmatched closing bracket
}
char top = stack.pop();
if ((c == ')' && top != '(') || (c == '}' && top != '{') || (c == ']' && top != '[')) {
return false; // Mismatched opening and closing brackets
}
}
}

return stack.isEmpty(); // If stack is empty, all brackets are matched


}

int main() {
cout << "Test Case 1: " << (isBalanced("{[{}{}]}[()]") ? "Balanced" : "Not Balanced")
<< endl;
cout << "Test Case 2: " << (isBalanced("{{}{}},") ? "Balanced" : "Not Balanced") <<
endl;
cout << "Test Case 3: " << (isBalanced("[]{}()") ? "Balanced" : "Not Balanced") << endl;
cout << "Test Case 4: " << (isBalanced("{()}[)") ? "Balanced" : "Not Balanced") << endl;
cout << "Test Case 5: " << (isBalanced("{(})") ? "Balanced" : "Not Balanced") << endl;

system("pause");
return 0;
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy