Advertisement
jcvitanovic

CTCI, 3.6.

Jan 11th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. /*
  2. 11.01.2015
  3. 3.6. Write a program to sort a stack in ascending order (with biggest items on top).
  4. You may use at most one additional stack to hold items, but you may not copy
  5. the elements into any other data structure (such as an array). The stack supports
  6. the following operations: push, pop, peek, and isEmpty
  7. */
  8. #include<stdio.h>
  9. #include<iostream>
  10. #include<time.h>
  11. #include<stdlib.h>
  12.  
  13. using namespace std;
  14.  
  15. class Node{
  16. public:
  17.     Node(int n){
  18.         value = n;
  19.         next = NULL;
  20.     }
  21.     int value;
  22.     Node *next;
  23. };
  24.  
  25. class Stack{
  26. private:
  27.     Node *top;
  28. public:
  29.     Stack(){
  30.         top = NULL;
  31.     }
  32.     void push(int value){
  33.         if(!top){
  34.             top = new Node(value);
  35.         }
  36.         else{
  37.             Node *newTop = new Node(value);
  38.             newTop->next = top;
  39.             top = newTop;
  40.         }
  41.     }
  42.     int pop(){
  43.         if (!top){
  44.             return -1;
  45.         }
  46.         Node *tmp = top;
  47.         int retValue = tmp->value;
  48.         top = top->next;
  49.         free(tmp);
  50.         return retValue;
  51.     }
  52.     bool isEmpty(){
  53.         return top == NULL;
  54.     }
  55.     int peek(){
  56.         if (!top)
  57.             return -1;
  58.         return top->value;
  59.     }
  60.     void print(){
  61.         if (isEmpty())
  62.             return;
  63.         else{
  64.             int val = pop();
  65.             cout << val << " ";
  66.             print();
  67.             push(val);
  68.  
  69.         }
  70.     }
  71. };
  72.  
  73. void sortOrder(Stack &stack, int value){
  74.     if (stack.isEmpty()){
  75.         stack.push(value);
  76.         return;
  77.     }
  78.     if (stack.peek() > value){
  79.         int memTop = stack.pop();
  80.         sortOrder(stack, value);
  81.         stack.push(memTop);
  82.     }
  83.     else{
  84.         stack.push(value);
  85.     }
  86.  
  87.  
  88. }
  89. void sortStack(Stack &stack){
  90.     if (stack.isEmpty()){
  91.         return;
  92.     }
  93.     int topValue = stack.pop();
  94.     sortStack(stack);
  95.     sortOrder(stack, topValue);
  96.  
  97. }
  98.  
  99. int main(){
  100.     Stack s;
  101.     srand(time(NULL));
  102.    
  103.     for (int i = 0; i < 10; i++){
  104.         int rnd = rand() % 100 + 1;
  105.         cout << "push: " << rnd << "\n";
  106.         s.push(rnd);
  107.     }
  108.     s.print();
  109.     sortStack(s);
  110.     cout << "sorted: \n";
  111.     s.print();
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement