1

I need to draw a stack data structure to explain it in first concept course as well as queue.enter image description here

\begin{tikzpicture}[draw, minimum width=1cm, minimum height=0.5cm] 
\node[draw] (in) at (-1,2) {};
\node[draw] (out) at (1,-2) {}; 
\matrix (queue)[matrix of nodes, nodes={draw, nodes={draw}}, nodes in empty cells] { \\ \\ \\ \\ }; 
\draw[-latex] (0.25,-1) .. controls (0.25,-1.25) and (1,-1.25) .. (out.north); 
\draw[-latex] (in.south) .. controls (-1, 1.5) and (-0.25,1.5) .. (-0.25,1); 
\end{tikzpicture}
4
  • 2
    What have you tried?
    – user156344
    Commented May 9, 2019 at 15:35
  • found an example using matrix but does not fit to my need
    – mourad
    Commented May 9, 2019 at 15:50
  • 1
    Post it anyway.
    – user156344
    Commented May 9, 2019 at 15:50
  • \begin{tikzpicture}[draw, minimum width=1cm, minimum height=0.5cm] \node[draw] (in) at (-1,2) {}; \node[draw] (out) at (1,-2) {}; \matrix (queue)[matrix of nodes, nodes={draw, nodes={draw}}, nodes in empty cells] { \\ \\ \\ \\ }; \draw[-latex] (0.25,-1) .. controls (0.25,-1.25) and (1,-1.25) .. (out.north); \draw[-latex] (in.south) .. controls (-1, 1.5) and (-0.25,1.5) .. (-0.25,1); \end{tikzpicture}
    – mourad
    Commented May 9, 2019 at 15:51

3 Answers 3

2

To give you a start (anyway I am now in freetime :))

\documentclass[tikz,margin=1]{standalone}
\usetikzlibrary{arrows.meta}
\tikzset{
    queue element/.style={
        draw,very thin,rounded corners,
        fill=yellow!30,
        minimum width=1cm,minimum height=.5cm,
        font=\sffamily\footnotesize
    },
    >={[scale=0.8]Triangle}
}
\begin{document}
\begin{tikzpicture}
\fill[green!20] (5.1,.35) rectangle (-.6,-.35);
\draw[green,thick,>->] (-.6,.35) -- (5.1,.35);
\draw[green,thick,>->] (-.6,-.35) -- (5.1,-.35);
\foreach \i/\name in {1/C,2/B,3/A}
    \node[queue element] (\name) at (1.5*\i,0) {\name};
\draw[<-] ([yshift=.2cm]A.north) -- ++ (0,.5) node[above] {front};
\draw[<-] ([yshift=.2cm]C.north) -- ++ (0,.5) node[above] {rear};
\path (6,0) node[right] {before};
\node[queue element] (D) at (-2,1) {D};
\draw[->,very thick] (D.south) to[out=-90,in=180] (-.7,0);

\scope[yshift=-3cm] % queue after
\fill[green!20] (5.1,.35) rectangle (-.6,-.35);
\draw[green,thick,>->] (-.6,.35) -- (5.1,.35);
\draw[green,thick,>->] (-.6,-.35) -- (5.1,-.35);
\foreach \i/\name in {0/D,1/C,2/B,3/A}
    \node[queue element] (\name) at (1.5*\i,0) {\name};
\draw[<-] ([yshift=.2cm]A.north) -- ++ (0,.5) node[above] {front};
\draw[<-] ([yshift=.2cm]D.north) -- ++ (0,.5) node[above] {rear};
\path (6,0) node[right] {after};
\endscope
\end{tikzpicture}
\end{document}

enter image description here

A little modification and you will have dequeue() instead of enqueue().

\documentclass[tikz,margin=1]{standalone}
\usetikzlibrary{arrows.meta}
\tikzset{
    queue element/.style={
        draw,very thin,rounded corners,
        fill=yellow!30,
        minimum width=1cm,minimum height=.5cm,
        font=\sffamily\footnotesize
    },
    >={[scale=0.8]Triangle}
}
\begin{document}
\begin{tikzpicture}
\fill[green!20] (5.1,.35) rectangle (-.6,-.35);
\draw[green,thick,>->] (-.6,.35) -- (5.1,.35);
\draw[green,thick,>->] (-.6,-.35) -- (5.1,-.35);
\foreach \i/\name in {0/D,1/C,2/B,3/A}
    \node[queue element] (\name) at (1.5*\i,0) {\name};
\draw[<-] ([yshift=.2cm]A.north) -- ++ (0,.5) node[above] {front};
\draw[<-] ([yshift=.2cm]D.north) -- ++ (0,.5) node[above] {rear};
\draw[->,very thick] (5.2,0) to[out=0,in=90] ++ (1,-1);
\path (6,0) node[right] {before};

