publicArrayStack(int maxSize){ data = new String[maxSize]; this.maxSize = maxSize; count = 0; }
publicvoidpush(String item)throws Exception { if (count == maxSize - 1) { thrownew Exception("stack is full ,cant push anymore !"); } data[count] = item; count++; }
public String pop()throws Exception { if (count == 0) { thrownew Exception("Stack is empty, no element to pop !"); } String top = data[count-1]; count--; return top; }
publicvoidprintStack(){ for (int i = this.count-1; i >=0; i--) { System.out.println(this.data[i]); } }
publicvoidpush(T t){ Node node = new Node(t,top); top = node; }
public T pop()throws Exception { if (top == null){ thrownew Exception("Stack is empty, no element to pop !"); } Node<T> node = top; top = top.next; return node.data; }
publicvoidprintStack(Node<T> top){ while (top != null){ System.out.println(top.data); top = top.next; } }