fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. try {
  13. // your code goes here
  14. SStack<String> stack = new SStack();
  15. boolean threwException;
  16. threwException = false;
  17. try {
  18. stack.peek();
  19. }
  20. {
  21. threwException = true;
  22. }
  23. finally
  24. {
  25. if(!threwException)
  26. {
  27. System.out.println("peek for empty is broken");
  28. }
  29. }
  30.  
  31. threwException = false;
  32. try {
  33. stack.pop();
  34. }
  35. {
  36. threwException = true;
  37. }
  38. finally
  39. {
  40. if(!threwException)
  41. {
  42. System.out.println("pop for empty is broken");
  43. }
  44. }
  45. String one = "1";
  46. String two = "2";
  47. String three = "3";
  48.  
  49. stack.push(one);
  50. assertTrue(stack.pop().equals(one));
  51.  
  52. threwException = false;
  53. try {
  54. stack.pop();
  55. }
  56. {
  57. threwException = true;
  58. }
  59. finally
  60. {
  61. if(!threwException)
  62. {
  63. System.out.println("pop does not remove objects");
  64. }
  65. }
  66.  
  67. stack.push(one);
  68. stack.push(one);
  69. assertTrue(stack.pop().equals(one));
  70. assertTrue(stack.pop().equals(one));//duplicate object test
  71.  
  72. stack.push(one);
  73. stack.push(two);
  74. stack.push(three);
  75.  
  76. assertTrue(stack.pop().equals(three), three);
  77. assertTrue(stack.pop().equals(two), two);
  78. assertTrue(stack.pop().equals(one), one);//in order test
  79.  
  80. stack.push(one);
  81. stack.push(two);
  82.  
  83. assertTrue(stack.pop().equals(two), two);
  84.  
  85. stack.push(three);
  86. assertTrue(stack.peek().equals(three), three); //peek test
  87. assertTrue(stack.pop().equals(three), three);
  88.  
  89. assertTrue(stack.pop().equals(one), one);//test for adding items after removing
  90. } catch(Exception e)
  91. {
  92. System.out.println(e.getMessage());
  93. }
  94. }
  95.  
  96. public static void assertTrue(boolean value){
  97. if(!value){
  98. throw new IllegalStateException("code is bork");
  99. }
  100. }
  101. public static void assertTrue(boolean value, String message){
  102. if(!value){
  103. throw new IllegalStateException("code is bork: "+message);
  104. }
  105. }
  106. }
  107.  
  108. class SStack<T> {
  109.  
  110. private class Node {
  111.  
  112. private T data;
  113. private Node prev, next;
  114.  
  115. public Node() {
  116. }
  117.  
  118. public Node(T data) {
  119. this.data = data;
  120. }
  121. }
  122.  
  123. private Node top;
  124.  
  125. public SStack() {
  126. top = null;
  127. }
  128.  
  129. public boolean empty() {
  130. return top == null;
  131. }
  132.  
  133. public T peek() {
  134. if (top == null) {
  135. throw new EmptyStackException();
  136. }
  137. return top.data;
  138. }
  139.  
  140. public T pop() {
  141. if (top == null) {
  142. throw new EmptyStackException();
  143. }
  144. T value = top.data;
  145. top = top.prev;
  146. return value;
  147. }
  148.  
  149. public T push(T data) {
  150. Node temp = new Node(data);
  151. if (top == null) {
  152. top = temp;
  153. } else {
  154. top.next = temp;
  155. temp.prev = top;
  156. top = temp;
  157. }
  158. return temp.data;
  159. }
  160. }
Success #stdin #stdout 0.09s 320576KB
stdin
Standard input is empty
stdout
Standard output is empty