fueanta

Two Stacks in one array.

Nov 10th, 2016
155
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Implementation of two stacks in one array...
  2.  
  3. #include <iostream>
  4. #include <stdlib.h>
  5. using namespace std;
  6.  
  7. int SIZE, topA, topB, *arr, N1, N2;
  8. void stacks_initiate() {
  9.     topA = -1;
  10.     topB = SIZE;
  11.     arr = new int[SIZE];
  12. }
  13.  
  14. void push_A(int);
  15. void push_B(int);
  16. int pop_A();
  17. int pop_B();
  18. int top_A();
  19. int top_B();
  20. void show_A();
  21. void show_B();
  22. void menu();
  23.  
  24. int main() {
  25.     cout << "Stack size 01 : "; cin >> N1;
  26.     cout << "Stack size 02 : "; cin >> N2;
  27.     SIZE = N1 + N2;
  28.     stacks_initiate();
  29.     menu();
  30.     return 0;
  31. }
  32.  
  33. void push_A(int x) {
  34.     if (topB == topA + 1)
  35.         cout << "\nStack overflow. [ARRAY is full]" << endl;
  36.     else if (topA == N1 - 1) {
  37.         cout << "\nStack(A) overflow. It seems you can still push into the other stack from the menu.\n";
  38.     }
  39.     else {
  40.         arr[++topA] = x;
  41.         cout << "\nData " << x << " has been pushed into stack A." << endl;
  42.     }
  43. }
  44.  
  45. void push_B(int x) {
  46.     if (topB == topA + 1)
  47.         cout << "\nStack overflow. [ARRAY is full]" << endl;
  48.     else if (topB == SIZE - N2) { // or simply top == N1 could be used..
  49.         cout << "\nStack(B) overflow. It seems you can still push into the other stack from the menu.\n";
  50.     }
  51.     else {
  52.         arr[--topB] = x;
  53.         cout << "\nData " << x << " has been pushed into stack B." << endl;
  54.     }
  55. }
  56.  
  57. int pop_A() {
  58.     if (topA == -1)
  59.         cout << "\nStack underflow. [0 element in stack A]" << endl;
  60.     else {
  61.         int x = top_A();
  62.         arr[topA--] = NULL;
  63.         return x;
  64.     }
  65. }
  66.  
  67. int pop_B() {
  68.     if (topB == SIZE)
  69.         cout << "\nStack underflow. [0 element in stack B]" << endl;
  70.     else {
  71.         int x = top_B();
  72.         arr[topB++] = NULL;
  73.         return x;
  74.     }
  75. }
  76.  
  77. int top_A() {
  78.     return arr[topA];
  79. }
  80.  
  81. int top_B() {
  82.     return arr[topB];
  83. }
  84.  
  85. void show_A() {
  86.     if (topA == -1) cout << "\nStack A is empty." << endl;
  87.     else {
  88.         cout << "\nStack A : ";
  89.         for (int i = topA; i >= 0; i--)
  90.         {
  91.             cout << arr[i] << " ";
  92.         }
  93.         cout << endl;
  94.     }
  95. }
  96.  
  97. void show_B() {
  98.     if (topB == SIZE) cout << "\nStack B is empty." << endl;
  99.     else {
  100.         cout << "\nStack B : ";
  101.         for (int i = topB; i < SIZE; i++)
  102.         {
  103.             cout << arr[i] << " ";
  104.         }
  105.         cout << endl;
  106.     }
  107. }
  108.  
  109. void menu() {
  110.     cout << "\n01. Push into A.\n02. Push into B.\n03. Pop from A.\n04. Pop from B.\n05. Get top from A.\n06. Get top from B.\n07. Show A.\n08. Show B.\n09. EXIT." << endl;
  111.     cout << "\nChoice : "; int ch; int x;
  112.     cin >> ch;
  113.     switch (ch) {
  114.     case 1: cout << "\nWhat do you want to push into A? Push : ";
  115.         cin >> x;
  116.         push_A(x); menu();
  117.         break;
  118.     case 2: cout << "\nWhat do you want to push into B? Push : ";
  119.         cin >> x;
  120.         push_B(x); menu();
  121.         break;
  122.     case 3: if (topA == -1) cout << "\nStack A is empty." << endl;
  123.         else cout << "\nData " << pop_A() << " has been removed from stack A.\n"; menu();
  124.         break;
  125.     case 4: if (topB == SIZE) cout << "\nStack B is empty." << endl;
  126.         else cout << "\nData " << pop_B() << " has been removed from stack B.\n"; menu();
  127.         break;
  128.     case 5: if (topA == -1) cout << "\nStack A is empty." << endl;
  129.          else cout << "\nData at top of stack A : " << top_A() << endl; menu();
  130.         break;
  131.     case 6: if (topB == SIZE) cout << "\nStack B is empty." << endl;
  132.         else cout << "\nData at top of stack B : " << top_B() << endl; menu();
  133.         break;
  134.     case 7: show_A(); menu();
  135.         break;
  136.     case 8: show_B(); menu();
  137.         break;
  138.     case 9: cout << "\nThank You..! Terminating Program...\n" << endl;
  139.         delete []arr;
  140.         exit(0);
  141.         break;
  142.     default: cout << "\nInvalid choice. Try again." << endl;
  143.         menu();
  144.     }
  145. }
RAW Paste Data