public class SequenceStack<T> { private int DEFAULT_SIZE = 10; // save the length of the array private int capacity; // Define the length of the array to increase each time when the underlying array capacity is not enough private int capacityIncrement = 0; // Define an array to hold the elements of the sequential stack private Object[] elementData; // Save the current number of elements in the sequence stack private int size = 0; // create empty sequential stack with default array length public SequenceStack() { capacity = DEFAULT_SIZE; elementData = new Object[capacity]; } // create a sequential stack with an initializer element public SequenceStack(T element) { this(); elementData[0] = element; size++; } /** * Create a sequential stack with an array of specified length * @param element specifies the first element in the sequence stack * @param initSize specifies the length of the underlying array of the order stack */ public SequenceStack(T element, int initSize) { this.capacity = initSize; elementData = new Object[capacity]; elementData[0] = element; size++; } /** * Create a sequential stack with an array of specified length * @param element specifies the first element in the sequence stack * @param initSize specifies the length of the underlying array of the order stack * @param capacityIncrement specifies when the length of the bottom array of the sequence stack is not enough, the length of the bottom array to increase each time */ public SequenceStack(T element, int initSize, int capacityIncrement) { this.capacity = initSize; this.capacityIncrement = capacityIncrement; elementData = new Object[capacity]; elementData[0] = element; size++; } // Get the size of the sequence stack public int length() { return size; } // push to stack public void push(T element) { ensureCapacity(size + 1); elementData[size++] = element; } private void ensureCapacity(int minCapacity) { // If the original length of the array is less than the current required length if (minCapacity > capacity) { if (capacityIncrement > 0) { while (capacity < minCapacity) { capacity += capacityIncrement; } } else { while (capacity < minCapacity) { capacity <<= 1; } } elementData = Arrays.copyOf(elementData, capacity); } } // pop the stack public T pop() { T oldValue = (T) elementData[size - 1]; // release the top element of the stack elementData[--size] = null; return oldValue; } // return the top element of the stack, but do not delete the top element public T peek() { return (T)elementData[size - 1]; } // Determine if the sequence stack is empty public boolean empty() { return size == 0; } // clear the sequence stack public void clear() { Arrays.fill(elementData, null); size = 0; } public String toString() { if (size == 0) { return "[]"; } else { StringBuilder sb = new StringBuilder("["); for (int i = size - 1; i > -1; i--) { sb.append(elementData[i].toString() + ", "); } int len = sb.length(); return sb.delete(len - 2, len).append("]").toString(); } } }
public class SequenceStackTest { public static void main(String[] args) { SequenceStack<String> stack = new SequenceStack<String>(); stack.push("aaaa"); stack.push("bbbb"); stack.push("cccc"); stack.push("dddd"); System.out.println("List elements after initialization: " + stack.toString()); System.out.println("Access the top element of the stack: " + stack.peek()); System.out.println("Popping the top element of the stack for the first time: " + stack.pop()); System.out.println("The stack element after the first pop: " + stack.toString()); System.out.println("Pop the top element of the stack for the second time: " + stack.pop()); System.out.println("Stack element after two pops: " + stack.toString()); } }