-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueLLImplementation.java
More file actions
67 lines (53 loc) · 1.15 KB
/
QueueLLImplementation.java
File metadata and controls
67 lines (53 loc) · 1.15 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
package DSImplementations;
public class QueueLLImplementation<T> {
static class Node<T> {
T data;
Node<T> next;
public Node(T data) {
this.data = data;
this.next = null;
}
}
int size = 0;
Node<T> head, tail;
public QueueLLImplementation() {
this.head = null;
this.tail = null;
}
void enqueue(T data) {
Node<T> newNode = new Node<>(data);
if (this.tail == null) {
this.tail = this.head = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
size++;
}
T dequeue() {
if (this.head == null) {
return null;
}
T temp = this.head.data;
this.head = this.head.next;
if (this.head == null) {
this.tail = null;
}
size--;
return temp;
}
public T peek() {
if (this.head == null) {
return null;
}
return this.head.data;
}
void print() {
Node<T> temp = this.head;
while (temp != null) {
System.out.printf("%s > ", temp.data);
temp = temp.next;
}
System.out.print("null\n");
}
}