Subsections


2.3 ArrayQueue: An Array-Based Queue

In this section, we present the ArrayQueue data structure, which implements a FIFO (first-in-first-out) queue; elements are removed (using the $ \ensuremath{\mathrm{remove}()}$ operation) from the queue in the same order they are added (using the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ operation).

Notice that an ArrayStack is a poor choice for an implementation of a FIFO queue. It is not a good choice because we must choose one end of the list upon which to add elements and then remove elements from the other end. One of the two operations must work on the head of the list, which involves calling $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ or $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}})}$ with a value of $ \ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}=0$. This gives a running time proportional to $ \ensuremath{\ensuremath{\mathit{n}}}$.

To obtain an efficient array-based implementation of a queue, we first notice that the problem would be easy if we had an infinite array $ \ensuremath{\ensuremath{\mathit{a}}}$. We could maintain one index $ \ensuremath{\ensuremath{\mathit{j}}}$ that keeps track of the next element to remove and an integer $ \ensuremath{\ensuremath{\mathit{n}}}$ that counts the number of elements in the queue. The queue elements would always be stored in

$\displaystyle \ensuremath{\ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\math...
...th{\mathit{a}}[\ensuremath{\mathit{j}}+\ensuremath{\mathit{n}}-1]}} \enspace . $

Initially, both $ \ensuremath{\ensuremath{\mathit{j}}}$ and $ \ensuremath{\ensuremath{\mathit{n}}}$ would be set to 0. To add an element, we would place it in $ \ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\mathit{j}}+\ensuremath{\mathit{n}}]}$ and increment $ \ensuremath{\ensuremath{\mathit{n}}}$. To remove an element, we would remove it from $ \ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\mathit{j}}]}$, increment $ \ensuremath{\ensuremath{\mathit{j}}}$, and decrement $ \ensuremath{\ensuremath{\mathit{n}}}$.

Of course, the problem with this solution is that it requires an infinite array. An ArrayQueue simulates this by using a finite array $ \ensuremath{\ensuremath{\mathit{a}}}$ and modular arithmetic. This is the kind of arithmetic used when we are talking about the time of day. For example 10:00 plus five hours gives 3:00. Formally, we say that

$\displaystyle 10 + 5 = 15 \equiv 3 \pmod{12} \enspace .
$

We read the latter part of this equation as ``15 is congruent to 3 modulo 12.'' We can also treat $ \bmod$ as a binary operator, so that

$\displaystyle 15 \bmod 12 = 3 \enspace .
$

More generally, for an integer $ a$ and positive integer $ m$, $ a \bmod m$ is the unique integer $ r\in\{0,\ldots,m-1\}$ such that $ a = r + km$ for some integer $ k$. Less formally, the value $ r$ is the remainder we get when we divide $ a$ by $ m$. In many programming languages, including C, C++, and Java, the mod operate is represented using the % symbol.

Modular arithmetic is useful for simulating an infinite array, since $ \ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}\bmod \ensuremath{\ensuremath{\mathrm{length}(\ensuremath{\mathit{a}})}}$ always gives a value in the range $ 0,\ldots,\ensuremath{\ensuremath{\mathrm{length}(\ensuremath{\mathit{a}})-1}}$. Using modular arithmetic we can store the queue elements at array locations

$\displaystyle \ensuremath{\ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\math...
...math{\mathit{n}}-1)\bmod \mathrm{length}(\ensuremath{\mathit{a}})]}}
\enspace. $

This treats the array $ \ensuremath{\ensuremath{\mathit{a}}}$ like a circular array in which array indices larger than $ \ensuremath{\ensuremath{\mathrm{length}(\ensuremath{\mathit{a}})}}-1$ ``wrap around'' to the beginning of the array.

The only remaining thing to worry about is taking care that the number of elements in the ArrayQueue does not exceed the size of $ \ensuremath{\ensuremath{\mathit{a}}}$.


