fork(1) download
  1.  
  2. class Ideone {
  3. public static void main(String[] args) {
  4. MinStack stack = new MinStack();
  5. for (int i = 0; i < 5; ++i) {
  6. stack.push(i);
  7. }
  8. for (int i = 0; i < 5; ++i) {
  9. System.out.println(stack.getMin());
  10. stack.pop();
  11. }
  12.  
  13. }
  14. }
  15.  
  16. class MinStack {
  17. private Node head;
  18.  
  19. public void push(int x) {
  20. if (head == null)
  21. head = new Node(x, x, null);
  22. else
  23. head = new Node(x, Math.min(x, head.min), head);
  24. }
  25.  
  26. public void pop() {
  27. head = head.next;
  28. }
  29.  
  30. public int top() {
  31. return head.val;
  32. }
  33.  
  34. public int getMin() {
  35. return head.min;
  36. }
  37.  
  38. private class Node {
  39. int val;
  40. int min;
  41. Node next;
  42.  
  43. private Node(int val, int min, Node next) {
  44. this.val = val;
  45. this.min = min;
  46. this.next = next;
  47. }
  48. }
  49. }
Success #stdin #stdout 0.07s 47076KB
stdin
Standard input is empty
stdout
0
0
0
0
0