Advertisement
skb50bd

[207Lec2] STACK with ARRAY

May 18th, 2016
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int size;
  5. int Top = -1;
  6. int stk[1000];
  7.  
  8. bool isEmpty() {
  9.     return Top == -1 ? true : false;
  10. }
  11.  
  12. bool isFull() {
  13.     return Top == size - 1 ? true : false;
  14. }
  15.  
  16. void top() {
  17.     if (isEmpty())
  18.         cout << "Stack is Empty" << endl;
  19.     else
  20.         cout << "TOP: " << stk[Top] << endl;
  21. }
  22.  
  23. void pop() {
  24.     if (isEmpty())
  25.         cout << "Stack is Already Empty" << endl;
  26.     else {
  27.         Top--;
  28.         cout << "Success" << endl;
  29.     }
  30. }
  31.  
  32. void push(int x) {
  33.     if (isFull())
  34.         cout << "Overflow" << endl;
  35.     else {
  36.         stk[++Top] = x;
  37.         cout << "Success" << endl;
  38.     }
  39. }
  40.  
  41. void show() {
  42.     if (isEmpty()) {
  43.         cout << "Stack is Empty" << endl;
  44.         return;
  45.     }
  46.  
  47.     for (int i = 0; i <= Top; i++)
  48.         cout << stk[i] << " ";
  49.     cout << endl;
  50. }
  51.  
  52.  
  53. int main(){
  54.     int choice, value;
  55.     cout << "Enter the Maximum Size of the Stack: ";
  56.     cin >> size;
  57.  
  58.     while (true) {
  59.         cout << endl;
  60.         cout << "---Press 1 - for LIST" << endl;
  61.         cout << "---Press 2 - for TOP" << endl;
  62.         cout << "---Press 3 - for PUSH" << endl;
  63.         cout << "---Press 4 - for POP" << endl;
  64.         cout << "---Press 5 - for Checking if Empty" << endl;
  65.         cout << "---Press 6 - for Checking if Full" << endl;
  66.         cout << "---Press 0 - Exit" << endl;
  67.         cout << endl;
  68.         cout << "---Enter your Choice: ";
  69.         cin >> choice;
  70.         cout << endl;
  71.  
  72.         switch (choice) {
  73.             case 1: {
  74.                 show();
  75.                 break;
  76.             }
  77.             case 2: {
  78.                 top();
  79.                 break;
  80.             }
  81.             case 3: {
  82.                 cout << "Enter Value to Push: ";
  83.                 cin >> value;
  84.                 push(value);
  85.                 break;
  86.             }
  87.             case 4: {
  88.                 pop();
  89.                 break;
  90.             }
  91.             case 5: {
  92.                 isEmpty() ? cout << "Stack is Empty" << endl : cout << "Stack is NOT Empty" << endl;
  93.                 break;
  94.             }
  95.             case 6: {
  96.                 isFull() ? cout << "Stack is Full" << endl : cout << "Stack is NOT Full" << endl;
  97.                 break;
  98.             }
  99.             case 0: {
  100.                 return 0;
  101.             }
  102.             default: {
  103.                 cout << "Invalid Input. Try Again." << endl;
  104.                 continue;
  105.             }
  106.         }
  107.     }
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement