Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ArrayStack {
- private Object[] elements;
- private int size;
- private int capacity;
- /**
- * Creates an empty ArrayStack with capacity 1.
- */
- public ArrayStack() {
- capacity = 1;
- size = 0;
- elements = new Object[1];
- }
- /**
- * @return The size of this ArrayStack.
- */
- public int size() {
- return size;
- }
- /**
- * @return `true` iff this ArrayStack is empty, `false` otherwise.
- */
- public boolean isEmpty() {
- if(size == 0){
- return true;
- }else{
- return false;
- }
- }
- /**
- * @return `true` iff the size is equal to the capacity, `false` otherwise.
- */
- public boolean isFull() {
- if(size == capacity){
- return true;
- }else{
- return false;
- }
- }
- /**
- * @return the top element of the stack without removing it
- */
- public Object peek() throws EmptyStackException {
- if(capacity == 0 || size == 0){
- throw new EmptyStackException();
- }else{
- return elements[size - 1];
- }
- }
- /**
- * Adds `o` to the stack.
- * If capacity of stack was too small, capacity is doubled and `o` is added.
- *
- * @param o
- * the element to add to the stack.
- */
- public void push(Object o) {
- if(isFull()){
- int capacitynew = 2 * capacity;
- Object[] temp = new Object[capacity];
- for(int i = 0; i < elements.length; i++){
- temp [i] = elements[i];
- }
- elements = new Object[capacitynew];
- for(int j = 0; j < temp.length; j++){
- elements[j] = temp[j];
- }
- capacity = capacitynew;
- }
- elements[size] = o;
- size++;
- }
- /**
- * Removes the top element from the stack.
- * If removing top would make the stack use less than 25% of its capacity,
- * then the capacity is halved.
- *
- * @return the element which was at the top of the stack.
- * @throws EmptyStackException
- * iff the queue is empty
- */
- public Object pop() throws EmptyStackException {
- if(size == 0){
- throw new EmptyStackException();
- }else{
- Object e = elements[size - 1];
- elements[size - 1] = null;
- size--;
- if(size < (capacity / 4)){
- int newcapacity = capacity/2;
- Object[] temp = new Object[capacity];
- for(int i = 0; i < size; i++){
- temp [i] = elements[i];
- }
- elements = new Object[newcapacity];
- for(int j = 0; j < size; j++){
- elements[j] = temp[j];
- }
- capacity = newcapacity;
- }
- return e;
- }
- }
- /**
- * @return a String representation of the ArrayStack
- * Example output for ArrayStack with 2 elements and capacity 5:
- * <ArrayStack[1,2]>(Size=2, Cap=5)
- */
- public String toString() {
- if(size == 0){
- return "<ArrayStack[]>(Size=0, Cap=" + capacity + ")";
- }
- String begin = "<ArrayStack[";
- String result = begin;
- for(int i = 0; i < size; i++){
- if(i == size -1){
- result += elements[i] + "]>";
- }else{
- result += elements[i] + ",";
- }
- }
- result += "(Size=" + size + ", Cap=" + capacity + ")";
- return result;
- }
- // For testing, do not remove or change.
- public Object[] getElements() {
- return elements;
- }
- }
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement