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.  * Created by green on 19.12.15.
  8.  */
  9. class steck {
  10. private int size;
  11. private Object[] date;
  12. private int capacity;
  13. public steck(){
  14. date = new Object[2];
  15. capacity = 2;
  16. }
  17. public steck(int x){
  18. date = new Object[x];;
  19. capacity = x;
  20. }
  21.  
  22. public boolean isEmpty(){
  23. return size == 0;
  24. }
  25.  
  26. private void resize(int x){
  27. Object[] newDate = new Object[x];
  28. System.arraycopy(date, 0, newDate, 0, size);
  29. date = null;
  30. date = newDate;
  31. }
  32.  
  33. public void push(Object x){
  34. if(size == capacity){
  35. resize(2*capacity);
  36. }
  37. date[size++] = x;
  38. }
  39.  
  40. public Object pop(){
  41. if(isEmpty()){
  42. return null;
  43. }
  44. if(size < capacity/2){
  45. resize(capacity/2);
  46. }
  47. Object temp = date[size -1];
  48. size = size -1;
  49. return temp;
  50. }
  51.  
  52. public Object peek(){
  53. if(isEmpty()) return null;
  54. return date[size-1];
  55. }
  56.  
  57. public void clear(){
  58. size = 0;
  59. }
  60.  
  61. public int size(){
  62. return size;
  63. }
  64. public int getCapacity(){
  65. return size;
  66. }
  67. }
  68.  
  69. class Ideone
  70. {
  71. public static void main (String[] args) throws java.lang.Exception
  72. {
  73. steck test = new steck(4);
  74. System.out.println(test.isEmpty());
  75. test.push(12);
  76. System.out.println(test.isEmpty());
  77. System.out.println(test.peek());
  78. test.push(13);
  79. test.push(14);
  80. test.push(15);
  81. test.push(16);
  82. test.push(17);
  83. test.push('(');
  84. System.out.println(test.size());
  85. System.out.println(test.peek().equals('('));
  86. test.pop();
  87. System.out.println(test.peek());
  88. test.pop();
  89. System.out.println(test.peek());
  90. test.pop();
  91. System.out.println(test.peek());
  92. test.pop();
  93. System.out.println(test.peek());
  94. test.pop();
  95. System.out.println(test.peek());
  96. System.out.println(test.isEmpty());
  97. test.pop();
  98. System.out.println(test.peek());
  99. test.pop();
  100. System.out.println(test.isEmpty());
  101. }
  102. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
true
false
12
7
true
17
16
15
14
13
false
12
true