Subsections


3.1 SLList: A Singly-Linked List

An SLList (singly-linked list) is a sequence of Nodes. Each node $ \ensuremath{\ensuremath{\mathit{u}}}$ stores a data value $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{x}}}$ and a reference $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{next}}}$ to the next node in the sequence. For the last node $ \ensuremath{\ensuremath{\mathit{w}}}$ in the sequence, $ \ensuremath{\ensuremath{\ensuremath{\mathit{w}}.\ensuremath{\mathit{next}}}} = \ensuremath{\ensuremath{\ensuremath{\mathit{nil}}}}$

For efficiency, an SLList uses variables $ \ensuremath{\ensuremath{\mathit{head}}}$ and $ \ensuremath{\ensuremath{\mathit{tail}}}$ to keep track of the first and last node in the sequence, as well as an integer $ \ensuremath{\ensuremath{\mathit{n}}}$ to keep track of the length of the sequence:
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{initialize}(...
...nsuremath{\mathit{tail}} \gets \ensuremath{nil}}\\
\end{flushleft}\end{leftbar}
A sequence of Stack and Queue operations on an SLList is illustrated in Figure 3.1.

Figure 3.1: A sequence of Queue ( $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}()}$) and Stack ( $ \ensuremath{\mathrm{push}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{pop}()}$) operations on an SLList.
\includegraphics[width=\textwidth ]{figs-python/sllist}

An SLList can efficiently implement the Stack operations $ \ensuremath{\mathrm{push}()}$ and $ \ensuremath{\mathrm{pop}()}$ by adding and removing elements at the head of the sequence. The $ \ensuremath{\mathrm{push}()}$ operation simply creates a new node $ \ensuremath{\ensuremath{\mathit{u}}}$ with data value $ \ensuremath{\ensuremath{\mathit{x}}}$, sets $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{next}}}$ to the old head of the list and makes $ \ensuremath{\ensuremath{\mathit{u}}}$ the new head of the list. Finally, it increments $ \ensuremath{\ensuremath{\mathit{n}}}$ since the size of the SLList has increased by one:


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

The $ \ensuremath{\mathrm{pop}()}$ operation, after checking that the SLList is not empty, removes the head by setting $ \ensuremath{\ensuremath{\ensuremath{\mathit{head}}\gets \ensuremath{\ensuremath{\mathit{head}}.next}}}$ and decrementing $ \ensuremath{\ensuremath{\mathit{n}}}$. A special case occurs when the last element is being removed, in which case $ \ensuremath{\ensuremath{\mathit{tail}}}$ is set to $ \ensuremath{\ensuremath{\mathit{nil}}}$:


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

Clearly, both the $ \ensuremath{\mathrm{push}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{pop}()}$ operations run in $ O(1)$ time.

3.1.1 Queue Operations

An SLList can also implement the FIFO queue operations $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}()}$ in constant time. Removals are done from the head of the list, and are identical to the $ \ensuremath{\mathrm{pop}()}$ operation:


\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{remove}()}\\...
...ck} \textbf{return}} \ensuremath{\mathrm{pop}()}\\
\end{flushleft}\end{leftbar}

Additions, on the other hand, are done at the tail of the list. In most cases, this is done by setting $ \ensuremath{\ensuremath{\ensuremath{\mathit{tail}}.\ensuremath{\mathit{next}}}}=\ensuremath{\ensuremath{\ensuremath{\mathit{u}}}}$, where $ \ensuremath{\ensuremath{\mathit{u}}}$ is the newly created node that contains $ \ensuremath{\ensuremath{\mathit{x}}}$. However, a special case occurs when $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}=0$, in which case $ \ensuremath{\ensuremath{\ensuremath{\mathit{tail}}}}=\ensuremath{\ensuremath{\ensuremath{\mathit{head}}}}=\ensuremath{\ensuremath{\ensuremath{\mathit{nil}}}}$. In this case, both $ \ensuremath{\ensuremath{\mathit{tail}}}$ and $ \ensuremath{\ensuremath{\mathit{head}}}$ are set to $ \ensuremath{\ensuremath{\mathit{u}}}$.


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

Clearly, both $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}()}$ take constant time.

3.1.2 Summary

The following theorem summarizes the performance of an SLList:

Theorem 3..1   An SLList implements the Stack and (FIFO) Queue interfaces. The $ \ensuremath{\mathrm{push}(\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{pop}()}$, $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}()}$ operations run in $ O(1)$ time per operation.

An SLList nearly implements the full set of Deque operations. The only missing operation is removing from the tail of an SLList. Removing from the tail of an SLList is difficult because it requires updating the value of $ \ensuremath{\ensuremath{\mathit{tail}}}$ so that it points to the node $ \ensuremath{\ensuremath{\mathit{w}}}$ that precedes $ \ensuremath{\ensuremath{\mathit{tail}}}$ in the SLList; this is the node $ \ensuremath{\ensuremath{\mathit{w}}}$ such that $ \ensuremath{\ensuremath{\ensuremath{\mathit{w}}.\ensuremath{\mathit{next}}}}=\ensuremath{\ensuremath{\ensuremath{\mathit{tail}}}}$. Unfortunately, the only way to get to $ \ensuremath{\ensuremath{\mathit{w}}}$ is by traversing the SLList starting at $ \ensuremath{\ensuremath{\mathit{head}}}$ and taking $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-2$ steps.

opendatastructures.org