-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGInterview.java
More file actions
62 lines (55 loc) · 1.34 KB
/
GInterview.java
File metadata and controls
62 lines (55 loc) · 1.34 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
public class GInterview {
public static class Queue {
class Node {
int data;
Node next;
Node(int data, Node next) {
this.data = data;
this.next = next;
}
}
Node head = null;
Node tail = null;
final int size;
int count = 0;
Queue(int size) {
this.size = size;
}
void enqueue(int data) {
if (head == null) {
head = new Node(data, null);
tail = head;
count++;
return;
}
Node n = new Node(data, null);
head.next = n;
head = n;
if (count == size) {
tail = tail.next;
} else {
count++;
}
}
double getAvg() {
int sum = 0;
int i = 0;
for (Node n = tail; n != null; n = n.next) {
sum += n.data;
i++;
}
return sum/i;
}
}
public static void main(String[] args) {
Queue q = new Queue(3);
q.enqueue(7);
System.out.println(q.getAvg());
q.enqueue(8);
System.out.println(q.getAvg());
q.enqueue(9);
System.out.println(q.getAvg());
q.enqueue(10);
System.out.println(q.getAvg());
}
}