public class LinkedPureStack implements PureStack { protected Entry top; protected int size; /** * Initializes this LinkedPureStact object to be empty. * */ public LinkedPureStack() { top = null; size = 0; } // default constructor /** * Initializes this LinkedPureStact object to have a shallow copy of * a specified LinkedPureStack object. The worstTime(n) is O(n), where n is the * number of elements in the specified LinkedListPureQueue object. * * @param otherStack - the specified LinkedPureStack object. * */ public LinkedPureStack (LinkedPureStack otherStack) { this(); if (!otherStack.isEmpty()) { // Copy first element from otherStack: top = new Entry(); top.element = otherStack.top.element; top.next = null; size = 1; // Copy remaining elements from otherStack: Entry current = top; for (Entry otherCurrent = otherStack.top.next; otherCurrent != null; otherCurrent = otherCurrent.next) { current.next = new Entry(); current = current.next; current.element = otherCurrent.element; size++; } // for current.next = null; } // if } //copy constructor /** * Determines the number of elements in this LinkedPureStack object. * * @return the number of elements in this LinkedPureStack object. * */ public int size() { return size; } /** * Determines if this LinkedPureStack object has no elements. * * @return true - if this LinkedPureStack object has no elements; otherwise, * return false. * */ public boolean isEmpty() { return size == 0; } // method isEmpty /** * Inserts a specified element on the top of this LinkedPureStack object. * The averageTime(n) is constant and worstTime(n) is O(n). * * @param element - the element to be pushed. * */ public void push (E element) { Entry newEntry = new Entry(); newEntry.element = element; newEntry.next = top; top = newEntry; size++; } // method push /** * Removes the top element from this LinkedPureStack object. * * @return - the element removed. * * @throws NoSuchElementException - if this LinkedPureStack object is empty. * */ public E pop() { E element = top.element; top = top.next; size--; return element; } // method pop /** * Returns the top element on this PureStack object. * * @return - the element returned. * * @throws NoSuchElementException - if this PureStack object is empty. * */ public E peek() { return top.element; } // method peek protected static class Entry { protected E element; protected Entry next; } // inner class Entry } // class LinkedPureStack