lab task DS
lab task DS
S2024105053
TASK#1
#include <iostream>
using namespace std;
class CharStack {
private:
char arr[100];
int top;
public:
CharStack() {
top = -1;
}
void push(char c) {
if (top < 99) {
arr[++top] = c;
} else {
cout << "Stack Overflow: Cannot push " << c << endl;
}
}
void pop() {
if (top >= 0) {
top--;
} else {
cout << "Stack Underflow: Cannot pop from an empty stack" <<
endl;
}
}
char displayTop() {
if (top >= 0) {
return arr[top];
} else {
cout << "Stack is empty" << endl;
return '\0';
}
}
int size() {
return top + 1;
}
bool isEmpty() {
return top < 0;
}
void displayStack() {
if (top < 0) {
cout << "Stack is empty" << endl;
return;
}
cout << "Stack elements from top to bottom: ";
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main() {
CharStack stack;
stack.push('A');
stack.push('B');
stack.push('C');
stack.push('D');
stack.push('E');
stack.push('F');
stack.push('G');
stack.push('H');
stack.pop();
stack.pop();
cout << "Top element after popping: " << stack.displayTop() << endl;
cout << "Current size of the stack: " << stack.size() << endl;
cout << "Stack is " << (stack.isEmpty() ? "empty" : "not empty") <<
endl;
stack.displayStack();
return 0;
}
TASK#2
#include <iostream>
using namespace std;
class Stack {
private:
int arr[100];
int top;
public:
Stack() {
top = -1;
}
int pop() {
if (top >= 0) {
return arr[top--];
} else {
cout << "Stack Underflow: Cannot pop from an empty stack" <<
endl;
return -1;
}
}
int peek() {
if (top >= 0) {
return arr[top];
} else {
cout << "Stack is empty" << endl;
return -1;
}
}
bool isEmpty() {
return top < 0;
}
int size() {
return top + 1;
}
void display() {
if (top < 0) {
cout << "Stack is empty" << endl;
return;
}
cout << "Stack elements from top to bottom: ";
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main() {
Stack stack;
int choice, value;
do {
cout << "\nMenu:\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Peek\n";
cout << "4. IsEmpty\n";
cout << "5. Size\n";
cout << "6. Display\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
value = stack.pop();
if (value != -1) {
cout << value << " popped from the stack." << endl;
}
break;
case 3:
value = stack.peek();
if (value != -1) {
cout << "Top element is: " << value << endl;
}
break;
case 4:
cout << "Stack is " << (stack.isEmpty() ? "empty" : "not empty")
<< endl;
break;
case 5:
cout << "Current size of the stack: " << stack.size() << endl;
break;
case 6:
stack.display();
break;
case 7:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 7);
return 0;
}
TASK#3
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
class Stack {
private:
Node* topNode;
int count;
public:
Stack() {
topNode = nullptr;
count = 0;
}
int size() {
return count;
}
bool isEmpty() {
return count == 0;
}
int top() {
if (topNode != nullptr) {
return topNode->data;
} else {
cout << "Stack is empty" << endl;
return -1;
}
}
void pop() {
if (topNode != nullptr) {
Node* temp = topNode;
topNode = topNode->next;
delete temp;
count--;
cout << "Top element popped from the stack." << endl;
} else {
cout << "Stack Underflow: Cannot pop from an empty stack" <<
endl;
}
}
void display() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return;
}
cout << "Stack elements from top to bottom: ";
Node* current = topNode;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
};
int main() {
Stack stack;
int choice, value;
do {
cout << "\nMenu:\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Top\n";
cout << "4. IsEmpty\n";
cout << "5. Size\n";
cout << "6. Display\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
value = stack.top();
if (value != -1) {
cout << "Top element is: " << value << endl;
}
break;
case 4:
cout << "Stack is " << (stack.isEmpty() ? "empty" : "not empty")
<< endl;
break;
case 5:
cout << "Current size of the stack: " << stack.size() << endl;
break;
case 6:
stack.display();
break;
case 7:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 7);
return 0;
}