The Deque Interface: Insert
The Deque Interface: Insert
Insert
The addfirst and offerFirst methods insert elements at the beginning of the Deque instance. The
methodsaddLast and offerLast insert elements at the end of the Deque instance. When the capacity
of the Dequeinstance is restricted, the preferred methods
are offerFirst and offerLast because addFirst might fail to throw an exception if it is full.
Remove
The removeFirst and pollFirst methods remove elements from the beginning of
the Deque instance. TheremoveLast and pollLast methods remove elements from the end. The
methods pollFirst and pollLastreturn null if the Deque is empty whereas the
methods removeFirst and removeLast throw an exception if the Deque instance is empty.
Retrieve
The methods getFirst and peekFirst retrieve the first element of the Deque instance. These
methods dont remove the value from the Deque instance. Similarly, the
methods getLast and peekLast retrieve the last element. The
methods getFirst and getLast throw an exception if the deque instance is empty whereas the
methods peekFirst and peekLast return NULL.
The 12 methods for insertion, removal and retieval of Deque elements are summarized in the following
table:
Deque Methods
Type of
Operation
Insert
addFirst(e)
offerFirst(e)
addLast(e)
offerLast(e)
Remove
removeFirst()
pollFirst()
removeLast()
pollLast()
Examine
getFirst()
peekFirst()
getLast()
peekLast()
In addition to these basic methods to insert,remove and examine a Deque instance, the Deque interface
also has