Subsections

# 2.4 : Fast Deque Operations Using an Array

The from the previous section is a data structure for representing a sequence that allows us to efficiently add to one end of the sequence and remove from the other end. The data structure allows for efficient addition and removal at both ends. This structure implements the interface by using the same circular array technique used to represent an .

```  array<T> a;
int j;
int n;
```

The and operations on an are straightforward. They get or set the array element .

```  T get(int i) {
return a[(j + i) % a.length];
}
T set(int i, T x) {
T y = a[(j + i) % a.length];
a[(j + i) % a.length] = x;
return y;
}
```

The implementation of is a little more interesting. As usual, we first check if is full and, if necessary, call to resize . Remember that we want this operation to be fast when is small (close to 0) or when is large (close to ). Therefore, we check if . If so, we shift the elements left by one position. Otherwise ( ), we shift the elements right by one position. See Figure 2.3 for an illustration of and operations on an . ```  void add(int i, T x) {
if (n + 1 > a.length)  resize();
if (i < n/2) { // shift a,..,a[i-1] left one position
j = (j == 0) ? a.length - 1 : j - 1;
for (int k = 0; k <= i-1; k++)
a[(j+k)%a.length] = a[(j+k+1)%a.length];
} else { // shift a[i],..,a[n-1] right one position
for (int k = n; k > i; k--)
a[(j+k)%a.length] = a[(j+k-1)%a.length];
}
a[(j+i)%a.length] = x;
n++;
}
```

By doing the shifting in this way, we guarantee that never has to shift more than elements. Thus, the running time of the operation (ignoring the cost of a operation) is .

The implementation of the operation is similar. It either shifts elements right by one position or shifts the elements left by one position depending on whether . Again, this means that never spends more than time to shift elements.

```  T remove(int i) {
T x = a[(j+i)%a.length];
if (i < n/2) { // shift a,..,[i-1] right one position
for (int k = i; k > 0; k--)
a[(j+k)%a.length] = a[(j+k-1)%a.length];
j = (j + 1) % a.length;
} else { // shift a[i+1],..,a[n-1] left one position
for (int k = i; k < n-1; k++)
a[(j+k)%a.length] = a[(j+k+1)%a.length];
}
n--;
if (3*n < a.length) resize();
return x;
}
```

## 2.4.1 Summary

The following theorem summarizes the performance of the data structure:

Theorem 2..3   An implements the interface. Ignoring the cost of calls to , an supports the operations
• and in time per operation; and
• and in time per operation.
Furthermore, beginning with an empty , performing any sequence of and operations results in a total of time spent during all calls to .

opendatastructures.org
{"ft_lang_label":"__label__en","ft_lang_prob":0.64897484,"math_prob":0.99868226,"size":2259,"snap":"2022-27-2022-33","text_gpt3_token_len":663,"char_repetition_ratio":0.15920177,"word_repetition_ratio":0.061611373,"special_character_ratio":0.33687472,"punctuation_ratio":0.17537314,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99889106,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104],"im_url_duplicate_count":[null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-25T17:09:48Z\",\"WARC-Record-ID\":\"<urn:uuid:8969675f-48b3-4902-9c14-1425bc10ef5b>\",\"Content-Length\":\"18618\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:63f26bd2-b5bc-46ec-a0ca-347b5ab2f8b0>\",\"WARC-Concurrent-To\":\"<urn:uuid:bd236a52-83e0-4d5a-9eb8-5b75ea653253>\",\"WARC-IP-Address\":\"134.117.27.24\",\"WARC-Target-URI\":\"http://opendatastructures.org/ods-cpp/2_4_Fast_Deque_Operations_U.html\",\"WARC-Payload-Digest\":\"sha1:QHF43DKGM6DGLDUKGMP4CW3FMHDWOFN3\",\"WARC-Block-Digest\":\"sha1:CVVHS226BTYMOZYNNV2Z3Y6FR56M4SLN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103036077.8_warc_CC-MAIN-20220625160220-20220625190220-00284.warc.gz\"}"}