✔️ Queue
- FIFO - First in First out
import java.util.LinkedList;
import java.util.Queue;
import java.util.Deque;
public class practiceQueue {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
queue.offer(2);
queue.offer(3);
queue.offer(4);
queue.offer(5);
System.out.println(queue);
queue.poll();
System.out.println(queue);
queue.poll();
System.out.println(queue);
queue.poll();
System.out.println(queue);
System.out.println(queue.peek());
System.out.println("=======================Deque==========================");
Deque<Integer> deque = new LinkedList<>();
deque.offerFirst(1);
System.out.println(deque);
deque.offerLast(2);
System.out.println(deque);
deque.offerFirst(3);
System.out.println(deque);
deque.offerLast(4);
System.out.println(deque);
deque.pollFirst();
System.out.println(deque);
deque.pollLast();
System.out.println(deque);
deque.peek();
System.out.println(deque);
}
}
✔️ Deque(Double-Ended Queue)
import java.util.LinkedList;
import java.util.Deque;
public class practiceDeque {
public static void main(String[] args) {
Deque<Integer> deque = new LinkedList<>();
deque.offerFirst(1);
System.out.println(deque);
deque.offerLast(2);
System.out.println(deque);
deque.offerFirst(3);
System.out.println(deque);
deque.offerLast(4);
System.out.println(deque);
deque.pollFirst();
System.out.println(deque);
deque.pollLast();
System.out.println(deque);
deque.peek();
System.out.println(deque);
}
}