# 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.

• Deque() creates a new deque that is empty. It needs no parameters and returns an empty deque.

• addFront(item) adds a new item to the front of the deque. It needs the item and returns nothing.

• addRear(item) adds a new item to the rear of the deque. It needs the item and returns nothing.

• removeFront() removes the front item from the deque. It needs no parameters and returns the item. The deque is modified.

• removeRear() removes the rear item from the deque. It needs no parameters and returns the item. The deque is modified.

• 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 {dequeoperations} 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 1: Examples of Deque Operations

Deque Operation

Deque Contents

Return Value

d.isEmpty()

[]

True

d.addRear(4)

[4]

d.addRear('dog')

['dog',4,]

d.addFront('cat')

['dog',4,'cat']

d.addFront(True)

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

d.size()

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

4

d.isEmpty()

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

False

d.addRear(8.4)

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

d.removeRear()

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

8.4

d.removeFront()

['dog',4,'cat']

True
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.3700234591960907, "perplexity": 1267.9740922261556}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-30/segments/1563195525312.3/warc/CC-MAIN-20190717141631-20190717163631-00078.warc.gz"}