-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackList.java
More file actions
134 lines (112 loc) · 3.68 KB
/
StackList.java
File metadata and controls
134 lines (112 loc) · 3.68 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Nabil Arbouz
Algorithms Spring 2020
Stack Linked List Exercise
*/
class StackList {
public static void main(String[] args) {
System.out.println("Pushing values onto the stack");
StackList testStack = new StackList(0);
testStack.printStack();
for(int i = 1; i <= 10; i++) {
testStack.push(i);
testStack.printStack();
}
System.out.println("-------------------------------");
System.out.println("Popping values from the stack");
for(int i = 0; i < 11; i++) {
testStack.pop();
System.out.print("The new stack is: ");
testStack.printStack();
}
//Edge Cases
// StackList edgeCaseStack = new StackList();
// edgeCaseStack.pop(); //Test the pop of an empty stack
//
// edgeCaseStack.push(1); //Push an item onto the empty stack
// System.out.println("Pushing a node onto the top of the stack was successful.");
//
// edgeCaseStack.push(-1); //Push a negative number onto the stack
// edgeCaseStack.push("hello world"); //Pushing a string onto the stack will produce an error
}
Node head;
Node top;
int numOfNodes;
// public StackList() {
// this.head = null;
// this.top = null;
// }
public StackList(int value) {
Node newNode = new Node(value, null);
numOfNodes += 1;
this.head = newNode;
this.top = newNode;
}
public void push(int val) {
Node newNode = new Node(val, null);
if (this.isNotEmpty()) {
this.top.next = newNode;
} else {
this.head = newNode;
}
this.top = newNode;
this.numOfNodes++;
}
public void pop() {
if (this.isNotEmpty()) {
if (numOfNodes > 1) {
Node previousNode = null;
Node currentNode = this.head;
while (currentNode.getNext() != null) {
previousNode = currentNode;
currentNode = currentNode.getNext();
}
System.out.println("Popping " + currentNode.value + " from the stack");
assert previousNode != null;
previousNode.setNext(null);
} else {
System.out.println("Popping " + this.head.value + " from the stack");
this.head = null;
this.top = null;
}
numOfNodes--;
} else {
System.out.println("The stack is empty. Pop operation not available.");
}
}
public void printStack() {
Node currentNode = head;
if (this.numOfNodes > 0) {
while (currentNode != null) {
System.out.print(currentNode.getValue() + " ");
currentNode = currentNode.getNext();
}
System.out.print('\n');
} else {
System.out.println("The stack is empty");
}
}
public boolean isNotEmpty() {
return this.top != null;
}
private static class Node {
private int value;
private Node next;
public Node(int value, Node nextNode) {
this.value = value;
this.next = nextNode;
}
public void setNext(Node newNext) {
this.next = newNext;
}
// public void setValue(int newVal) {
// this.value = newVal;
// }
public int getValue() {
return this.value;
}
public Node getNext() {
return this.next;
}
}
}