5

I would like to draw the following open ended lists one after another. On the second list, drawing an arrow from each item to a struct. This could basically viewed as a hash table.

             |     |   />   
             |_____|  /
|     |      | 300 | /
|_____|      |_____| 
| 100 |      | 100 | -----> 
|_____|      |_____|

Example sketch:

![enter image description here


What I was able to come up with:

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,arrows.meta,arrows}

\tikzset{
mymat/.style={
  matrix of math nodes,
  text height=2.5ex,
  text depth=0.75ex,
  text width=3.25ex,
  align=center,
  row sep=-\pgflinewidth
  },
}
\begin{document}

\begin{tikzpicture}[>=latex]
\matrix[mymat,anchor=west,style={nodes=draw}]
at (0,0)
(mat1)
{
...\\
...\\
...\\
...\\
...\\
69\\
98\\
};
\matrix[mymat,right=of mat1,anchor=north,style={nodes={draw}},yshift=1.0cm]
(mat2)
{
7\\
7\\
};
\matrix[mymat,right=of mat1,anchor=north,style={nodes={draw}},yshift=-1.0cm]
(mat5)
{
1\\
2\\
};
\path[->]
  (mat1-6-1.center) edge[] node [left] {} (mat2-1-1.south west);
\path[->]
  (mat1-7-1.center) edge[] node [left] {} (mat5-1-1.south west);

\end{tikzpicture}

\end{document}

enter image description here

8
  • 1
    tex.stackexchange.com/q/228835/134144 seems to be related.
    – leandriis
    Mar 6 at 7:19
  • I have to find a way to draw the lists vertical from the related example
    – alper
    Mar 6 at 11:25
  • 1
    tex.stackexchange.com/a/476490/134144 could serve as a point to start from
    – leandriis
    Mar 6 at 11:47
  • Definitely matrices of nodes. Follow the link provided by @leandriis and come back with what you started with.
    – SebGlav
    Mar 6 at 13:29
  • @SebGlav Thank you I will update the question with improvent using !leandriis's guide
    – alper
    Mar 6 at 13:36
4
+50

You already did almost everything on your own, congratulations. Here's what you can add to complete your picture.

EDIT

I edited my post (and the picture) to fix some issues in the creation of the matrices (now everything is drawn on grid) and to add the open ended that you wanted in the first place.

list and arrows

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,positioning,arrows}

\tikzset{
    mymat/.style={
      matrix of math nodes,
      minimum width=1cm,
      minimum height=1cm,
      text height=2.5ex,
      text depth=0.75ex,
      text width=3.25ex,
      align=center,
      row sep=-\pgflinewidth,
      outer sep=0pt,
      inner sep=0pt
      },
    }
\begin{document}
    
    \begin{tikzpicture}[>=latex,on grid]
        \matrix[mymat,anchor=south west,style={nodes=draw}]
        at (1,0)
        (mat1)
        {
        \dots\\
        \dots\\
        \dots\\
        69\\
        98\\
        };
        \matrix[mymat,right=30mm of mat1-5-1.south,anchor=south,style={nodes={draw}}]
        (mat2)
        {
        7\\
        7\\
        };
        \matrix[mymat,above=30 mm of mat2,style={nodes={draw}}]
        (mat5)
        {
        1\\
        2\\
        };
        
        %%%%%% Modified part
        
        % Draw the open end
        \draw (mat1-1-1.north west) --++ (0,1);
        \draw (mat1-1-1.north east) --++ (0,1);
        
        % Draw the arrows
        \path[->]
          (mat1-5-1.east) edge[] node [left] {} (mat2-2-1.west);
        \path[->]
          (mat1-4-1.east) edge[] node [left] {} (mat5-2-1.west);
        
        % Number the cells
        \foreach \i [count=\l from 0] in {5,...,1} \node[left= 8mm of mat1-\i-1] {\l};
        \foreach \l [count=\i from 1] in {A,B}
            {
            \node[right=8 mm of mat2-\i-1] {\l};
            \node[right=8 mm of mat5-\i-1] {\l};
            }
    \end{tikzpicture}

\end{document}

EDIT 2

Now to the colours and the text widths, you can add some filling into your matrices declaration and you can change locally the text width to adapt it to your content.

list v2

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,positioning,arrows}

\tikzset{
    mymat/.style={
      matrix of math nodes,
      minimum width=1cm,
      minimum height=1cm,
      text height=2.5ex,
      text depth=0.75ex,
      text width=3.25ex,
      align=center,
      row sep=-\pgflinewidth,
      outer sep=0pt,
      inner sep=0pt
      },
    }
\begin{document}
    
    \begin{tikzpicture}[>=latex,on grid]
        \matrix[mymat,fill=olive!50,anchor=south west,style={nodes=draw}]
        at (1,0)
        (mat1)
        {
        \dots\\
        \dots\\
        \dots\\
        69\\
        98\\
        };
        \matrix[mymat,text width=15mm,fill=cyan,right=30mm of mat1-5-1.south,anchor=south,style={nodes={draw}}]
        (mat2)
        {
        7584\\
        7968\\
        };
        \matrix[mymat,fill=pink,above=30 mm of mat2,style={nodes={draw}}]
        (mat5)
        {
        1\\
        2\\
        };
        
        %%%%%% Modified part
        
        % Draw the open end
        \draw (mat1-1-1.north west) --++ (0,1);
        \draw (mat1-1-1.north east) --++ (0,1);
        
        % Draw the arrows
        \path[->]
          (mat1-5-1.east) edge[] node [left] {} (mat2-2-1.west);
        \path[->]
          (mat1-4-1.east) edge[] node [left] {} (mat5-2-1.west);
        
        % Number the cells
        \foreach \i [count=\l from 0] in {5,...,1} \node[left= 8mm of mat1-\i-1] {\l};
        \foreach \l [count=\i from 1] in {A,B}
            {
            \node[right=1 mm of mat2-\i-1.east] {\l};
            \node[right=1 mm of mat5-\i-1.east] {\l};
            }
    \end{tikzpicture}

\end{document}
12
  • @alper I edited my answer to add the open ended feature you asked for, and I also did some minor improvements in the matrices construction.
    – SebGlav
    Apr 7 at 10:02
  • Is it also possible to color background of arrays on the right as well? For example from changing their backgrounds from white to light gray, if possible
    – alper
    Apr 7 at 22:32
  • I am trying to combine your answer with following solution (tex.stackexchange.com/questions/591410/…), where your answer's output will be bottom of the timeline. The other solution is not defined using \begin{tikzpicture}[>=latex,on grid] rather its \begin{tikzpicture}. Could there be anyway to combine both under \begin{tikzpicture} ... \end{tikzpicture}
    – alper
    Apr 8 at 0:22
  • If there is longer text in the arrays on the right side, lets say 700000 instead of 7, can the lists' width could be expanded little bit ? or longer text AAAAAAA rather than A
    – alper
    Apr 8 at 0:36
  • I edited my post to add some colours and text width changes. If you want to insert it into a bigger project, I suggest you post a new question which we will happy to help you with.
    – SebGlav
    Apr 8 at 18:22

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.