-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathCarQueue.java
More file actions
36 lines (30 loc) · 1013 Bytes
/
CarQueue.java
File metadata and controls
36 lines (30 loc) · 1013 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
import java.util.LinkedList;
import java.util.Queue;
public class CarQueue {
public static void main(String[] args) {
Queue<String> carQueue = new LinkedList<>();
enqueue(carQueue, "Audi");
enqueue(carQueue, "Honda");
enqueue(carQueue, "Ford");
enqueue(carQueue, "Rolls-Royce");
printQueueContents(carQueue);
while (!carQueue.isEmpty()) {
String removedCar = dequeue(carQueue);
System.out.println(removedCar+" Removed ");
}
if (carQueue.isEmpty()) {
System.out.println("The queue is empty.");
} else {
System.out.println("The queue is not empty.");
}
}
public static void enqueue(Queue<String> queue, String car) {
queue.add(car);
}
public static String dequeue(Queue<String> queue) {
return queue.poll();
}
public static void printQueueContents(Queue<String> queue) {
System.out.println("The queue: " + queue);
}
}