-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdaa796
commit 6cd2d3e
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include<iostream> | ||
#define MAX 50 | ||
using namespace std; | ||
|
||
class Stack | ||
{ | ||
int top; | ||
public: | ||
int a[MAX]; | ||
Stack() | ||
{ | ||
top=0; | ||
} | ||
bool push(int x); | ||
int pop(); | ||
bool isEmpty(); | ||
int peek(); | ||
void display(); | ||
}; | ||
|
||
|
||
bool Stack::push(int x) | ||
{ | ||
if(top>=MAX) | ||
{ | ||
cout<<"\n\nSTACK OVERFLOW!"; | ||
return 0; | ||
} | ||
else | ||
{ | ||
a[top++]=x; | ||
cout<<"\n\nSuccessfully Pushed : "<<x; | ||
return 1; | ||
} | ||
} | ||
|
||
int Stack::pop() | ||
{ | ||
if(top<=0) | ||
{ | ||
cout<<"\n\nSTACK UNDERFLOW!"; | ||
return 0; | ||
} | ||
else | ||
{ | ||
//cout<<"\n\nSuccessfully Pop : "<<a[top-1]; | ||
int x=a[top-1]; | ||
--top; | ||
return x; | ||
} | ||
} | ||
|
||
bool Stack::isEmpty() | ||
{ | ||
return(!top); | ||
} | ||
|
||
int Stack::peek() | ||
{ | ||
if(top==0) | ||
{ | ||
cout<<"\n\nSTACK IS EMPTY! "; | ||
return -1; | ||
} | ||
else | ||
{ | ||
return(a[top-1]); | ||
} | ||
} | ||
|
||
void Stack::display() | ||
{ | ||
if(top<=0) | ||
{ | ||
cout<<"\n\nSTACK IS EMPTY! "; | ||
} | ||
else | ||
{ | ||
cout<<"\n\nCurrent Stack : "; | ||
for(int i=0;i<top;++i) | ||
cout<<a[i]<<" "; | ||
} | ||
|
||
} | ||
|
||
int main() | ||
{ | ||
Stack s; | ||
int ch=1; | ||
cout<<"1 to push\n\n2 to pop\n\n3 to display\n\n4 to get top element\n\n5 to check if stack empty\n\n"; | ||
while(choice) | ||
{ | ||
cout<<"\n\nEnter choice: "; | ||
cin>>ch; | ||
if(ch==1) | ||
{ | ||
int ele; | ||
cout<<"\nEnter element to push: "; | ||
cin>>ele; | ||
s.push(ele); | ||
} | ||
else if(ch==2) | ||
{ | ||
cout<<"\n"; | ||
cout<<s.pop()<<" is popped from stack"; | ||
} | ||
else if(ch==3) | ||
{ | ||
s.display(); | ||
} | ||
else if(ch==4) | ||
{ | ||
cout<<"\n"; | ||
if(s.peek()!=-1) | ||
cout<<s.peek()<<" is on top"; | ||
} | ||
else if(ch==5) | ||
{ | ||
s.isEmpty()?cout<<"\nStack is empty!":cout<<"\nStack is not empty!"; | ||
} | ||
} | ||
} |