-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedStack.java
More file actions
50 lines (42 loc) · 1.18 KB
/
LinkedStack.java
File metadata and controls
50 lines (42 loc) · 1.18 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
package Etapa4;
public class LinkedStack<E> implements Stack<E> {
private Node<E> top;
private int count;
public LinkedStack() {
top = null;
count = 0;
}
@Override
public boolean isEmpty() {
return top == null;
}
@Override
public boolean isFull() {
// Pilha encadeada nunca fica cheia (sem limite fixo)
return false;
}
@Override
public void push(E element) throws OverflowException {
Node<E> newNode = new Node<>(element); // usa o construtor com 1 argumento
newNode.setNext(top); // conecta o novo nó ao antigo topo
top = newNode; // atualiza o topo
count++;
}
@Override
public E pop() throws UnderflowException {
if (isEmpty()) throw new UnderflowException();
E element = top.getElement();
top = top.getNext();
count--;
return element;
}
@Override
public E top() throws UnderflowException {
if (isEmpty()) throw new UnderflowException();
return top.getElement();
}
@Override
public int numElements() {
return count;
}
}