-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackAsList.java
104 lines (89 loc) · 2.16 KB
/
StackAsList.java
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
/**
* This class takes a Stack and converts it to a Linked List.
*
* @author robinwettstaedt
* @author n-c0de-r
* @version 21-06-03 update 21-06-07
*/
//Assignment 2
public class StackAsList implements Stack{
private Node first;
/**
* constructor for the stack as a linked list.
*/
public StackAsList() {
}
class Node {
String data;
Node next;
//Constructor of the Node class
public Node() {
}
}
/**
* Insert an element in the beginning of the list,
* as the top-most element of a stack.
* @return The String just stored in the Node.
*/
@Override
public String push(String element) {
Node node = new Node();
node.data = element;
node.next = first;
first = node;
return element;
}
/**
* Gets the first/ top-most element of the list,
* and removes it from the order and it's linking,
* @return String stored in the Node's data field.
*/
@Override
public String pop() {
if (!isEmpty()) {
String s = first.data;
first = first.next;
return s;
} else {
return null;
}
}
/**
* Just look into the data field of the top-most Node.
* * @return String stored in the Node's data field.
*/
@Override
public String peek() {
if (isEmpty())
return null;
else
return first.data;
}
/**
* Check if the list/stack is empty.
* @return Boolean, true if there are no Nodes in the list.
*/
@Override
public boolean isEmpty() {
if (first == null)
return true;
return false;
}
//not sure why we would need this.
@Override
public String toString() {
String s = "";
if (isEmpty()) {
return s;
}
Node current = first;
while (true) {
s = s + "[ " + current.data + " ] ";
if (current.next == null) {
break;
}
current = current.next;
}
return first.data;
}
}