-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackList.java
More file actions
152 lines (123 loc) · 2.74 KB
/
Copy pathStackList.java
File metadata and controls
152 lines (123 loc) · 2.74 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.util.Iterator;
/**
* A stack implementation using a linked list.
* @author Daniel Pantyukhov & Valentim Khakhitva
* @version 1.0 [public]
*/
public class StackList<T>
{
/**
* A node class that contains the content of the node and a reference to the next node.
* @param <T> The type of the content of the node.
* @param content The content of the node.
* @param next The reference to the next node.
*/
private class Node {
private T content;
private Node next;
public Node(T content) {
this.content = content;
this.next = null;
}
}
private Node top;
private int size;
/**
* Constructor for the StackList class.
*/
public StackList()
{
top = null;
size = 0;
}
/**
* @return The size of the stack.
*/
public int size()
{
return size;
}
/**
* A shallow copy in this case is point to the same objects as in the original stack, but the new stack itself is a different object.
* @return A shallow copy of the stack.
*/
public StackList<T> shallowCopy()
{
StackList<T> copy = new StackList<>();
if (!this.isEmpty()) {
Node currentNode = this.top;
Node copyNode = new Node(currentNode.content);
copy.top = copyNode;
currentNode = currentNode.next;
while (currentNode != null) {
copyNode.next = new Node(currentNode.content);
copyNode = copyNode.next;
currentNode = currentNode.next;
}
copy.size = this.size;
}
return copy;
}
public boolean isEmpty()
{
return size == 0;
}
/**
* Adds an item to the top of the stack.
* @param item The item to be added to the stack (Content).
*/
public void push(T item)
{
Node node = new Node(item);
node.next = top;
top = node;
size++;
}
/**
* Removes the item from the top of the stack.
* @return The item that was removed from the top of the stack.
*/
public T pop()
{
if (isEmpty()) return null;
T item = top.content;
top = top.next;
size--;
return item;
}
/**
* Returns the item from the top of the stack without removing it.
* @return The item from the top of the stack.
*/
public T peek() {
if (isEmpty()) return null;
return top.content;
}
public Iterator<T> iterator()
{
return new StackListIterator();
}
/**
* An iterator class that iterates through the stack from the top to the bottom.
* @param <T> The type of the content of the stack.
*/
private class StackListIterator implements Iterator<T> {
private Node current = top;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public T next() {
if (!hasNext()) {
throw new IllegalStateException("There is no next element");
}
T item = current.content;
current = current.next;
return item;
}
}
public static void main(String[] args)
{
}
}