Created
January 23, 2020 12:44
-
-
Save susanhsrestha/00fb4c057c902a873719194c52bf5d83 to your computer and use it in GitHub Desktop.
This program implemented in C++ provides the Stack ADT in array implementation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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