Be the first user to complete this post
|
Add to List |
260. Deque Implementation – Java
Java Deque Interface –
- It’s a linear collection.
- The Deque interface is a subtype of the util.Queue interface.
- Deque is acronym of “Double Ended Queue” means it supports insertion and removal of data from both the ends. So it can be used as Stack OR Queue.
Declaration
public interface Deque extends Queue
Methods:
Return Type | Method | Description |
boolean | add(E e) | Insert element at the tail of this deque |
void | addFirst(E e) | inserts the specified element at the front of this deque. |
void | addLast(E e) | Inserts the specified element at the end of this deque |
boolean | offerFirst(E e) | Inserts the specified element at the front of this deque |
boolean | offerLast(E e) | Inserts the specified element at the end of this deque |
E | removeFirst() | Retrieves and removes the first element of this deque |
E | removeLast() | Retrieves and removes the last element of this deque |
E | pollFirst() | Retrieves and removes the first element of this deque, or returns null if this deque is empty. |
E | pollLast() | Retrieves and removes the last element of this deque, or returns null if this deque is empty. |
E | getFirst() | Retrieves, but does not remove, the first element of this deque. |
E | peekFirst() | Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty. |
E | peekLast() | Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty. |
boolean | removeFirstOccurrence(Object o) | Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. |
boolean | removeLastOccurrence(Object o) | Removes the last occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. |
boolean | offer(E e) | Inserts the specified element at the tail of this deque |
void | push(E e) | Pushes an element onto the stack represented by this deque |
E | pop() | Pops an element from the stack represented by this deque. In other words, removes and returns the first element of this deque. |
Iterator<E> | iterator() | Returns an iterator over the elements in this deque, The elements will be returned in order from first (head) to last (tail). |
Iterator<E> | descendingIterator() | Returns an iterator over the elements in this deque The elements will be returned in order from last (tail) to first (head). |
Output:
ADD Operations... Deque is: [Head-3, Head-2, Head-1, Tail-1, Tail-2, Tail-3, Tail-4] ITERATOR.... Head-3 Head-2 Head-1 Tail-1 Tail-2 Tail-3 Tail-4 REVERSE ITERATOR.... Tail-4 Tail-3 Tail-2 Tail-1 Head-1 Head-2 Head-3 Peek : Head-3 Peek First : Head-3 Peek Last: Tail-4 Stack operations: Push Deque: [Head-4, Head-3, Head-2, Head-1, Tail-1, Tail-2, Tail-3, Tail-4] Stack operations: Pop Deque: [Head-3, Head-2, Head-1, Tail-1, Tail-2, Tail-3, Tail-4] REMOVE Operations... Deque: [Head-1, Tail-1, Tail-2, Tail-3] Deque Contains 'AAAAA': false Deque Contains 'Tail-1': true
Ref: here