Stack are the data structures, working on the principle of Last In First Out. In this C++ program, the stack class is implemented without using the container.
#include<iostream>
using namespace std;
class Stack{
private:
int* stk;
int size;
int top;
public:
Stack(int);
void push(int);
void pop();
void stkSize();
bool isFull();
bool isEmpty();
void traverse();
};
Stack::Stack(int s){
size= s;
top = -1;
stk = new int[s];
}
void Stack::push(int i){
if(isFull()){
cout<<"Stack is Full"<<endl;
}
else{
top++;
stk[top] = i;
}
}
void Stack::pop(){
if(isEmpty()){
cout<<"Stack is empty"<<endl;
}
else{
top--;
}
}
bool Stack::isFull(){
if(top==size-1) return true;
else return false;
}
bool Stack::isEmpty(){
if(top==-1)return true;
else return false;
}
void Stack::traverse(){
if(isEmpty()){}
else{
cout<<"The elements are ";
for(int i=0;i<=top;i++){
cout<<stk[i]<<" ";
}
cout<<endl;
}
}
int main(){
Stack s(10);
s.push(32);
s.push(25);
s.push(10);
s.traverse();
s.push(20);
s.traverse();
s.pop();
s.traverse();
s.pop();
s.traverse();
return 0;
}
