Skip to content

Instantly share code, notes, and snippets.

@xiaohanyu
Last active May 3, 2023 11:33
Show Gist options
  • Save xiaohanyu/2b1c9a546a4a6dd93b9d6a70899652f9 to your computer and use it in GitHub Desktop.
Save xiaohanyu/2b1c9a546a4a6dd93b9d6a70899652f9 to your computer and use it in GitHub Desktop.
Demo of drawing an array stack by LaTeX TikZ in Emacs Org-mode.

This is a demo solution for exercise 10.1-1 of the famous CLRS’s Introduction to Algorithms, 3rd Edition book.

\def\nullvalue{}
\def\drawarraystack(#1, #2, #3){
  \begin{tikzpicture}
    \def\scale{0.5}
    \def\array{#1}
    \def\top{#2}
    \def\length{#3}
    \foreach \i/\j in \array {
      \node[minimum size=\scale cm] at (\i * \scale, \scale) (index\i) {\footnotesize \i};
      \ifthenelse{\equal{\j}{\nullvalue}}{
        \def\fillcolor{lightgray}
      }{
        \def\fillcolor{lightgray!40}
      }
      \node[draw, fill=\fillcolor, minimum size=\scale cm] at (\i * \scale, 0) {\j};
    }
    \fill [fill=lightgray] (\length * \scale + \scale * 0.5, 0 - \scale * 0.5) -- +(\scale, 0) -- +(\scale, \scale) -- +(0, \scale) -- cycle;
    \draw (\length * \scale + \scale * 0.5, 0 - \scale * 0.5) -- +(\scale, 0);
    \draw (\length * \scale + \scale * 0.5, 0 + \scale * 0.5) -- +(\scale, 0);
    \node at (0, 0) (start) {\small $S$};
    \node at (\top * \scale, 0) (top) {};
    \node at (\top* \scale, -1) (pointer) {\small $S.top = \top$};
    \draw [-{Stealth[sep=6pt]}] (pointer) -- (top);
  \end{tikzpicture}
}

\begin{figure}[htbp]
  \begin{subfigure}[b]{.33\linewidth}
    \centering
    \drawarraystack({1/\nullvalue, 2/\nullvalue, 3/\nullvalue, 4/\nullvalue, 5/\nullvalue, 6/\nullvalue}, 0, 6)
    \caption{\texttt{Init(S)}}\label{fig:1a}
  \end{subfigure}%
  \begin{subfigure}[b]{.33\linewidth}
    \centering
    \drawarraystack({1/4, 2/\nullvalue, 3/\nullvalue, 4/\nullvalue, 5/\nullvalue, 6/\nullvalue}, 1, 6)
    \caption{\texttt{PUSH(S, 4)}}\label{fig:1b}
  \end{subfigure}
  \begin{subfigure}[b]{.33\linewidth}
    \centering
    \drawarraystack({1/4, 2/1, 3/\nullvalue, 4/\nullvalue, 5/\nullvalue, 6/\nullvalue}, 2, 6)
    \caption{\texttt{PUSH(S, 1)}}\label{fig:1c}
  \end{subfigure}
  \begin{subfigure}[b]{.33\linewidth}
    \centering
    \drawarraystack({1/4, 2/1, 3/3, 4/\nullvalue, 5/\nullvalue, 6/\nullvalue}, 3, 6)
    \caption{\texttt{PUSH(S, 3)}}\label{fig:1d}
  \end{subfigure}
  \begin{subfigure}[b]{.33\linewidth}
    \centering
    \drawarraystack({1/4, 2/1, 3/\nullvalue, 4/\nullvalue, 5/\nullvalue, 6/\nullvalue}, 2, 6)
    \caption{\texttt{POP(S)}}\label{fig:1e}
  \end{subfigure}
  \begin{subfigure}[b]{.33\linewidth}
    \centering
    \drawarraystack({1/4, 2/1, 3/8, 4/\nullvalue, 5/\nullvalue, 6/\nullvalue}, 3, 6)
    \caption{\texttt{PUSH(S, 8)}}\label{fig:1f}
  \end{subfigure}
  \caption{Stack operation}\label{fig:1}
\end{figure}
@xiaohanyu
Copy link
Author

xiaohanyu commented Mar 5, 2017

Generated pdf:

tt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment