Be the first user to complete this post

  • 0
Add to List
Beginner

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 TypeMethodDescription
booleanadd(E e) Insert element at the tail of this deque
voidaddFirst(E e)inserts the specified element at the front of this deque.
voidaddLast(E e)Inserts the specified element at the end of this deque
booleanofferFirst(E e)Inserts the specified element at the front of this deque
booleanofferLast(E e)Inserts the specified element at the end of this deque
EremoveFirst()Retrieves and removes the first element of this deque
EremoveLast()Retrieves and removes the last element of this deque
EpollFirst()Retrieves and removes the first element of this deque, or returns null if this deque is empty.
EpollLast()Retrieves and removes the last element of this deque, or returns null if this deque is empty.
EgetFirst()Retrieves, but does not remove, the first element of this deque.
EpeekFirst()Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
EpeekLast()Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
booleanremoveFirstOccurrence(Object o)Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged.
booleanremoveLastOccurrence(Object o)Removes the last occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged.
booleanoffer(E e)Inserts the specified element at the tail of this deque
voidpush(E e)Pushes an element onto the stack represented by this deque
Epop()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