\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{initialize}(...
...th{\ensuremath{\mathit{n}} \gets \ensuremath{0}}\\
\end{flushleft}\end{leftbar}

A sequence of $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}()}$ operations on an ArrayQueue is illustrated in Figure 2.2. To implement $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$, we first check if $ \ensuremath{\ensuremath{\mathit{a}}}$ is full and, if necessary, call $ \ensuremath{\mathrm{resize}()}$ to increase the size of $ \ensuremath{\ensuremath{\mathit{a}}}$. Next, we store $ \ensuremath{\ensuremath{\mathit{x}}}$ in $ \ensuremath{\ensuremath{\mathit{a}}[(\ensuremath{\mathit{j}}+\ensuremath{\mathit{n}})\bmod \mathrm{length}(\ensuremath{\mathit{a}})]}$ and increment $ \ensuremath{\ensuremath{\mathit{n}}}$.

Figure 2.2: A sequence of $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}})}$ operations on an ArrayQueue. Arrows denote elements being copied. Operations that result in a call to $ \ensuremath{\mathrm{resize}()}$ are marked with an asterisk.
\includegraphics[scale=0.90909]{figs-python/arrayqueue}


\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{add}(\ensure...
...return}} \ensuremath{\ensuremath{\mathit{true}}}\\
\end{flushleft}\end{leftbar}

To implement $ \ensuremath{\mathrm{remove}()}$, we first store $ \ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\mathit{j}}]}$ so that we can return it later. Next, we decrement $ \ensuremath{\ensuremath{\mathit{n}}}$ and increment $ \ensuremath{\ensuremath{\mathit{j}}}$ (modulo $ \ensuremath{\mathrm{length}(\ensuremath{\mathit{a}})}$) by setting $ \ensuremath{\ensuremath{\ensuremath{\mathit{j}}}}=(\ensuremath{\ensuremath{\en...
...}}}}+1)\bmod \ensuremath{\ensuremath{\mathrm{length}(\ensuremath{\mathit{a}})}}$. Finally, we return the stored value of $ \ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\mathit{j}}]}$. If necessary, we may call $ \ensuremath{\mathrm{resize}()}$ to decrease the size of $ \ensuremath{\ensuremath{\mathit{a}}}$.


\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{remove}()}\\...
...bf{return}} \ensuremath{\ensuremath{\mathit{x}}}\\
\end{flushleft}\end{leftbar}

Finally, the $ \ensuremath{\mathrm{resize}()}$ operation is very similar to the $ \ensuremath{\mathrm{resize}()}$ operation of ArrayStack. It allocates a new array, $ \ensuremath{\ensuremath{\mathit{b}}}$, of size $ 2\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}$ and copies

$\displaystyle \ensuremath{\ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\math...
...}}+\ensuremath{\mathit{n}}-1)\bmod \mathrm{length}(\ensuremath{\mathit{a}})]}}
$

onto

$\displaystyle \ensuremath{\ensuremath{\ensuremath{\mathit{b}}[0]}},\ensuremath{...
...s,\ensuremath{\ensuremath{\ensuremath{\mathit{b}}[\ensuremath{\mathit{n}}-1]}}
$

and sets $ \ensuremath{\ensuremath{\ensuremath{\mathit{j}}}}=0$.


\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{resize}()}\\...
...th{\ensuremath{\mathit{j}} \gets \ensuremath{0}}\\
\end{flushleft}\end{leftbar}

2.3.1 Summary

The following theorem summarizes the performance of the ArrayQueue data structure:

Theorem 2..2   An ArrayQueue implements the (FIFO) Queue interface. Ignoring the cost of calls to $ \ensuremath{\mathrm{resize}()}$, an ArrayQueue supports the operations $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}()}$ in $ O(1)$ time per operation. Furthermore, beginning with an empty ArrayQueue, any sequence of $ m$ $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}})}$ operations results in a total of $ O(m)$ time spent during all calls to $ \ensuremath{\mathrm{resize}()}$.

opendatastructures.org