Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. stack* sortStack(stack* Input) {
  2. // TODO
  3. stack* sortedStack = new stack();
  4. node* centralNode = new node();
  5. int tempIntHolder = 0;
  6. int numOfTempInputs = 0;
  7. while (!Input->isEmpty()) //WHEN NOT EMPTY
  8. {
  9. if (centralNode->data == -1) {
  10. centralNode = Input->pop(); //CENTRAL NODE IS TOP
  11. }
  12. if (Input->top() < centralNode->data) {//INPUT->TOP IS SMALLER, ATTEMPT TO PUSH INTO SORTEDSTACK
  13. if ((Input->top() > sortedStack->top()) || (sortedStack->isEmpty() == true)) //CENTRAL NODE > INPUT->TOP > SORTEDSTACK->TOP
  14. {
  15. sortedStack->push((Input->pop())->data);
  16. }
  17. else //CENTRAL NODE > INPUT->TOP < SORTEDSTACK->TOP
  18. {
  19. tempIntHolder = (Input->pop())->data;
  20. while (((tempIntHolder < sortedStack->top()) && (sortedStack->isEmpty() == false)) || (sortedStack->isEmpty() == true)) {
  21. if (sortedStack->isEmpty() == true) break;
  22. Input->push((sortedStack->pop())->data); //REVERSE PUSH INTO INPUT TEMPORARILY
  23. numOfTempInputs++;
  24. }
  25. //AT THIS POINT TEMPINTHOLDER IS LARGER THAN TOP OF SORTEDSTACK
  26. sortedStack->push(tempIntHolder); tempIntHolder = 0;
  27. while (numOfTempInputs > 0) { //PUSH THE TEMPORARIES IN INPUT STACK BACK TO SORTEDSTACK, THEY ARE SORTED
  28. sortedStack->push((Input->pop())->data);
  29. numOfTempInputs--;
  30. }
  31. }
  32. }
  33. else { //INPUT->TOP > CENTRAL NODE, PUSH OLD CENTRAL NODE INTO SORTEDSTACK AND THEN MAKE INPUT->TOP THE CENTRAL NODE
  34. sortedStack->push(centralNode->data);
  35. centralNode = Input->pop();
  36. }
  37. }
  38. //AT THIS POINT INPUT STACK IS EMPTY
  39. sortedStack->push(centralNode->data);
  40. return sortedStack;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement