-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
48 lines (34 loc) · 777 Bytes
/
Queue.java
File metadata and controls
48 lines (34 loc) · 777 Bytes
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
package Queue;
public class Queue<T extends Comparable<T>> {
private Node<T> firstNode;
private Node<T> lastNode;
private int count;
public boolean isEmpty(){
return this.firstNode == null;
}
public int size(){
return this.count;
}
// O(1)
public void enqueue(T newData){
this.count++;
Node<T> oldLastNode = this.lastNode;
this.lastNode = new Node<>(newData);
this.lastNode.setNextNode(null);
if( isEmpty() ){
this.firstNode = this.lastNode;
}else{
oldLastNode.setNextNode(this.lastNode);
}
}
// O(1)
public T dequeue(){
this.count--;
T dataToDequeue = this.firstNode.getData();
this.firstNode=this.firstNode.getNextNode();
if( isEmpty() ){
this.lastNode = null;
}
return dataToDequeue;
}
}