1

How can I make this memory drawing using tikz?

enter image description here

Above there is an array and below there is a linked list with linked lists in it.

I need those different colors - it doesn't matter which colors exactly, I just need them different. I need the bullet points as I did them (preferably in the middle of their box).

Many thanks!

6

Do-it-for-me style, without trying to be clever:

\documentclass[tikz,margin=.5cm]{standalone}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{patterns}

\begin{document}
    \begin{tikzpicture}[scale=.5]
        \tikzstyle{bluelines}=[blue]
        \tikzstyle{redlines}=[red]
        \tikzstyle{purplelines}=[purple!50!black]
        \tikzstyle{number}=[black]
        \tikzstyle{nullpointer}=[arrows={Circle[sep={-2pt}]-To[sep=1pt].Bar[scale width=2,sep=2pt]Bar[sep={-3.8pt}]}]
        \tikzstyle{pointer}=[arrows={Circle[sep={-2pt}]-To[sep=1pt]}]

        % linked list at the bottom, including some pointers
        \foreach \x/\y in {0/0,6/0,12/0} {
            \draw[bluelines] (\x,\y) rectangle +(3,4)
                            +(0,2) -- +(2,2)
                            +(2,0) -- +(2,4)
                            +(1,1) node[number] {2}
                            +(1,3) edge[pointer] +(1,6)
                            +(.5,6.5) edge[purplelines,pointer,out=180,in=150] +(0,3.5);
        }

        % second layer of linked lists
        \foreach \x/\y in {0/6,0/10,6/6,12/6} {
            \draw[bluelines] (\x,\y) rectangle +(3,3)
                            +(0,1) -- +(3,1)
                            +(0,2) -- +(3,2)
                            +(1.5,1.5) node[number] {2};
        }

        % array at the top
        \path[pattern=north east lines,pattern color=blue] (2,14) rectangle +(2,2);
        \foreach \i in {0,...,4} {
            \draw[bluelines] (2 + 2 * \i,14) rectangle +(2,2)
                            +(1,2) node[above] {\i};
        }

        % head and pointer
        \draw[bluelines] (-8,-2.5) rectangle +(3,2)
                        +(1.5,1) node {head};
        \draw[bluelines,pointer] (-6,-1) |- (0,1);

        % bottom linked list links
        \draw ( 2.5,2) edge[bluelines,pointer]     ( 6,2)
              ( 8.5,2) edge[bluelines,pointer]     (12,2)
              (14.5,2) edge[bluelines,nullpointer] (17,2);

        % second layer linked list links
        \draw ( 1.5, 8.5) edge[bluelines,pointer]     ( 1.5,10.0)
              ( 0.5,12.5) edge[bluelines,nullpointer] ( 0.5,13.5)
              ( 7.5, 8.5) edge[bluelines,nullpointer] ( 7.5,10.0)
              (13.5, 8.5) edge[bluelines,nullpointer] (13.5,10.0)
              ( 0.5,10.5) edge[purplelines,pointer,out=180,in=150] (0,2);

        % pointers from array
        \draw ( 5,15) edge[redlines,pointer,out=-90,in= 0]                 ( 3.0,12.5)
              ( 7,15) edge[redlines,pointer,out=-90,in= 0]                 ( 3.0, 8.5)
              ( 9,15) edge[redlines,pointer,out= 65,in=90,out looseness=2] (14.5, 9.0)
              (11,15) edge[redlines,pointer]                               ( 9.0, 9.0);
    \end{tikzpicture}
\end{document}

output

The colors are defined at the beginning, so they should be easy to change. The coordinate system is set up so that one unit is exactly one square on your paper, with the origin at the lower left corner of the entry that head is pointing at.

If you change the line width, you probably have to mess around with the sep=x values in the pointer and nullpointer styles (these lenghts don't scale with the line width, but the sizes of the arrow tips do).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.