You are on page 1of 26

DEQUEUE

IN
JAVA
DEQUEUE IN JAVA

• A linear collection that supports element insertion and removal at both ends
• The name deque is short for "double ended queue" and is usually pronounced "deck"
• Most Deque implementations place no fixed limits on the number of elements they
may contain, but this interface supports capacity-restricted deques as well as those
with no fixed size limit
NOTE

• This interface defines methods to access the elements at both ends of the deque.
Methods are provided to insert, remove, and examine the element
• Each of these methods exists in two forms: one throws an exception if the
operation fails, the other returns a special value (either null or false, depending on
the operation)
QUEUE AND DEQUEUE

The methods inherited from the Queue interface are precisely equivalent to Deque
methods as indicated in the following table

Queue Method Equivalent Deque Method
Add(e) Addlast(e)

offer(e) Offerlast(e)

Poll() Pollfirst()

Element() Getfirst()
addFirst

void addFirst(E e)

Inserts the specified element at the front of this deque if it is possible to do so


immediately without violating capacity restrictions

Parameters:
e - the element to add
Deque offerFirst() 

boolean offerFirst(E e)

Parameters: This method accepts a mandatory parameter e which is the element


to be inserted in the front of the Dequeue

Returns: This method returns true on successful insertion else it returns false
LOGIC

Deque<Integer> DQ = new LinkedBlockingDeque<Integer>(3);


if (DQ.offerFirst(10))
System.out.println("The Deque is not full and 10 is inserted");
Else
System.out.println("The Deque is full");
if (DQ.offerFirst(15))
System.out.println("The Deque is not full and 15 is inserted");
Else
System.out.println("The Deque is full");
Deque offerLast()

boolean offerLast(E e)

Parameters: This method accepts a mandatory parameter e which is the element to


be inserted in the end of the Dequeue

Returns: This method returns true on successful insertion else it returns false
LOGIC

Deque<Integer> DQ = new if (DQ.offerLast(15))


LinkedList<Integer>(); System.out.println("The Deque is
if (DQ.offerLast(10)) not full and 15 is inserted");
System.out.println("The Deque is else
not full and 10 is inserted"); System.out.println("The
else Deque is full");
System.out.println("The Deque // before removing print
is full"); Deque
System.out.println("Deque:
" + DQ)
Deque getFirst()

E getFirst()

Parameters: This method does not accepts any parameter.

Returns: The method returns the first element or the head of the Deque but does not
delete it
LOGIC

Deque<Integer> DQ = new LinkedList<Integer>();


// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// print Deque
System.out.println("Deque: " + DQ);
// print head
System.out.println("Deque's head: " + DQ.getFirst());
ARRAY DEQUEUE

● Resizable-array implementation of the Deque interface

● Array deques have no capacity restrictions; they grow as necessary to support


usage

● Null elements are prohibited

● This class is likely to be faster than Stack when used as a stack, and faster than
LinkedList when used as a queue
CONSTRUCTORS OF ARRAY DEQUEUE

ArrayDeque() Constructs an empty array deque with an


initial capacity sufficient to hold 16
elements
ArrayDeque(int numElements) Constructs an empty array deque with an
initial capacity sufficient to hold the
specified number of elements
METHODS IN ARRAY DEQUEUE
addfirst

public void addFirst(E e)

inserts the specified element at the front of this deque

Parameters: e - the element to add


Throws: NullPointerException - if the specified element is null
LOGIC

Deque<Integer> de_que = new ArrayDeque<Integer>();


          // Use add() method to add elements into the Deque
        de_que.add(10);
        de_que.add(15);
        de_que.add(30);
        de_que.add(20);
        de_que.add(5);
          // Displaying the ArrayDeque
        System.out.println("ArrayDeque: " + de_que);
          // Adding elements at front
        de_que.addFirst(40);
        de_que.addFirst(50);
        de_que.addFirst(60);
        de_que.addFirst(70);
          // Displaying the ArrayDeque
        System.out.println("ArrayDeque_front_addition: " + de_que);
offerFirst

public boolean offerFirst(E e)

Inserts the specified element at the front of this deque

Parameters: e - the element to add


Returns: true

Throws: NullPointerException - if the specified element is null


LOGIC

Deque<String> de_que = new ArrayDeque<String>();


// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("Grees");
de_que.add("4");
de_que.add("Greys");
// Displaying the ArrayDeque
System.out.println("Initial Deque: " + de_que);
// Using offerFirst() to add elements
de_que.offerFirst("World");
de_que.offerFirst("Hello");
// Displaying the ArrayDeque
System.out.println("Final Deque: " + de_que);
offerLast

public boolean offerLast(E e)

Parameters: e - the element to add

Returns: true
Throws: NullPointerException - if the specified element is null
LOGIC:

// Creating an empty ArrayDeque


Deque<String> de_que = new ArrayDeque<String>();
// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("MARIO");
de_que.add("4");
de_que.add("ROAD");
// Displaying the ArrayDeque
System.out.println("Initial Deque: " + de_que);
// Using offerLast() to add elements
de_que.offerLast("Hello");
de_que.offerLast("World");
// Displaying the ArrayDeque
System.out.println("Final Deque: " + de_que);
}
push

public void push(E e)

• Pushes an element onto the stack represented by this deque


• In other words, inserts the element at the front of this deque.

Parameters: e - the element to push


Throws: NullPointerException - if the specified element is null
pop

public E pop()

• Pops an element from the stack represented by this deque


• In other words, removes and returns the first element of this deque
• This method is equivalent to removeFirst()

Returns: the element at the front of this deque


(which is the top of the stack represented by this deque)
Throws: NoSuchElementException - if this deque is empty
LOGIC

Deque<String> de_que = new ArrayDeque<String>();


// Use add() method to add elements
de_que.add("1");
de_que.add("2");
de_que.add("3");
de_que.add("4");
//de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("Initial ArrayDeque: " + de_que);
// Removing elements using pop() method
System.out.println("Popped element: " + de_que.pop());
System.out.println("Popped element: " + de_que.pop());
// Displaying the ArrayDeque after pop
System.out.println("Deque after operation " + de_que);
THANK YOU

You might also like