\scope[yshift=-3cm] % queue after
\fill[green!20] (5.1,.35) rectangle (-.6,-.35);
\draw[green,thick,>->] (-.6,.35) -- (5.1,.35);
\draw[green,thick,>->] (-.6,-.35) -- (5.1,-.35);
\foreach \i/\name in {0/D,1/C,2/B}
    \node[queue element] (\name) at (1.5*\i,0) {\name};
\draw[<-] ([yshift=.2cm]B.north) -- ++ (0,.5) node[above] {front};
\draw[<-] ([yshift=.2cm]D.north) -- ++ (0,.5) node[above] {rear};
\path (6,0) node[right] {after};
\endscope
\end{tikzpicture}
\end{document}

enter image description here

With the same technique, you can also illustrate stack data structure :)

\documentclass[tikz,margin=1]{standalone}
\usetikzlibrary{arrows.meta}
\tikzset{
    queue element/.style={
        draw,very thin,rounded corners,
        fill=yellow!30,
        minimum width=1cm,minimum height=.5cm,
        font=\sffamily\footnotesize
    },
    >={[scale=0.8]Triangle}
}
\begin{document}
\begin{tikzpicture}
\fill[green!20] (5.1,.35) rectangle (-.6,-.35);
\draw[green,thick] (-.6,.35) -- (5.1,.35) |- (-.6,-.35);
\foreach \i/\name in {1/C,2/B,3/A}
    \node[queue element] (\name) at (1.5*\i,0) {\name};
\draw[<-] ([yshift=.2cm]A.north) -- ++ (0,.5) node[above] {rear};
\draw[<-] ([yshift=.2cm]C.north) -- ++ (0,.5) node[above] {front};
\path (6,0) node[right] {before};
\node[queue element] (D) at (-2,1) {D};
\draw[->,very thick] (D.south) to[out=-90,in=180] (-.7,0);

\scope[yshift=-3cm] % stack after
\fill[green!20] (5.1,.35) rectangle (-.6,-.35);
\draw[green,thick] (-.6,.35) -- (5.1,.35) |- (-.6,-.35);
\foreach \i/\name in {0/D,1/C,2/B,3/A}
    \node[queue element] (\name) at (1.5*\i,0) {\name};
\draw[<-] ([yshift=.2cm]A.north) -- ++ (0,.5) node[above] {rear};
\draw[<-] ([yshift=.2cm]D.north) -- ++ (0,.5) node[above] {front};
\path (6,0) node[right] {after};
\endscope
\end{tikzpicture}
\end{document}

enter image description here

\documentclass[tikz,margin=1]{standalone}
\usetikzlibrary{arrows.meta}
\tikzset{
    queue element/.style={
        draw,very thin,rounded corners,
        fill=yellow!30,
        minimum width=1cm,minimum height=.5cm,
        font=\sffamily\footnotesize
    },
    >={[scale=0.8]Triangle}
}
\begin{document}
\begin{tikzpicture}
\fill[green!20] (5.1,.35) rectangle (-.6,-.35);
\draw[green,thick] (-.6,.35) -- (5.1,.35) |- (-.6,-.35);
\foreach \i/\name in {0/D,1/C,2/B,3/A}
    \node[queue element] (\name) at (1.5*\i,0) {\name};
\draw[<-] ([yshift=.2cm]A.north) -- ++ (0,.5) node[above] {rear};
\draw[<-] ([yshift=.2cm]D.north) -- ++ (0,.5) node[above] {front};
\draw[->,very thick] (-.7,0) to[out=180,in=90] ++ (-1,-1);
\path (6,0) node[right] {before};

\scope[yshift=-3cm] % stack after
\fill[green!20] (5.1,.35) rectangle (-.6,-.35);
\draw[green,thick] (-.6,.35) -- (5.1,.35) |- (-.6,-.35);
\foreach \i/\name in {1/C,2/B,3/A}
    \node[queue element] (\name) at (1.5*\i,0) {\name};
\draw[<-] ([yshift=.2cm]A.north) -- ++ (0,.5) node[above] {rear};
\draw[<-] ([yshift=.2cm]C.north) -- ++ (0,.5) node[above] {front};
\path (6,0) node[right] {after};
\endscope
\end{tikzpicture}
\end{document}

enter image description here

