-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMQueue.java
More file actions
77 lines (55 loc) · 2.01 KB
/
MQueue.java
File metadata and controls
77 lines (55 loc) · 2.01 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
76
77
package structure;
import java.util.*;
public class MQueue {
public static void run() {
System.out.println("\n==== Queue ====\n");
/*
* Queue = FIFO data Structure. First-In First-Out (ex: A line of people)
*
* A collection designed for holding elements prior to processing
* Linear Data Structure
*
* add = enqueue, offer()
* remove = dequeue = poll()
*
*/
Queue<String> queue = new LinkedList<String>();
queue.offer("Karen");
queue.offer("Chad");
queue.offer("Steve");
queue.offer("Harold");
System.out.println("First: " + queue.peek());
System.out.println("Entire Queue: " + queue);
queue.poll();
System.out.println("1° queue.poll(): " + queue);
String name = queue.poll();
System.out.println("Who leaves the queue now: " + name);
System.out.println(queue);
// Validating if it's empty
System.out.println("Is the queue empty: " + queue.isEmpty());
// Validating the size
System.out.println("Queue Size: " + queue.size());
// Validating if the queue contains some person:
System.out.println("Queue contains Kare? -> " + queue.contains("Karen"));
System.out.println("Queue contains Harold? -> " + queue.contains("Harold"));
/**
*
* Collection.add() method is common to use to add an element into queue
* whe error exceptions is a common thing to occur.
*
* The Collection.offer() method will offer the value to validate if it can be
* inserted into the Queue. If it can, it's inserted, otherwise the method
* returns false.
*
* the same occurs with the following methods:
*
* Remove remove() poll()
* Examine element() peek()
*
*/
// Where are queues useful?
// 1. Keyboard Buffer (letters should appear on the screen in order they're pressed)
// 2. Printer Queue (Print jobs, should be completed in order)
// 3. Used in LinkedLists, PriorityQueues, Breadth-first search
}
}