-
Notifications
You must be signed in to change notification settings - Fork 20
/
CustomrQueue.java
74 lines (61 loc) · 1.56 KB
/
CustomrQueue.java
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
public class CustomrQueue {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public static Node head;
public static Node tail;
public static int size;
public void enqueue(int customerID) {
Node newNode = new Node(customerID);
size++;
if (head == null) {
head = tail = newNode;
return;
}
tail.next = newNode;
tail = newNode;
}
public int dequeue() {
if (head == null) {
System.out.println("Queue is empty");
return -1;
}
int val = head.data;
head = head.next;
size--;
return val;
}
public int size() {
return size;
}
public void print() {
Node temp = head;
while (temp != null) {
System.out.println(temp.data);
temp = temp.next;
}
}
public static void main(String[] args) {
CustomrQueue q = new CustomrQueue();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
System.out.println("Intial Queue");
q.print();
q.dequeue();
q.dequeue();
System.out.println("Queue after two customers are serviced");
q.print();
q.enqueue(4);
q.dequeue();
q.dequeue();
System.out.println("Queue after one new customer joins in and two customers are serviced");
q.print();
System.out.println("Size of queue=>"+q.size());
}
}