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).

Code: :


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

__________________________________________________
Top Companies Interview Questions..-

GoogleMicrosoftAmazonFacebookmore..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: