-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab2_2540_Q5.java
More file actions
75 lines (56 loc) · 1.47 KB
/
Copy pathLab2_2540_Q5.java
File metadata and controls
75 lines (56 loc) · 1.47 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
class Lab2_2540_Q5 {
QNode front, rear;
public Lab2_2540_Q5() {
this.front = this.rear = null;
}
void enqueue(int key) {
QNode temp = new QNode(key);
if (this.rear == null) {
this.front = this.rear = temp;
return;
}
this.rear.next = temp;
this.rear = temp;
}
public int dequeue()
{
if (this.front == null)
return -99;
int temp = this.front.key;
this.front = this.front.next;
if (this.front == null)
this.rear = null;
return temp;
}
public int getFirst() {
QNode n = this.front;
return n.key;
// System.out.println("FirstElement:" + n.key);
}
public int size() {
QNode node = this.front;
int c = 1;
while (node.next != null) {
node = node.next;
c++;
}
return c;
// System.out.println("Size:" + c);
}
public boolean isEmpty() {
if (this.front == null) {
return true;
} else {
return false;
}
}
public void show(QNode node) {
node = this.front;
while (node.next != null) {
System.out.println(node.key);
node = node.next;
}
System.out.println(node.key);
}
}
// Both enque and deque is O(1) as there are no loops in them.