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