forked from scottnakada/HackerRank-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackInt.java
More file actions
38 lines (32 loc) · 934 Bytes
/
StackInt.java
File metadata and controls
38 lines (32 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package stackQueue;
/* Manually implement a Java Stack of integers */
public class StackInt extends StackQueueInt {
/* Constructor for the StackInt Class */
StackInt() {
/* Create the superClass */
super();
/* Initialize the head to be null */
head = null;
}
/* Push data onto the stack */
public void push (int newData) {
/* Create a new node to add to the stack */
Node newNode = new Node(newData);
/* Initialize the next pointer on the new node to point to the previous head */
newNode.next = head;
/* The new head is the new Node */
head = newNode;
}
/* Pop data off the stack */
public int pop () {
if (head == null) {
throw new IllegalStateException("Can't pop off of an empty list");
}
/* Save the value to return */
int returnValue = head.data;
/* Remove the node from the head of the Stack */
head = head.next;
/* Return the popped value */
return returnValue;
}
}