0% found this document useful (0 votes)
3 views

lab task DS

The document contains three tasks that implement stack data structures in C++. Each task demonstrates different stack implementations: the first uses an array, the second uses an array with a user menu, and the third uses a linked list. All implementations include basic stack operations such as push, pop, peek, size, and display.

Uploaded by

moeezaareej
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)
3 views

lab task DS

The document contains three tasks that implement stack data structures in C++. Each task demonstrates different stack implementations: the first uses an array, the second uses an array with a user menu, and the third uses a linked list. All implementations include basic stack operations such as push, pop, peek, size, and display.

Uploaded by

moeezaareej
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/ 9

Moeeza Areej

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');

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

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;
}

void push(int value) {


if (top < 99) {
arr[++top] = value;
cout << value << " pushed onto the stack." << endl;
} else {
cout << "Stack Overflow: Cannot push " << value << endl;
}
}

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 push(int element) {


Node* newNode = new Node(element);
newNode->next = topNode;
topNode = newNode;
count++;
cout << element << " pushed onto the stack." << endl;
}

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;
}

You might also like