Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- constexpr int _aSize = 20;
- int pArray[_aSize];
- int stackPointer = 0;
- void pushOp(int input)
- {
- pArray[stackPointer] = input;
- stackPointer = stackPointer + 1;
- //if (stackPointer > _aSize)
- //{
- // std::cout << "Stack Overflow!"; //Exception handle for stack overflow
- //}
- }
- void popOp()
- {
- stackPointer = stackPointer - 1;
- pArray[stackPointer] = NULL;
- if (stackPointer < 0) stackPointer = 0; //Exception handle for stackPointer notNULL
- }
- void switchIndexForPush()
- {
- for (int count = _aSize - 2; count >= stackPointer; --count)
- {
- pArray[count + 1] = pArray[count];
- }
- }
- void switchIndexForPop()
- {
- for (stackPointer; stackPointer < _aSize-1; ++stackPointer)
- {
- pArray[stackPointer] = pArray[stackPointer + 1];
- }
- }
- void searchStackForPush()
- {
- for (int count = stackPointer; pArray[count] != 0; ++count)
- {
- stackPointer = count +1;
- }
- }
- void searchStackForPop()
- {
- for (int count = stackPointer; pArray[count] == 0; --count)
- {
- stackPointer = count;
- }
- }
- void pushDirect(int input)
- {
- std::cout << "Give position(stack) to push: ";
- std::cin >> stackPointer;
- switchIndexForPush();
- pArray[stackPointer] = input;
- searchStackForPush();
- }
- void popDirect()
- {
- std::cout << "Give position(stack) to pop: ";
- std::cin >> stackPointer;
- switchIndexForPop();
- searchStackForPop();
- }
- void fillArray(int _pArray[])
- {
- for (int count = 0; count < _aSize; ++count)
- {
- _pArray[count] = NULL;
- }
- }
- void readArray(int _pArray[])
- {
- for (int count = 0; count < _aSize; ++count)
- {
- std::cout << "[ " << _pArray[count] << " ]";
- }
- std::cout << " Stackpointer: " << stackPointer;
- }
- int main()
- {
- int In;
- int In2;
- fillArray(pArray);
- while (true) {
- std::cout << "Choose one function: (1) for PUSH | (2) for direkt PUSH | (3) for POP | (4) for direkt POP ";
- std::cin >> In;
- switch (In)
- {
- case 1:
- std::cout << "Give one Number for Push on Stack: ";
- std::cin >> In2;
- pushOp(In2);
- break;
- case 2:
- std::cout << "Give one Number for Push on Stack: ";
- std::cin >> In2;
- pushDirect(In2);
- break;
- case 3:
- popOp();
- break;
- case 4:
- popDirect();
- break;
- }
- readArray(pArray);
- std::cout << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement