Skip to content

Instantly share code, notes, and snippets.

@susanhsrestha
Created January 23, 2020 12:44
Show Gist options
  • Save susanhsrestha/00fb4c057c902a873719194c52bf5d83 to your computer and use it in GitHub Desktop.
Save susanhsrestha/00fb4c057c902a873719194c52bf5d83 to your computer and use it in GitHub Desktop.
This program implemented in C++ provides the Stack ADT in array implementation.
#include<iostream>
#include<conio.h>
#define size 10
using namespace std;
class Stack{
int a[size], top;
public: Stack(){
top = -1;
}
void push(int x){
if(top == size-1){
cout<<"Stack Overflow"<<endl;
return;
}
a[++top] = x;
display();
}
void pop(){
if(top == -1){
cout<<"Stack Underflow"<<endl;
return;
}
cout<<"Deleted Element is "<<a[top]<<endl;
top = top-1;
display();
}
int front(){
return a[top];
}
void display(){
cout<<"Stack is: ";
if(top == -1){
cout<<" Empty"<<endl;
return;
}
for(int i=0; i<=top; i++)
cout<<a[i]<<" ";
cout<<endl;
}
// also a function to check if a stack is empty or not
};
int main(){
Stack s;
s.push(4);
s.pop();
s.push(5);
s.push(0);
s.push(66);
s.pop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment