Skip to content

Instantly share code, notes, and snippets.

@rahuladream
Created October 9, 2017 23:14
Show Gist options
  • Select an option

  • Save rahuladream/ebb315765ff79a387aad05af035014d3 to your computer and use it in GitHub Desktop.

Select an option

Save rahuladream/ebb315765ff79a387aad05af035014d3 to your computer and use it in GitHub Desktop.
Stack Implementation
#include <bits/stdc++.h>
using namespace std;
int max,i,n;
int top=-1;
class stack
{
int s[20];
public:
void push();
void pop();
void display();
};
void stack::push()
{
int x;
if(top==(max-1))
cout<<"Stack is full";
else
{
cout<<"enter data:";
cin>>x;
top=top+1;
s[top]=x;
}
}
void stack::pop()
{
if(top==-1)
cout<<"stack is empty";
else
s[top--]='\0';
}
void stack::display()
{
cout<<"displaying the contents of stack:\n";
if(top==-1)
cout<<"stack is empty"<<"\n";
else
{
for(i=0;i<=top;i++)
cout<<s[i]<<"\t";
cout<<"\n";
}
}
void main()
{
stack sa;
clrscr();
cout<<"enter the range:";
cin>>max;
while(1)
{
cout<<"\n1.push\n2.pop\n3.display\n4.exit";
cout<<"\n enter ur choice:";
cin>>n;
switch(n)
{
case 1:
sa.push();
break;
case 2:
sa.pop();
break;
case 3:
sa.display();
break;
case 4:
exit(0);
default:
cout<<"Invalid option";
}
}
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment