|
| 1 | +/** |
| 2 | +* Implementation of a stack using nodes. |
| 3 | +* Unlimited size, no arraylist. |
| 4 | +* |
| 5 | +* @author Kyler Smith, 2017 |
| 6 | +*/ |
| 7 | + |
| 8 | + |
| 9 | +public class NodeStack<Item> { |
| 10 | + |
| 11 | + /** |
| 12 | + * Entry point for the program. |
| 13 | + */ |
| 14 | + public static void main(String[] args) { |
| 15 | + NodeStack<Integer> Stack = new NodeStack<Integer>(); |
| 16 | + |
| 17 | + Stack.push(3); |
| 18 | + Stack.push(4); |
| 19 | + Stack.push(5); |
| 20 | + System.out.println("Testing :"); |
| 21 | + Stack.print(); // prints : 5 4 3 |
| 22 | + |
| 23 | + Integer x = Stack.pop(); // x = 5 |
| 24 | + Stack.push(1); |
| 25 | + Stack.push(8); |
| 26 | + Integer y = Stack.peek(); // y = 8 |
| 27 | + System.out.println("Testing :"); |
| 28 | + Stack.print(); // prints : 8 1 4 3 |
| 29 | + |
| 30 | + System.out.println("Testing :"); |
| 31 | + System.out.println("x : " + x); |
| 32 | + System.out.println("y : " + y); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Information each node should contain. |
| 37 | + * @value data : information of the value in the node |
| 38 | + * @value head : the head of the stack |
| 39 | + * @value next : the next value from this node |
| 40 | + * @value previous : the last value from this node |
| 41 | + * @value size : size of the stack |
| 42 | + */ |
| 43 | + private Item data; |
| 44 | + private static NodeStack<?> head; |
| 45 | + private NodeStack<?> next; |
| 46 | + private NodeStack<?> previous; |
| 47 | + private static int size = 0; |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * Constructors for the NodeStack. |
| 52 | + */ |
| 53 | + public NodeStack() { |
| 54 | + } |
| 55 | + |
| 56 | + private NodeStack(Item item) { |
| 57 | + this.data = item; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Put a value onto the stack. |
| 62 | + * |
| 63 | + * @param item : value to be put on the stack. |
| 64 | + */ |
| 65 | + public void push(Item item) { |
| 66 | + |
| 67 | + NodeStack<Item> newNs = new NodeStack<Item>(item); |
| 68 | + |
| 69 | + if(this.isEmpty()) { |
| 70 | + NodeStack.setHead(new NodeStack<>(item)); |
| 71 | + newNs.setNext(null); |
| 72 | + newNs.setPrevious(null); |
| 73 | + } else { |
| 74 | + newNs.setPrevious(NodeStack.head); |
| 75 | + NodeStack.head.setNext(newNs); |
| 76 | + NodeStack.head = newNs; |
| 77 | + } |
| 78 | + |
| 79 | + NodeStack.setSize(NodeStack.getSize() + 1); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Value to be taken off the stack. |
| 84 | + * |
| 85 | + * @return item : value that is returned. |
| 86 | + */ |
| 87 | + public Item pop() { |
| 88 | + |
| 89 | + Item item = (Item) NodeStack.head.getData(); |
| 90 | + |
| 91 | + NodeStack.head = NodeStack.head.getPrevious(); |
| 92 | + NodeStack.head.setNext(null); |
| 93 | + |
| 94 | + NodeStack.setSize(NodeStack.getSize() - 1); |
| 95 | + |
| 96 | + return item; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Value that is next to be taken off the stack. |
| 101 | + * |
| 102 | + * @return item : the next value that would be popped off the stack. |
| 103 | + */ |
| 104 | + public Item peek() { |
| 105 | + return (Item) NodeStack.head.getData(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * If the stack is empty or there is a value in. |
| 110 | + * |
| 111 | + * @return boolean : whether or not the stack has anything in it. |
| 112 | + */ |
| 113 | + public boolean isEmpty() { |
| 114 | + return NodeStack.getSize() == 0; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Returns the size of the stack. |
| 119 | + * |
| 120 | + * @return int : number of values in the stack. |
| 121 | + */ |
| 122 | + public int size() { |
| 123 | + return NodeStack.getSize(); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Print the contents of the stack in the following format. |
| 128 | + * |
| 129 | + * x <- head (next out) |
| 130 | + * y |
| 131 | + * z <- tail (first in) |
| 132 | + * . |
| 133 | + * . |
| 134 | + * . |
| 135 | + * |
| 136 | + */ |
| 137 | + public void print() { |
| 138 | + for(NodeStack<?> n = NodeStack.head; n != null; n = n.previous) { |
| 139 | + System.out.println(n.getData().toString()); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** Getters and setters (private) */ |
| 144 | + private NodeStack<?> getHead() { |
| 145 | + return NodeStack.head; |
| 146 | + } |
| 147 | + |
| 148 | + private static void setHead(NodeStack<?> ns) { |
| 149 | + NodeStack.head = ns; |
| 150 | + } |
| 151 | + |
| 152 | + private NodeStack<?> getNext() { |
| 153 | + return next; |
| 154 | + } |
| 155 | + |
| 156 | + private void setNext(NodeStack<?> next) { |
| 157 | + this.next = next; |
| 158 | + } |
| 159 | + |
| 160 | + private NodeStack<?> getPrevious() { |
| 161 | + return previous; |
| 162 | + } |
| 163 | + |
| 164 | + private void setPrevious(NodeStack<?> previous) { |
| 165 | + this.previous = previous; |
| 166 | + } |
| 167 | + |
| 168 | + private static int getSize() { |
| 169 | + return size; |
| 170 | + } |
| 171 | + |
| 172 | + private static void setSize(int size) { |
| 173 | + NodeStack.size = size; |
| 174 | + } |
| 175 | + |
| 176 | + private Item getData() { |
| 177 | + return this.data; |
| 178 | + } |
| 179 | + |
| 180 | + private void setData(Item item) { |
| 181 | + this.data = item; |
| 182 | + } |
| 183 | +} |
0 commit comments