You are on page 1of 3

Bangladesh University of Business and Technology

Lab Report: 7

Dept. name: Computer Science and Engineering

Course name: Data Structure Lab

Course code: CSE 232

Submitted By: Rafsan Samin


22234103208

Submitted To: Md. Mahbub-Or-Rashid


Assistant professor,
Dept. of Computer Science and Engineering

Submission Date: 11-12-2023


Stack Implementation - Push Pop Operation

#include <iostream>
#include <vector>

class Stack {
private:
std::vector<int> elements;

public:
void push(int value) {
elements.push_back(value);
}

int pop() {
if (isEmpty()) {
std::cerr << "Error: Stack is empty." << std::endl;
return -1;
}

int top = elements.back();


elements.pop_back();
return top;
}

bool isEmpty() {
return elements.empty();
}
};

int main() {
Stack myStack;

myStack.push(1);
myStack.push(2);
myStack.push(3);

while (!myStack.isEmpty()) {
int top = myStack.pop();
std::cout << "Popped: " << top << std::endl;
}
return 0;
}

Output:
Popped: 3
Popped: 2
Popped: 1

You might also like