|
1 | 1 | package com.thealgorithms.datastructures.stacks;
|
2 | 2 |
|
3 | 3 | /**
|
4 |
| - * Implementation of a stack using nodes. Unlimited size, no arraylist. |
| 4 | + * A stack implementation using linked nodes, supporting unlimited size without an ArrayList. |
5 | 5 | *
|
6 |
| - * @author Kyler Smith, 2017 |
| 6 | + * <p>Each node in the stack contains data of generic type {@code Item}, along with references |
| 7 | + * to the next and previous nodes, supporting typical stack operations. |
| 8 | + * |
| 9 | + * <p>The stack follows a Last-In-First-Out (LIFO) order where elements added last are |
| 10 | + * removed first. Supported operations include push, pop, and peek. |
| 11 | + * |
| 12 | + * @param <Item> the type of elements held in this stack |
7 | 13 | */
|
8 | 14 | public class NodeStack<Item> {
|
9 | 15 |
|
10 | 16 | /**
|
11 |
| - * Entry point for the program. |
| 17 | + * Node class representing each element in the stack. |
12 | 18 | */
|
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 |
| 19 | + private class Node { |
| 20 | + Item data; |
| 21 | + Node previous; |
21 | 22 |
|
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); |
| 23 | + Node(Item data) { |
| 24 | + this.data = data; |
| 25 | + this.previous = null; |
| 26 | + } |
32 | 27 | }
|
33 | 28 |
|
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 |
| - private Item data; |
44 |
| - |
45 |
| - private static NodeStack<?> head; |
46 |
| - private NodeStack<?> previous; |
47 |
| - private static int size = 0; |
| 29 | + private Node head; // Top node in the stack |
| 30 | + private int size; // Number of elements in the stack |
48 | 31 |
|
49 | 32 | /**
|
50 |
| - * Constructors for the NodeStack. |
| 33 | + * Constructs an empty NodeStack. |
51 | 34 | */
|
52 | 35 | public NodeStack() {
|
53 |
| - } |
54 |
| - |
55 |
| - private NodeStack(Item item) { |
56 |
| - this.data = item; |
| 36 | + head = null; |
| 37 | + size = 0; |
57 | 38 | }
|
58 | 39 |
|
59 | 40 | /**
|
60 |
| - * Put a value onto the stack. |
| 41 | + * Pushes an item onto the stack. |
61 | 42 | *
|
62 |
| - * @param item : value to be put on the stack. |
| 43 | + * @param item the item to be pushed onto the stack |
63 | 44 | */
|
64 | 45 | public void push(Item item) {
|
65 |
| - NodeStack<Item> newNs = new NodeStack<Item>(item); |
66 |
| - |
67 |
| - if (this.isEmpty()) { |
68 |
| - NodeStack.setHead(new NodeStack<>(item)); |
69 |
| - newNs.setNext(null); |
70 |
| - newNs.setPrevious(null); |
71 |
| - } else { |
72 |
| - newNs.setPrevious(NodeStack.head); |
73 |
| - NodeStack.head.setNext(newNs); |
74 |
| - NodeStack.setHead(newNs); |
75 |
| - } |
76 |
| - |
77 |
| - NodeStack.setSize(NodeStack.getSize() + 1); |
| 46 | + Node newNode = new Node(item); |
| 47 | + newNode.previous = head; |
| 48 | + head = newNode; |
| 49 | + size++; |
78 | 50 | }
|
79 | 51 |
|
80 | 52 | /**
|
81 |
| - * Value to be taken off the stack. |
| 53 | + * Removes and returns the item at the top of the stack. |
82 | 54 | *
|
83 |
| - * @return item : value that is returned. |
| 55 | + * @return the item at the top of the stack, or {@code null} if the stack is empty |
| 56 | + * @throws IllegalStateException if the stack is empty |
84 | 57 | */
|
85 | 58 | public Item pop() {
|
86 |
| - Item item = (Item) NodeStack.head.getData(); |
87 |
| - |
88 |
| - NodeStack.setHead(NodeStack.head.getPrevious()); |
89 |
| - NodeStack.head.setNext(null); |
90 |
| - |
91 |
| - NodeStack.setSize(NodeStack.getSize() - 1); |
92 |
| - |
93 |
| - return item; |
| 59 | + if (isEmpty()) { |
| 60 | + throw new IllegalStateException("Cannot pop from an empty stack."); |
| 61 | + } |
| 62 | + Item data = head.data; |
| 63 | + head = head.previous; |
| 64 | + size--; |
| 65 | + return data; |
94 | 66 | }
|
95 | 67 |
|
96 | 68 | /**
|
97 |
| - * Value that is next to be taken off the stack. |
| 69 | + * Returns the item at the top of the stack without removing it. |
98 | 70 | *
|
99 |
| - * @return item : the next value that would be popped off the stack. |
| 71 | + * @return the item at the top of the stack, or {@code null} if the stack is empty |
| 72 | + * @throws IllegalStateException if the stack is empty |
100 | 73 | */
|
101 | 74 | public Item peek() {
|
102 |
| - return (Item) NodeStack.head.getData(); |
| 75 | + if (isEmpty()) { |
| 76 | + throw new IllegalStateException("Cannot peek from an empty stack."); |
| 77 | + } |
| 78 | + return head.data; |
103 | 79 | }
|
104 | 80 |
|
105 | 81 | /**
|
106 |
| - * If the stack is empty or there is a value in. |
| 82 | + * Checks whether the stack is empty. |
107 | 83 | *
|
108 |
| - * @return boolean : whether or not the stack has anything in it. |
| 84 | + * @return {@code true} if the stack has no elements, {@code false} otherwise |
109 | 85 | */
|
110 | 86 | public boolean isEmpty() {
|
111 |
| - return NodeStack.getSize() == 0; |
| 87 | + return head == null; |
112 | 88 | }
|
113 | 89 |
|
114 | 90 | /**
|
115 |
| - * Returns the size of the stack. |
| 91 | + * Returns the number of elements currently in the stack. |
116 | 92 | *
|
117 |
| - * @return int : number of values in the stack. |
| 93 | + * @return the size of the stack |
118 | 94 | */
|
119 | 95 | public int size() {
|
120 |
| - return NodeStack.getSize(); |
| 96 | + return size; |
121 | 97 | }
|
122 | 98 |
|
123 | 99 | /**
|
124 |
| - * Print the contents of the stack in the following format. |
125 |
| - * |
126 |
| - * <p> |
127 |
| - * x <- head (next out) y z <- tail (first in) . . . |
| 100 | + * Prints the contents of the stack from top to bottom. |
128 | 101 | */
|
129 | 102 | public void print() {
|
130 |
| - for (NodeStack<?> n = NodeStack.head; n != null; n = n.previous) { |
131 |
| - System.out.println(n.getData().toString()); |
| 103 | + Node current = head; |
| 104 | + while (current != null) { |
| 105 | + System.out.println(current.data); |
| 106 | + current = current.previous; |
132 | 107 | }
|
133 | 108 | }
|
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; |
160 |
| - } |
161 | 109 | }
|
0 commit comments