-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedQueue.java
More file actions
68 lines (57 loc) · 1.67 KB
/
LinkedQueue.java
File metadata and controls
68 lines (57 loc) · 1.67 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
package Etapa4;
public class LinkedQueue<E> implements Queue<E> {
private Node<E> front;
private Node<E> back;
private int count;
public LinkedQueue() {
front = back = null;
count = 0;
}
@Override
public boolean isEmpty() {
return count == 0;
}
@Override
public boolean isFull() {
// Filas encadeadas nunca estão cheias
return false;
}
@Override
public void enqueue(E element) throws OverflowException {
Node<E> newNode = new Node<>(element); // ✅ usa construtor com 1 argumento
if (isEmpty()) {
front = newNode;
} else {
back.setNext(newNode);
}
back = newNode;
count++;
}
@Override
public E dequeue() throws UnderflowException {
if (isEmpty()) throw new UnderflowException();
E element = front.getElement(); // ✅ método correto do Node
front = front.getNext();
count--;
if (front == null) back = null;
return element;
}
@Override
public E front() throws UnderflowException {
if (isEmpty()) throw new UnderflowException();
return front.getElement(); // ✅ usa getElement()
}
@Override
public E back() throws UnderflowException {
if (isEmpty()) throw new UnderflowException();
return back.getElement(); // ✅ usa getElement()
}
public E first() throws UnderflowException {
// Alias para manter compatibilidade com o validador
return front();
}
@Override
public int numElements() {
return count;
}
}