Advertisement
gjorgjikirkov

SpecialStack

May 26th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package special_stack;
  7.  
  8. import java.util.Iterator;
  9. import java.util.Stack;
  10.  
  11. /**
  12.  *
  13.  * @author 141021
  14.  */
  15.  
  16. /*
  17.  Question: Design a Data Structure SpecialStack that supports all the stack
  18. operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin()
  19. which should return minimum element from the SpecialStack. All these operations
  20. of SpecialStack must be O(1).
  21. To implement SpecialStack, you should only use standard Stack data structure and no
  22. other data structure like arrays, list, .. etc.
  23.  
  24. Example:
  25.  
  26. Consider the following SpecialStack
  27. 16  --> TOP
  28. 15
  29. 29
  30. 19
  31. 18
  32.  
  33. When getMin() is called it should return 15, which is the minimum
  34. element in the current stack.
  35.  
  36. If we do pop two times on stack, the stack becomes
  37. 29  --> TOP
  38. 19
  39. 18
  40.  
  41. When getMin() is called, it should return 18 which is the minimum
  42. in the current stack.
  43.  */
  44. class SpecialStack {
  45.  
  46.     Stack min;
  47.     Stack stack;
  48.  
  49.     public SpecialStack() {
  50.         min = new Stack();
  51.         stack = new Stack();
  52.     }
  53.  
  54.     public void push(int x) {
  55.         if (stack.isEmpty()) {
  56.             stack.push(x);
  57.             min.push(x);
  58.         } else {
  59.             stack.push(x);
  60.             int y = (int) min.pop();
  61.             if (x < y) {
  62.                 min.push(x);
  63.             } else {
  64.                 min.push(y);
  65.             }
  66.         }
  67.     }
  68.  
  69.     public int peek() {
  70.         return (int) stack.peek();
  71.     }
  72.  
  73.     public boolean isEmpty() {
  74.         return stack.isEmpty();
  75.     }
  76.  
  77.     public int pop() {
  78.         return (int) stack.pop();
  79.     }
  80.  
  81.     public int getMin() {
  82.         int x = (int) min.pop();
  83.         min.push(x);
  84.         return x;
  85.     }
  86.  
  87.     @Override
  88.     public String toString() {
  89.         StringBuilder sb = new StringBuilder();
  90.         Iterator it = stack.iterator();
  91.         while (it.hasNext()) {
  92.             sb.append(it.next());
  93.             sb.append("\n");
  94.         }
  95.  
  96.         return sb.toString();
  97.     }
  98. }
  99.  
  100. public class SpecialStackTest {
  101.  
  102.     public static void main(String[] args) {
  103.         SpecialStack stack = new SpecialStack();
  104.  
  105.         for (int i = 0; i < 10; i += 2) {
  106.             stack.push(i);
  107.         }
  108.  
  109.         System.out.println(stack.toString());
  110.  
  111.         System.out.println(stack.isEmpty());
  112.         System.out.println(stack.peek());
  113.         System.out.println(stack.getMin());
  114.         stack.push(-2);
  115.         System.out.println(stack.getMin());
  116.  
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement