Advertisement
Sonicxte

Stackoverflow

Feb 16th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. constexpr int _aSize = 20;
  4. int pArray[_aSize];
  5. int stackPointer = 0;
  6.  
  7. void pushOp(int input)
  8. {
  9.     pArray[stackPointer] = input;
  10.  
  11.     stackPointer = stackPointer + 1;
  12.  
  13.     //if (stackPointer > _aSize)
  14.     //{
  15.     //  std::cout << "Stack Overflow!";         //Exception handle for stack overflow
  16.     //}
  17.  
  18.  
  19. }
  20.  
  21.  
  22.  
  23. void popOp()
  24. {
  25.     stackPointer = stackPointer - 1;
  26.     pArray[stackPointer] = NULL;
  27.  
  28.     if (stackPointer < 0) stackPointer = 0;     //Exception handle for stackPointer notNULL
  29. }
  30.  
  31. void switchIndexForPush()
  32. {
  33.     for (int count = _aSize - 2; count >= stackPointer; --count)
  34.     {
  35.         pArray[count + 1] = pArray[count];
  36.     }
  37. }
  38.  
  39. void switchIndexForPop()
  40. {
  41.     for (stackPointer; stackPointer < _aSize-1; ++stackPointer)
  42.     {
  43.         pArray[stackPointer] = pArray[stackPointer + 1];
  44.     }
  45. }
  46.  
  47. void searchStackForPush()
  48. {
  49.     for (int count = stackPointer; pArray[count] != 0; ++count)
  50.     {
  51.         stackPointer = count +1;
  52.     }
  53. }
  54.  
  55. void searchStackForPop()
  56. {
  57.     for (int count = stackPointer; pArray[count] == 0; --count)
  58.     {
  59.         stackPointer = count;
  60.     }
  61. }
  62.  
  63. void pushDirect(int input)
  64. {
  65.     std::cout << "Give position(stack) to push: ";
  66.     std::cin >> stackPointer;
  67.     switchIndexForPush();
  68.     pArray[stackPointer] = input;
  69.  
  70.     searchStackForPush();
  71. }
  72.  
  73. void popDirect()
  74. {
  75.     std::cout << "Give position(stack) to pop: ";
  76.     std::cin >> stackPointer;
  77.     switchIndexForPop();
  78.  
  79.     searchStackForPop();
  80. }
  81.  
  82. void fillArray(int _pArray[])
  83. {
  84.     for (int count = 0; count < _aSize; ++count)
  85.     {
  86.         _pArray[count] = NULL;
  87.     }
  88. }
  89.  
  90. void readArray(int _pArray[])
  91. {
  92.     for (int count = 0; count < _aSize; ++count)
  93.     {
  94.         std::cout << "[ " << _pArray[count] << " ]";
  95.     }
  96.     std::cout << "   Stackpointer: " << stackPointer;
  97. }
  98.  
  99.  
  100. int main()
  101. {
  102.     int In;
  103.     int In2;
  104.     fillArray(pArray);
  105.     while (true) {
  106.         std::cout << "Choose one function: (1) for PUSH | (2) for direkt PUSH | (3) for POP | (4) for direkt POP ";
  107.         std::cin >> In;
  108.         switch (In)
  109.         {
  110.         case 1:
  111.             std::cout << "Give one Number for Push on Stack: ";
  112.             std::cin >> In2;
  113.             pushOp(In2);
  114.             break;
  115.  
  116.         case 2:
  117.             std::cout << "Give one Number for Push on Stack: ";
  118.             std::cin >> In2;
  119.             pushDirect(In2);
  120.             break;
  121.  
  122.         case 3:
  123.             popOp();
  124.             break;
  125.  
  126.         case 4:
  127.             popDirect();
  128.             break;
  129.         }
  130.         readArray(pArray);
  131.         std::cout << std::endl;
  132.     }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement