3.16. The 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 front or rear. The deque operations are given below.

As an example, if we assume that d is a deque that has been created and is currently empty, then Table 6 shows the results of a sequence of deque operations. Note that the contents in front are listed on the right. It is very important to keep track of the front and the rear as you move items in and out of the collection as things can get a bit confusing.

Table 6: Examples of Deque Operations

Deque Operation

Deque Contents

Return Value

d.is_empty()

[]

True

d.add_rear(4)

[4]

d.add_rear("dog")

['dog', 4]

d.add_front("cat")

['dog', 4, 'cat']

d.add_front(True)

['dog', 4, 'cat', True]

d.size()

['dog', 4, 'cat', True]

4

d.is_empty()

['dog', 4, 'cat', True]

False

d.add_rear(8.4)

[8.4,'dog', 4, 'cat', True]

d.remove_rear()

['dog', 4, 'cat', True]

8.4

d.remove_front()

['dog', 4, 'cat']

True

You have attempted of activities on this page