Stack Data Structure Implementation in C++, Lab Reports of Data Structures and Algorithms

ABOUT THE STACK AND THEIR PROPERTY. hOW TO INSERT ,DELETE THE THE STACK QUEQE

Typology: Lab Reports

2020/2021

Uploaded on 05/23/2021

samrat-k-c
samrat-k-c 🇺🇸

2 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Stack
#include <iostream>
using namespace std;
class Stack
{
int *base;
int top;
int total_capacity;
public:
Stack(int size);
void push(int x);
void pop();
int peek();
int size();
bool isEmpty();
bool isFull();
};
void printMenu();
int main()
{
int num, ch;
cout << "Enter the size of stack: ";
cin >> num;
Stack stk(num);
pf3
pf4
pf5

Partial preview of the text

Download Stack Data Structure Implementation in C++ and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Stack #include using namespace std; class Stack { int *base; int top; int total_capacity; public: Stack(int size); void push(int x); void pop(); int peek(); int size(); bool isEmpty(); bool isFull(); }; void printMenu(); int main() { int num, ch; cout << "Enter the size of stack: "; cin >> num; Stack stk(num);

cout << "Stack size is: " << stk.size() << endl; bool loop = true; while(loop){ printMenu(); cout << "Enter your choice" << endl; cin >> ch; switch(ch){ case 1: // Push cout << "Enter the element to push: "; cin >> num; stk.push(num); cout << "Stack size is: " << stk.size() << endl; break; case 2: // Pop stk.pop(); cout << "Stack size is: " << stk.size() << endl; break; case 3: // Peek num = stk.peek(); cout << "Peeking : " << num << endl; break; case 4:

// at first top is pointing nothing top = - 1; } void Stack::push(int n){ // Check whether the stack is full if(isFull()){ cout << "Cannot push new element. Stack full" << endl; return; } top++; *(base + top) = n; cout << "New element has been pushed successfully" << endl; } void Stack::pop(){ if(isEmpty()){ cout << "Cannot pop element. Stack empty" << endl; return; } int poppedValue = *(base + top); top--; cout << poppedValue << " popped." << endl; }

bool Stack::isFull(){ if(size() == total_capacity){ return true; } return false; } int Stack::size(){ return top + 1; } bool Stack::isEmpty(){ if(size() < 1){ return true; } return false; } int Stack::peek(){ if(isEmpty()){ cout << "Cannot peek. Stack empty." << endl; } return *(base + top); }