

Get Data Structures and Algorithms in Java, 6th Edition now with the O’Reilly learning platform. Perhaps an even more amusing example is a PEZ® candy dispenser, which stores mint candies in a spring-loaded container that “pops” out the. The deque, which is short for double-ended queue, combines the concept of the queue with the stack, another fundamental data structure in which elements are. When we need a new plate from the dispenser, we “pop” the top plate off the stack, and when we add a plate, we “push” it down on the stack to become the new top plate. In this case, the fundamental operations involve the “pushing” and “popping” of plates on the stack. The name “stack” is derived from the metaphor of a stack of plates in a spring-loaded, cafeteria plate dispenser. A user may insert objects into a stack at any time, but may only access or remove the most recently inserted object that remains (at the so-called “top” of the stack). System.out.6.1.2 A Simple Array-Based Stack ImplementationĦ.1.3 Implementing a Stack with a Singly Linked ListĦ.2.3 Implementing a Queue with a Singly Linked ListĦ.3.3 Deques in the Java Collections FrameworkĪ stack is a collection of objects that are inserted and removed according to the last-in, first-out ( LIFO) principle. Remove the last element of the ArrayDeque. Remove the first element of the ArrayDeque. Print the last element of the ArrayDeque. Print the first element of the ArrayDeque. Print the ArrayDeque elements using iterator. * This class is used to show the ArrayDeque functionality. Syntax: public boolean removeLastOccurrence() A simple example of ArrayDeque class to explain few methods of Deque interface. Returns true if specified element is removed from this deque otherwise returns false. removeLastOccurrence(): Removes the last occurrence of the specified element from this deque. Syntax: public boolean removeFirstOccurrence()ġ4. removeFirstOccurrence(): Removes the first occurrence of the specified element from this deque. peekLast(): Returns but does not removes the last element of this deque. peekFirst(): Returns but does not removes the first element of this deque. It throws an exception if this deque is empty.ġ1. getLast(): Returns but does not removes the last element of this deque. It throws an exception if this deque is empty.ġ0. getFirst(): Returns but does not removes the first element of this deque. pollLast(): Returns and removes the last element of this deque. pollFirst(): Returns and removes the first element of this deque. It throws an exception if this deque is empty.ħ. removeLast(): Returns and removes the last element of this deque. It throws an exception if this deque is empty.Ħ. removeFirst(): Returns and removes the first element of this deque. Syntax: public boolean offerLast(Object obj)ĥ. Returns true if the element was added to this deque, else false. offerLast(Object obj): Inserts the specified element at the end of this deque.

Syntax: public boolean offerFirst(Object obj)Ĥ. offerFirst(Object obj): Inserts the specified element at the front of this deque. addLast(Object obj): Inserts the specified element at the end of this deque.ģ. addFirst(Object obj): Inserts the specified element at the front of this deque.Ģ. Commonly used methods of Deque interface:ġ.

In a deque all new elements can be inserted, retrieved and removed at both ends. Deques can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out).