4
  • Thank you, and for stack if i may? it will be vertical
    – mourad
    Commented May 9, 2019 at 16:00
  • @mourad Stack version added (the illustration is only the way I like, because I don't know how your stack looks like)
    – user156344
    Commented May 9, 2019 at 16:08
  • is it possible to rotate it 90°?
    – mourad
    Commented May 9, 2019 at 16:10
  • You can modify my code. It seems like you are a new TikZ user, so you may need to look at tex.stackexchange.com/q/15779/156344, after that you will be able to modify my code to even 45 degrees if you want
    – user156344
    Commented May 9, 2019 at 16:11
2

A little variation on JouleV's solution.

I've used a matrix of nodes to declare the queue which can be also used for an horizontal or vertical stack. The matrix node can be used as background area and no external rectangle is needed for it.

Internal nodes are conveniently labelled as front and rear to avoid using particular names.

\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{arrows.meta, matrix, positioning}
\tikzset{
    queue element/.style={
        draw,very thin,rounded corners,
        fill=yellow!30,
        minimum width=1cm,minimum height=.5cm,
        font=\sffamily\footnotesize
    },
    >={[scale=0.8]Triangle},
    queue/.style={matrix of nodes,
        nodes in empty cells,
        nodes={queue element, anchor=center},
        fill=green!20,
        column sep=5mm,
        row sep=2mm,
    },
}
\begin{document}
\begin{tikzpicture}

\matrix[queue] (Q1) {
|(rear)| D & C & B &|(front)| A\\};
\draw[green,thick,>->] (Q1.north west) -- (Q1.north east);
\draw[green,thick,>->] (Q1.south west) -- (Q1.south east);
\draw[<-] ([yshift=.2cm]front.north) -- ++ (0,.5) node[above] {front};
\draw[<-] ([yshift=.2cm]rear.north) -- ++ (0,.5) node[above] {rear};
\draw[->,very thick] (Q1.east) to[out=0,in=90] node[pos=.6, above right] (aux) {before} ++ (1,-1);

\scope[yshift=-2cm] % queue after
\matrix[queue] (Q1) {
|(rear)| D & C &|(front)| B &|[fill=none, draw=none]| \\};
\draw[green,thick,>->] (Q1.north west) -- (Q1.north east);
\draw[green,thick,>->] (Q1.south west) -- (Q1.south east);
\draw[<-] ([yshift=.2cm]front.north) -- ++ (0,.5) node[above] {front};
\draw[<-] ([yshift=.2cm]rear.north) -- ++ (0,.5) node[above] {rear};
\node at (aux|-Q1) {after};
\endscope
\end{tikzpicture}

\begin{tikzpicture}

\matrix[queue] (Q1) {
|[fill=none, draw=none]| \\
|(front)| C\\
B\\
|(rear)| A\\};
\draw[green,thick,-] (Q1.north west) |-(Q1.south)-| (Q1.north east);
\draw[<-] ([xshift=.2cm]front.east) -- ++ (0:.5) node[right] {front};
\draw[<-] ([xshift=.2cm]rear.east) -- ++ (0:.5) node[right] {rear};
\draw[<-,very thick] (Q1.north) to[out=90,in=190] ++ (1,1) node[right, queue element] (D) {D};
\node[below=3mm of Q1.south east] {before};

\scope[xshift=3.5cm] % queue after
\matrix[queue] (Q1) {
|(front)| D \\
 C\\
B\\
|(rear)| A\\};
\draw[green,thick,-] (Q1.north west) |-(Q1.south)-| (Q1.north east);
\draw[<-] ([xshift=.2cm]front.east) -- ++ (0:.5) node[right] {front};
\draw[<-] ([xshift=.2cm]rear.east) -- ++ (0:.5) node[right] {rear};
\node[below=3mm of Q1.south east] {after};
\endscope
\end{tikzpicture}
\end{document}

enter image description here

1
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{backgrounds,fit,chains}
\begin{document}
\begin{tikzpicture}[lazy/.style={rounded corners,fill=gray!30,draw=gray!50,
minimum width=3em,minimum height=4ex},arr/.style={very thick,-stealth},
arr2/.style={very thick,stealth-},font=\sffamily]
 \begin{scope}[start chain=going left,node distance=1em,nodes={lazy,on chain}]
  \node[lazy] (A2){A};
  \node[lazy] (B2){B};
  \node[lazy] (C2){C};
  \node[lazy] (D2){D};
 \end{scope}
 \begin{scope}[on background layer]
 \node[fit=(A2) (D2),top color=gray!20,bottom color=gray!10,inner sep=1em](F2){};
 \end{scope}
 \foreach \X in {north,south}
 {\draw[arr,gray!50]  (F2.\X\space west) -- (F2.\X\space east);}
 \begin{scope}[yshift=3cm,start chain=going left,node distance=1em,nodes={lazy,on chain}]
  \node[lazy] (A1){A};
  \node[lazy] (B1){B};
  \node[lazy] (C1){C};
 \end{scope}
 \begin{scope}[on background layer]
  \node[fit=(A1) (D2.west|-A1),top color=gray!20,bottom color=gray!10,inner sep=1em](F1){};
 \end{scope}
 \foreach \X in {north,south}
 {\draw[arr,gray!50]  (F1.\X\space west) -- (F1.\X\space east);}
 \node[left=2em of F1.north west,lazy] (D1) {D};
 \draw[arr,black] (D1.south) to[out=-60,in=180] (F1);
 \draw[arr2] (A2|-F2.north) -- ++ (0,1em) node[above]{Front};
 \draw[arr2] (D2|-F2.north) -- ++ (0,1em) node[above]{Rear};
 \draw[arr2] (A1|-F1.north) -- ++ (0,1em) node[above]{Front};
 \draw[arr2] (C1|-F1.north) -- ++ (0,1em) node[above]{Rear};
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .