Data Structure Lab 5 Name: Roll No: Section: Teacher: Task 1
Data Structure Lab 5 Name: Roll No: Section: Teacher: Task 1
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
}
int main() {
IntStack stack;
stack.push(10);
stack.push(20);
stack.push(30);
Output:
Task 2:
#include <iostream>
#include <string>
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}
return postfix;
}
return 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;
system("pause");
return 0;
}
Output:
Task 3:
#include <iostream>
using namespace std;
class Stack {
private:
char arr[MAX_SIZE];
int top;
public:
Stack() {
top = -1;
}
char pop() {
if (top < 0) {
cout << "Stack Underflow\n";
return '\0';
}
return arr[top--];
}
bool isEmpty() {
return top < 0;
}
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]);
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;
}
bool IsEmpty() {
return top == nullptr;
}
};
int main() {
StackLL stack;
stack.Push(10);
stack.Push(20);
stack.Push(30);
system("pause");
return 0;
}
Output:
Task 5:
#include <iostream>
#include <string>
struct Node {
char data;
Node* next;
};
class StackLL {
private:
Node* top;
public:
StackLL() {
top = nullptr;
}
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;
}
};
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: