44
55public class DoubleEndedQueue {
66 public static void main (String [] args ) {
7- // double-ended queue
8- // allows insertion and removal of elements from both ends
9- // versatile than regular queues and stacks because they support all the operations of both
7+ // a double-ended queue
8+ // allows insertion and removal of elements from both ends
9+ // versatile than regular queues and stacks because they support all the operations of both.
1010
1111 /*
1212 INSERTION METHODS
@@ -19,42 +19,37 @@ public static void main(String[] args) {
1919 */
2020
2121 /*
22-
2322 REMOVAL METHODS
2423
2524 removeFirst(): Retrieves and removes the first element.
2625 removeLast(): Retrieves and removes the last element.
2726 pollFirst(): Retrieves and removes the first element, or returns null if empty.
2827 pollLast(): Retrieves and removes the last element, or returns null if empty.
29-
3028 */
3129
3230 /*
33-
3431 EXAMINATION METHODS
3532
3633 getFirst(): Retrieves, but does not remove, the first element.
3734 getLast(): Retrieves, but does not remove, the last element.
3835 peekFirst(): Retrieves, but does not remove, the first element, or returns null if empty.
3936 peekLast(): Retrieves, but does not remove, the last element, or returns null if empty.
40-
4137 */
4238
4339 /*
44-
4540 STACK METHODS
4641
47- push(E e): Adds an element at the front (equivalent to addFirst(E e)).
48- pop(): Removes and returns the first element (equivalent to removeFirst())..
49-
42+ push(E e): Adds an element at the front (equivalent to addFirst(E e)).
43+ pop(): Removes and returns the first element (equivalent to removeFirst())..
5044 */
5145
52- Deque <Integer > deque1 = new ArrayDeque <>(); // faster iteration, low memory, no null allowed Reason why we write ArrayDeque<>().
46+ Deque <Integer > deque1 = new ArrayDeque <>();
47+ // faster iteration, low memory, no null allowed Reason why we write ArrayDeque<>().
5348
54- //Deque<Integer> deque1 = new LinkedList<>(); ask chatgpt which is better ArrayDeque and LinkedList. insertion, deletion somewhere in middle we use.
49+ // Deque<Integer> deque1 = new LinkedList<>();
5550
5651 // circular, head and tail
57- // no need to shift elements, just shift head and tail
52+ // no need to shift elements, just shift head and tail.
5853 deque1 .addFirst (10 ); // head--
5954 deque1 .addLast (20 );
6055 deque1 .offerFirst (5 );
@@ -68,10 +63,10 @@ public static void main(String[] args) {
6863 deque1 .pollLast (); // Removes 25
6964
7065 // Current Deque: [10, 20]
71- for (int num : deque1 )
72- {
66+ for (int num : deque1 ) {
7367 System .out .println (num );
7468 }
75- Deque <Integer > deque2 = new LinkedList <>(); // insertion, deletion somewhere in middle we use.
69+ Deque <Integer > deque2 = new LinkedList <>();
70+ // insertion, deletion somewhere in a middle we use.
7671 }
77- }
72+ }
0 commit comments