如何绘制一个列表,其项目指向一个结构?

如何绘制一个列表,其项目指向一个结构?

我想一个接一个地绘制以下开放式列表。在第二个列表中,从每个项目绘制一个指向的箭头struct。这基本上可以看作是一个哈希表。

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

示例草图:

![在此处输入图片描述


我能想到的办法是:

\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}

在此处输入图片描述

答案1

恭喜你,你已经独立完成了几乎所有的事情。以下是你可以添加的内容来完成你的图片。

编辑

我编辑了我的帖子(和图片)以修复矩阵创建中的一些问题(现在一切都已绘制on grid)并添加您首先想要的开放式结局。

列表和箭头

\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}

编辑2

现在对于颜色和文本宽度,您可以在矩阵声明中添加一些填充,并且可以本地更改文本宽度以使其适合您的内容。

列表 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}

相关内容