Skip to content

Commit fdaa4e9

Browse files
committed
Update Queue_Learn.java
1 parent 6ad7847 commit fdaa4e9

File tree

1 file changed

+88
-85
lines changed

1 file changed

+88
-85
lines changed

Queue_Learn.java

Lines changed: 88 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,146 +10,149 @@
1010
public class Queue_Learn {
1111

1212
public static void main(String[] args) {
13-
13+
1414
/*
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.
1920
*/
20-
21+
2122
/*
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.
2425
*/
25-
26+
2627
Queue<Integer> demoQueue = new LinkedList<>();
27-
28+
2829
/*
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.
3637
*/
37-
38-
demoQueue.add(30);
38+
39+
demoQueue.add(30);
3940
// demoQueue = [30]
40-
41-
demoQueue.add(10);
41+
42+
demoQueue.add(10);
4243
// demoQueue = [30, 10]
43-
44-
demoQueue.add(50);
44+
45+
demoQueue.add(50);
4546
// demoQueue = [30, 10, 50]
46-
47+
4748
System.out.println("demoQueue = " + demoQueue); // demoQueue = [30, 10, 50]
48-
49-
demoQueue.offer(20);
49+
50+
demoQueue.offer(20);
5051
// demoQueue = [30, 10, 50, 20]
51-
52-
demoQueue.offer(40);
52+
53+
demoQueue.offer(40);
5354
// demoQueue = [30, 10, 50, 20, 40]
54-
55+
5556
System.out.println("demoQueue = " + demoQueue); // demoQueue = [30, 10, 50, 20, 40]
56-
57+
5758
/*
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.
6364
*/
64-
65-
demoQueue.remove();
65+
66+
demoQueue.remove();
6667
// demoQueue = [10, 50, 20, 40]
67-
68+
6869
System.out.println("demoQueue = " + demoQueue); // demoQueue = [10, 50, 20, 40]
69-
70-
int removedElement = demoQueue.remove();
70+
71+
int removedElement = demoQueue.remove();
7172
// demoQueue = [50, 20, 40]
72-
73+
7374
System.out.println("Element removed = " + removedElement); // Element removed = 10
74-
75+
7576
/*
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.
8081
*/
81-
82+
8283
int headElement = demoQueue.peek();
8384
// demoQueue = [50, 20, 40]
84-
85+
8586
System.out.println("Head element = " + headElement); // Head element = 50
86-
87+
8788
/*
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.
9294
*/
93-
95+
9496
headElement = demoQueue.poll();
9597
// demoQueue = [20, 40]
96-
98+
9799
System.out.println("Head element = " + headElement); // Head element = 50
98-
100+
99101
demoQueue.poll();
100102
// demoQueue = [40]
101-
103+
102104
System.out.println("demoQueue = " + demoQueue); // demoQueue = [40]
103-
105+
104106
/*
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.
109111
*/
110-
112+
111113
int queueSize = demoQueue.size();
112-
114+
113115
System.out.println("Size = " + queueSize); // Size = 1
114-
116+
115117
/*
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.
120122
*/
121-
123+
122124
if (demoQueue.isEmpty())
123125
System.out.println("Queue is empty !");
124126
else
125127
System.out.println("Queue is not empty !");
126-
128+
127129
/*
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.
132135
*/
133-
136+
134137
int value = 40;
135-
138+
136139
if (demoQueue.contains(value))
137140
System.out.println("Queue contains " + value);
138141
else
139142
System.out.println("Queue does not contain " + value);
140-
143+
141144
/*
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.
146149
*/
147-
150+
148151
demoQueue.clear();
149152
// demoQueue = []
150-
153+
151154
System.out.println("demoQueue = " + demoQueue); // demoQueue = []
152-
155+
153156
}
154157

155158
}

0 commit comments

Comments
 (0)