# Problem Solving with Algorithms and Data Structures using Java: The Interactive Edition

## Section3.16The Deque Abstract Data Type

The deque abstract data type is defined by the following structure and operations. A deque is structured, as described above, as an ordered collection of items where items are added and removed from either end, either head or tail. The deque operations are given below.
• Deque() creates a new deque that is empty. It needs no parameters and returns an empty deque.
• addHead(item) adds a new item to the head of the deque. It needs the item and returns nothing.
• addTail(item) adds a new item to the tail of the deque. It needs the item and returns nothing.
• removeHead() removes the head item from the deque. It needs no parameters and returns the item. The deque is modified.
• removeTail() removes the tail item from the deque. It needs no parameters and returns the item. The deque is modified.
• peekHead() returns, but does not remove, the head item from the deque. It needs no parameters and does not modify the deque.
• peekTail() returns, but does not remove, the tail item from the deque. It needs no parameters and does not modify the deque.
• isEmpty() tests to see whether the deque is empty. It needs no parameters and returns a boolean value.
• size() returns the number of items in the deque. It needs no parameters and returns an integer.
As an example, if we assume that d is a deque that has been created and is currently empty, then Table 3.16.1 shows the results of a sequence of deque operations. Note that the contents at the head are listed on the right. It is very important to keep track of the head and the tail as you move items in and out of the collection, as things can get a bit confusing.