|
10 | 10 | public class Queue_Learn { |
11 | 11 |
|
12 | 12 | public static void main(String[] args) { |
13 | | - |
| 13 | + |
14 | 14 | /* |
15 | | - * The 'Queue' interface extends 'Collection' interface and declares the behaviour of a queue. |
16 | | - * 'Queue' being an interface needs a class which provides implementation to its methods |
17 | | - * and also that we can create objects of that class. 'LinkedList' and 'PriorityQueue' |
18 | | - * are the most commonly used classes used while creating a queue object. |
| 15 | + * The 'Queue' interface extends 'Collection' interface and declares the |
| 16 | + * behaviour of a queue. 'Queue' being an interface needs a class which provides |
| 17 | + * implementation to its methods and also that we can create objects of that |
| 18 | + * class. 'LinkedList' and 'PriorityQueue' are the most commonly used classes |
| 19 | + * used while creating a queue object. |
19 | 20 | */ |
20 | | - |
| 21 | + |
21 | 22 | /* |
22 | | - * 'LinkedList' class extends 'AbstractSequentialList' class and implements the 'List', |
23 | | - * 'Queue' and 'Deque' interfaces. |
| 23 | + * 'LinkedList' class extends 'AbstractSequentialList' class and implements the |
| 24 | + * 'List', 'Queue' and 'Deque' interfaces. |
24 | 25 | */ |
25 | | - |
| 26 | + |
26 | 27 | Queue<Integer> demoQueue = new LinkedList<>(); |
27 | | - |
| 28 | + |
28 | 29 | /* |
29 | | - * Add an element to the queue |
30 | | - * |
31 | | - * boolean add(E obj) : Declared in the Collection interface. Adds object to the collection. |
32 | | - * Returns true if object was added, otherwise returns false. |
33 | | - * |
34 | | - * boolean offer(E obj) : Declared in the Queue interface. Adds object to the queue. |
35 | | - * Returns true if object was added, otherwise returns false. |
| 30 | + * Add an element to the queue |
| 31 | + * |
| 32 | + * boolean add(E obj) : Declared in the Collection interface. Adds object to the |
| 33 | + * collection. Returns true if object was added, otherwise returns false. |
| 34 | + * |
| 35 | + * boolean offer(E obj) : Declared in the Queue interface. Adds object to the |
| 36 | + * queue. Returns true if object was added, otherwise returns false. |
36 | 37 | */ |
37 | | - |
38 | | - demoQueue.add(30); |
| 38 | + |
| 39 | + demoQueue.add(30); |
39 | 40 | // demoQueue = [30] |
40 | | - |
41 | | - demoQueue.add(10); |
| 41 | + |
| 42 | + demoQueue.add(10); |
42 | 43 | // demoQueue = [30, 10] |
43 | | - |
44 | | - demoQueue.add(50); |
| 44 | + |
| 45 | + demoQueue.add(50); |
45 | 46 | // demoQueue = [30, 10, 50] |
46 | | - |
| 47 | + |
47 | 48 | System.out.println("demoQueue = " + demoQueue); // demoQueue = [30, 10, 50] |
48 | | - |
49 | | - demoQueue.offer(20); |
| 49 | + |
| 50 | + demoQueue.offer(20); |
50 | 51 | // demoQueue = [30, 10, 50, 20] |
51 | | - |
52 | | - demoQueue.offer(40); |
| 52 | + |
| 53 | + demoQueue.offer(40); |
53 | 54 | // demoQueue = [30, 10, 50, 20, 40] |
54 | | - |
| 55 | + |
55 | 56 | System.out.println("demoQueue = " + demoQueue); // demoQueue = [30, 10, 50, 20, 40] |
56 | | - |
| 57 | + |
57 | 58 | /* |
58 | | - * Remove an element from the queue |
59 | | - * |
60 | | - * E remove() : Declared in the Queue interface. Removes the element at the head |
61 | | - * of the queue returning the element in the process. It throws |
62 | | - * NoSuchElementException if the queue is empty. |
| 59 | + * Remove an element from the queue |
| 60 | + * |
| 61 | + * E remove() : Declared in the Queue interface. Removes the element at the head |
| 62 | + * of the queue returning the element in the process. It throws |
| 63 | + * NoSuchElementException if the queue is empty. |
63 | 64 | */ |
64 | | - |
65 | | - demoQueue.remove(); |
| 65 | + |
| 66 | + demoQueue.remove(); |
66 | 67 | // demoQueue = [10, 50, 20, 40] |
67 | | - |
| 68 | + |
68 | 69 | System.out.println("demoQueue = " + demoQueue); // demoQueue = [10, 50, 20, 40] |
69 | | - |
70 | | - int removedElement = demoQueue.remove(); |
| 70 | + |
| 71 | + int removedElement = demoQueue.remove(); |
71 | 72 | // demoQueue = [50, 20, 40] |
72 | | - |
| 73 | + |
73 | 74 | System.out.println("Element removed = " + removedElement); // Element removed = 10 |
74 | | - |
| 75 | + |
75 | 76 | /* |
76 | | - * Get the element at the head of the queue |
77 | | - * |
78 | | - * E peek() : Declared in the Queue interface. Returns the element at the head |
79 | | - * of the queue. It returns null if the queue is empty. |
| 77 | + * Get the element at the head of the queue |
| 78 | + * |
| 79 | + * E peek() : Declared in the Queue interface. Returns the element at the head |
| 80 | + * of the queue. It returns null if the queue is empty. |
80 | 81 | */ |
81 | | - |
| 82 | + |
82 | 83 | int headElement = demoQueue.peek(); |
83 | 84 | // demoQueue = [50, 20, 40] |
84 | | - |
| 85 | + |
85 | 86 | System.out.println("Head element = " + headElement); // Head element = 50 |
86 | | - |
| 87 | + |
87 | 88 | /* |
88 | | - * Get & remove the element at the head of the queue |
89 | | - * |
90 | | - * E poll() : Declared in the Queue interface. Returns the element at the head of the queue, |
91 | | - * removing the element in the process. It returns null if the queue is empty. |
| 89 | + * Get & remove the element at the head of the queue |
| 90 | + * |
| 91 | + * E poll() : Declared in the Queue interface. Returns the element at the head |
| 92 | + * of the queue, removing the element in the process. It returns null if the |
| 93 | + * queue is empty. |
92 | 94 | */ |
93 | | - |
| 95 | + |
94 | 96 | headElement = demoQueue.poll(); |
95 | 97 | // demoQueue = [20, 40] |
96 | | - |
| 98 | + |
97 | 99 | System.out.println("Head element = " + headElement); // Head element = 50 |
98 | | - |
| 100 | + |
99 | 101 | demoQueue.poll(); |
100 | 102 | // demoQueue = [40] |
101 | | - |
| 103 | + |
102 | 104 | System.out.println("demoQueue = " + demoQueue); // demoQueue = [40] |
103 | | - |
| 105 | + |
104 | 106 | /* |
105 | | - * Get the count of elements present in the queue |
106 | | - * |
107 | | - * int size() : Declared in the Collection interface. Returns the number of elements |
108 | | - * held in the invoking collection. |
| 107 | + * Get the count of elements present in the queue |
| 108 | + * |
| 109 | + * int size() : Declared in the Collection interface. Returns the number of |
| 110 | + * elements held in the invoking collection. |
109 | 111 | */ |
110 | | - |
| 112 | + |
111 | 113 | int queueSize = demoQueue.size(); |
112 | | - |
| 114 | + |
113 | 115 | System.out.println("Size = " + queueSize); // Size = 1 |
114 | | - |
| 116 | + |
115 | 117 | /* |
116 | | - * Check if queue is empty or not |
117 | | - * |
118 | | - * boolean isEmpty() : Declared in the Collection interface. Returns true if the |
119 | | - * invoking collection is empty. Otherwise, returns false. |
| 118 | + * Check if queue is empty or not |
| 119 | + * |
| 120 | + * boolean isEmpty() : Declared in the Collection interface. Returns true if the |
| 121 | + * invoking collection is empty. Otherwise, returns false. |
120 | 122 | */ |
121 | | - |
| 123 | + |
122 | 124 | if (demoQueue.isEmpty()) |
123 | 125 | System.out.println("Queue is empty !"); |
124 | 126 | else |
125 | 127 | System.out.println("Queue is not empty !"); |
126 | | - |
| 128 | + |
127 | 129 | /* |
128 | | - * Check if an object is present the queue |
129 | | - * |
130 | | - * boolean contains(Object obj) : Declared in the Collection interface. Returns true if obj |
131 | | - * is an element of the invoking collection. Otherwise, returns false. |
| 130 | + * Check if an object is present the queue |
| 131 | + * |
| 132 | + * boolean contains(Object obj) : Declared in the Collection interface. Returns |
| 133 | + * true if obj is an element of the invoking collection. Otherwise, returns |
| 134 | + * false. |
132 | 135 | */ |
133 | | - |
| 136 | + |
134 | 137 | int value = 40; |
135 | | - |
| 138 | + |
136 | 139 | if (demoQueue.contains(value)) |
137 | 140 | System.out.println("Queue contains " + value); |
138 | 141 | else |
139 | 142 | System.out.println("Queue does not contain " + value); |
140 | | - |
| 143 | + |
141 | 144 | /* |
142 | | - * Clear the queue |
143 | | - * |
144 | | - * void clear() : Declared in the Collection interface. Removes all elements |
145 | | - * from the invoking collection. |
| 145 | + * Clear the queue |
| 146 | + * |
| 147 | + * void clear() : Declared in the Collection interface. Removes all elements |
| 148 | + * from the invoking collection. |
146 | 149 | */ |
147 | | - |
| 150 | + |
148 | 151 | demoQueue.clear(); |
149 | 152 | // demoQueue = [] |
150 | | - |
| 153 | + |
151 | 154 | System.out.println("demoQueue = " + demoQueue); // demoQueue = [] |
152 | | - |
| 155 | + |
153 | 156 | } |
154 | 157 |
|
155 | 158 | } |
0 commit comments