| 1 | // License: GPL v3 or later courtesy of author Kevin Wayne
|
|---|
| 2 | package edu.princeton.cs.algs4;
|
|---|
| 3 |
|
|---|
| 4 | /* ************************************************************************
|
|---|
| 5 | * Compilation: javac Stack.java
|
|---|
| 6 | * Execution: java Stack < input.txt
|
|---|
| 7 | *
|
|---|
| 8 | * A generic stack, implemented using a linked list. Each stack
|
|---|
| 9 | * element is of type Item.
|
|---|
| 10 | *
|
|---|
| 11 | * % more tobe.txt
|
|---|
| 12 | * to be or not to - be - - that - - - is
|
|---|
| 13 | *
|
|---|
| 14 | * % java Stack < tobe.txt
|
|---|
| 15 | * to be not that or be (2 left on stack)
|
|---|
| 16 | *
|
|---|
| 17 | *************************************************************************/
|
|---|
| 18 |
|
|---|
| 19 | import java.util.Iterator;
|
|---|
| 20 | import java.util.NoSuchElementException;
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * The <tt>Stack</tt> class represents a last-in-first-out (LIFO) stack of generic items.
|
|---|
| 25 | * It supports the usual <em>push</em> and <em>pop</em> operations, along with methods
|
|---|
| 26 | * for peeking at the top item, testing if the stack is empty, and iterating through
|
|---|
| 27 | * the items in LIFO order.
|
|---|
| 28 | * <p>
|
|---|
| 29 | * All stack operations except iteration are constant time.
|
|---|
| 30 | * <p>
|
|---|
| 31 | * For additional documentation, see <a href="/algs4/13stacks">Section 1.3</a> of
|
|---|
| 32 | * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
|
|---|
| 33 | */
|
|---|
| 34 | public class Stack<Item> implements Iterable<Item> {
|
|---|
| 35 | private int N; // size of the stack
|
|---|
| 36 | private Node first; // top of stack
|
|---|
| 37 |
|
|---|
| 38 | // helper linked list class
|
|---|
| 39 | private class Node {
|
|---|
| 40 | private Item item;
|
|---|
| 41 | private Node next;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Create an empty stack.
|
|---|
| 46 | */
|
|---|
| 47 | public Stack() {
|
|---|
| 48 | first = null;
|
|---|
| 49 | N = 0;
|
|---|
| 50 | assert check();
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Is the stack empty?
|
|---|
| 55 | */
|
|---|
| 56 | public boolean isEmpty() {
|
|---|
| 57 | return first == null;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Return the number of items in the stack.
|
|---|
| 62 | */
|
|---|
| 63 | public int size() {
|
|---|
| 64 | return N;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Add the item to the stack.
|
|---|
| 69 | */
|
|---|
| 70 | public void push(Item item) {
|
|---|
| 71 | Node oldfirst = first;
|
|---|
| 72 | first = new Node();
|
|---|
| 73 | first.item = item;
|
|---|
| 74 | first.next = oldfirst;
|
|---|
| 75 | N++;
|
|---|
| 76 | assert check();
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Delete and return the item most recently added to the stack.
|
|---|
| 81 | * Throw an exception if no such item exists because the stack is empty.
|
|---|
| 82 | */
|
|---|
| 83 | public Item pop() {
|
|---|
| 84 | if (isEmpty()) throw new RuntimeException("Stack underflow");
|
|---|
| 85 | Item item = first.item; // save item to return
|
|---|
| 86 | first = first.next; // delete first node
|
|---|
| 87 | N--;
|
|---|
| 88 | assert check();
|
|---|
| 89 | return item; // return the saved item
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Return the item most recently added to the stack.
|
|---|
| 95 | * Throw an exception if no such item exists because the stack is empty.
|
|---|
| 96 | */
|
|---|
| 97 | public Item peek() {
|
|---|
| 98 | if (isEmpty()) throw new RuntimeException("Stack underflow");
|
|---|
| 99 | return first.item;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /**
|
|---|
| 103 | * Return string representation.
|
|---|
| 104 | */
|
|---|
| 105 | @Override
|
|---|
| 106 | public String toString() {
|
|---|
| 107 | StringBuilder s = new StringBuilder();
|
|---|
| 108 | for (Item item : this)
|
|---|
| 109 | s.append(item + " ");
|
|---|
| 110 | return s.toString();
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | // check internal invariants
|
|---|
| 115 | private boolean check() {
|
|---|
| 116 | if (N == 0) {
|
|---|
| 117 | if (first != null) return false;
|
|---|
| 118 | }
|
|---|
| 119 | else if (N == 1) {
|
|---|
| 120 | if (first == null) return false;
|
|---|
| 121 | if (first.next != null) return false;
|
|---|
| 122 | }
|
|---|
| 123 | else {
|
|---|
| 124 | if (first.next == null) return false;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | // check internal consistency of instance variable N
|
|---|
| 128 | int numberOfNodes = 0;
|
|---|
| 129 | for (Node x = first; x != null; x = x.next) {
|
|---|
| 130 | numberOfNodes++;
|
|---|
| 131 | }
|
|---|
| 132 | if (numberOfNodes != N) return false;
|
|---|
| 133 |
|
|---|
| 134 | return true;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 | /**
|
|---|
| 139 | * Return an iterator to the stack that iterates through the items in LIFO order.
|
|---|
| 140 | */
|
|---|
| 141 | @Override
|
|---|
| 142 | public Iterator<Item> iterator() { return new ListIterator(); }
|
|---|
| 143 |
|
|---|
| 144 | // an iterator, doesn't implement remove() since it's optional
|
|---|
| 145 | private class ListIterator implements Iterator<Item> {
|
|---|
| 146 | private Node current = first;
|
|---|
| 147 | @Override
|
|---|
| 148 | public boolean hasNext() { return current != null; }
|
|---|
| 149 | @Override
|
|---|
| 150 | public void remove() { throw new UnsupportedOperationException(); }
|
|---|
| 151 |
|
|---|
| 152 | @Override
|
|---|
| 153 | public Item next() {
|
|---|
| 154 | if (!hasNext()) throw new NoSuchElementException();
|
|---|
| 155 | Item item = current.item;
|
|---|
| 156 | current = current.next;
|
|---|
| 157 | return item;
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 | ///**
|
|---|
| 163 | // * A test client.
|
|---|
| 164 | // */
|
|---|
| 165 | // public static void main(String[] args) {
|
|---|
| 166 | // Stack<String> s = new Stack<String>();
|
|---|
| 167 | // while (!StdIn.isEmpty()) {
|
|---|
| 168 | // String item = StdIn.readString();
|
|---|
| 169 | // if (!item.equals("-")) s.push(item);
|
|---|
| 170 | // else if (!s.isEmpty()) StdOut.print(s.pop() + " ");
|
|---|
| 171 | // }
|
|---|
| 172 | // StdOut.println("(" + s.size() + " left on stack)");
|
|---|
| 173 | // }
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|