Created
March 3, 2016 18:17
-
-
Save SohanChy/50b500349f222f4b3785 to your computer and use it in GitHub Desktop.
Two stacks in same array
This file contains hidden or 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> | |
| using namespace std; | |
| const int N = 10; | |
| class twinStack | |
| { | |
| int arr[10]; | |
| int firstStackTop = -1; | |
| int secondStackTop = 10; | |
| public: | |
| bool twinFull() | |
| { | |
| if(firstStackTop<9 && secondStackTop>=0) | |
| { | |
| if(secondStackTop > firstStackTop && (secondStackTop - firstStackTop) > 1) | |
| { | |
| return false; | |
| } | |
| else if(firstStackTop > secondStackTop && (firstStackTop - secondStackTop) > 1) | |
| { | |
| return false; | |
| } | |
| else return true; | |
| } | |
| else return true; | |
| } | |
| bool isEmptyFirst() | |
| { | |
| if(firstStackTop == -1) | |
| { | |
| return true; | |
| } | |
| else return false; | |
| } | |
| bool isEmptySecond() | |
| { | |
| if(secondStackTop > 9) | |
| { | |
| return true; | |
| } | |
| else return false; | |
| } | |
| void pushFirst(int value) | |
| { | |
| if(twinFull() == false) | |
| { | |
| firstStackTop++; | |
| arr[firstStackTop] = value; | |
| } | |
| else cout<<"Stack is full, OVERFLOW!"<<endl; | |
| } | |
| void pushSecond(int value) | |
| { | |
| if(twinFull() == false) | |
| { | |
| secondStackTop--; | |
| arr[secondStackTop] = value; | |
| } | |
| else cout<<"Stack is full, OVERFLOW!"<<endl; | |
| } | |
| void popFirst() | |
| { | |
| if(isEmptyFirst() == false) | |
| { | |
| firstStackTop--; | |
| } | |
| else cout<<"Stack is Empty, UNDERFLOW!"<<endl; | |
| } | |
| void popSecond() | |
| { | |
| if(isEmptySecond() == false) | |
| { | |
| secondStackTop++; | |
| } | |
| else cout<<"Stack is Empty, UNDERFLOW!"<<endl; | |
| } | |
| int topFirst() | |
| { | |
| if(isEmptyFirst() != true) | |
| { | |
| return arr[firstStackTop]; | |
| } | |
| else | |
| { | |
| cout<<"First Stack is Empty"; | |
| return -1; | |
| } | |
| } | |
| int topSecond() | |
| { | |
| if(isEmptySecond() != true) | |
| { | |
| return arr[secondStackTop]; | |
| } | |
| else | |
| { | |
| cout<<"Second Stack is Empty"; | |
| return -1; | |
| } | |
| } | |
| }; | |
| int main() | |
| { | |
| twinStack test; | |
| for(int i=0; i<10; i++) | |
| { | |
| if(i%2 == 0) | |
| { | |
| test.pushFirst(i); | |
| } | |
| else test.pushSecond(i); | |
| } | |
| cout<<test.topFirst()<<endl; | |
| test.popFirst(); | |
| cout<<test.topFirst()<<endl; | |
| test.popFirst(); | |
| cout<<test.topFirst()<<endl; | |
| test.popFirst(); | |
| cout<<test.topFirst()<<endl; | |
| test.popFirst(); | |
| cout<<test.topFirst()<<endl; | |
| test.popFirst(); | |
| cout<<test.topFirst()<<endl; | |
| test.popFirst(); | |
| cout<<test.topSecond()<<endl; | |
| test.popSecond(); | |
| cout<<test.topSecond()<<endl; | |
| test.popSecond(); | |
| cout<<test.topSecond()<<endl; | |
| test.popSecond(); | |
| cout<<test.topSecond()<<endl; | |
| test.popSecond(); | |
| cout<<test.topSecond()<<endl; | |
| test.popSecond(); | |
| cout<<test.topSecond()<<endl; | |
| test.popSecond(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment