Skip to content

Instantly share code, notes, and snippets.

@ravikiran0606
Created July 27, 2016 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravikiran0606/83a22a2927bd0e6752e09f90489832a1 to your computer and use it in GitHub Desktop.
Save ravikiran0606/83a22a2927bd0e6752e09f90489832a1 to your computer and use it in GitHub Desktop.
C++ program to create a stack class and implement its basic functions :
#include<iostream>
#define maxi 1000
using namespace std;
class sstack{
int a[maxi];
int top;
public:
void initialize();
void push(int val);
void pop();
void peep();
void display();
};
void sstack::initialize(){
cout<<"Empty Stack is created :D ";
top=-1;
}
void sstack::push(int val){
if(top==maxi-1){
cout<<"\nThe Stack is already full...";
cout<<"\nThe Stack is already full...";
}
a[++top]=val;
}
void sstack::pop(){
if(top==-1){
cout<<"\nThe Stack is already empty..";
}
else{
cout<<"\nThe Popped element is.."<<a[top];
top--;
}
}
void sstack::peep(){
if(top==-1){
cout<<"\nThe Stack is already empty..";
}
else{
cout<<"\nThe Topmost element is.."<<a[top];
}
}
void sstack::display(){
if(top==-1){
cout<<"\nThe Stack is Empty..";
}
else{
cout<<"\nThe Contents of the Stack is..";
int x=top;
while(x!=-1){
cout<<a[x]<<" ";
x--;
}
}
}
int main()
{
sstack s;
int ch,x,val;
s.initialize();
cout<<"\nChoice : \n1) Push \n2) Pop \n3) Peep \n4) Display \n5) Exit :P";
while(1){
cout<<"\nEnter your choice..";
cin>>ch;
if(ch==1){
cout<<"\nEnter the element to be inserted..";
cin>>val;
s.push(val);
}
else if(ch==2){
s.pop();
}
else if(ch==3){
s.peep();
}
else if(ch==4){
s.display();
}
else break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment