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

Dsa Practica - 2: Tejas Gitkhane Roll No.: S511042

Uploaded by

jaywareg003
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 views6 pages

Dsa Practica - 2: Tejas Gitkhane Roll No.: S511042

Uploaded by

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

Name : Tejas Gitkhane

Roll no. : S511042

Class : SE

Div. :A

Batch : A2

DSA PRACTICAL-2
IMPLIMENT STACK AS AN ABSTARACT DATATYPE AND USE THIS ADT FOR
CONVERSION OF INFIX TO POSTFIX, PREFIX AND EVALUATION OF POSTFIX AND
PREFIX EXPRESSION

#include <iostream>

using namespace std;

class Stack {

private:

int maxSize;

int *stackArray;

int top;

public:

Stack(int size) {

maxSize = size;

stackArray = new int[maxSize];

top = -1;

~Stack() {

delete[] stackArray;

}
void push(int value) {

if (top == maxSize - 1) {

cout << "Stack is full. Cannot push " << value << endl;

} else {

stackArray[++top] = value;

cout << "Pushed " << value << endl;

void pop() {

if (top == -1) {

cout << "Stack is empty. Cannot pop." << endl;

} else {

int value = stackArray[top--];

cout << "Popped " << value << endl;

void display() {

if (top == -1) {

cout << "Stack is empty." << endl;

} else {

cout << "Stack elements: ";

for (int i = 0; i <= top; i++) {

cout << stackArray[i] << " ";

cout << endl;

};
int main() {

int choice, value;

Stack stack(5); // Stack with maximum size 5

do {

cout << "\nChoose an operation:\n";

cout << "1. Push\n";

cout << "2. Pop\n";

cout << "3. Display Stack\n";

cout << "4. 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:

stack.display();

break;

case 4:

cout << "Exiting..." << endl;

break;

default:

cout << "Invalid choice. Please choose again." << endl;


}

} while (choice != 4);

return 0;

}
Output :

You might also like