Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stack* sortStack(stack* Input) {
- // TODO
- stack* sortedStack = new stack();
- node* centralNode = new node();
- int tempIntHolder = 0;
- int numOfTempInputs = 0;
- while (!Input->isEmpty()) //WHEN NOT EMPTY
- {
- if (centralNode->data == -1) {
- centralNode = Input->pop(); //CENTRAL NODE IS TOP
- }
- if (Input->top() < centralNode->data) {//INPUT->TOP IS SMALLER, ATTEMPT TO PUSH INTO SORTEDSTACK
- if ((Input->top() > sortedStack->top()) || (sortedStack->isEmpty() == true)) //CENTRAL NODE > INPUT->TOP > SORTEDSTACK->TOP
- {
- sortedStack->push((Input->pop())->data);
- }
- else //CENTRAL NODE > INPUT->TOP < SORTEDSTACK->TOP
- {
- tempIntHolder = (Input->pop())->data;
- while (((tempIntHolder < sortedStack->top()) && (sortedStack->isEmpty() == false)) || (sortedStack->isEmpty() == true)) {
- if (sortedStack->isEmpty() == true) break;
- Input->push((sortedStack->pop())->data); //REVERSE PUSH INTO INPUT TEMPORARILY
- numOfTempInputs++;
- }
- //AT THIS POINT TEMPINTHOLDER IS LARGER THAN TOP OF SORTEDSTACK
- sortedStack->push(tempIntHolder); tempIntHolder = 0;
- while (numOfTempInputs > 0) { //PUSH THE TEMPORARIES IN INPUT STACK BACK TO SORTEDSTACK, THEY ARE SORTED
- sortedStack->push((Input->pop())->data);
- numOfTempInputs--;
- }
- }
- }
- else { //INPUT->TOP > CENTRAL NODE, PUSH OLD CENTRAL NODE INTO SORTEDSTACK AND THEN MAKE INPUT->TOP THE CENTRAL NODE
- sortedStack->push(centralNode->data);
- centralNode = Input->pop();
- }
- }
- //AT THIS POINT INPUT STACK IS EMPTY
- sortedStack->push(centralNode->data);
- return sortedStack;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement