使用 Tikz 绘制哈希表

使用 Tikz 绘制哈希表

我需要尝试复制类似的结构

在此处输入图片描述

但我对 tikz 还是个新手。我不确定是否要使用链、显式节点定位等。而且,根据我绘制图形的经验,我永远无法让箭头正确弯曲。

这个问题超出了我的 TeX 能力......

答案1

您可以使用以下模板来完成并进行最后的调整。我放了两个箭头来显示如何调整定位。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes,
nodes in empty cells,
nodes={font={\ttfamily}},
column sep=0,row sep=0,
column 1/.style={},
column 2/.style={nodes={text width=1.5cm,align=center}},
column 3/.style={nodes={minimum width=5mm}}
]{
1 &JEFF &\\
2 &AUDREY&\\
3 & & \\
4 &DONNA &\\
5 & & \\[1pt]
(9)  &DAVE &\\
(10)&MARK &\\
(11)&AL &\\
};

\draw (m-1-2.north west) rectangle (m-5-2.south east);
\draw (m-1-2.north east) rectangle (m-5-3.south east);
\draw (m-6-2.north west) rectangle (m-8-2.south east);
\draw (m-6-2.north east) rectangle (m-8-3.south east);

\foreach \x in {1,...,4,6,7} {
\draw (m-\x-2.south west) -- (m-\x-3.south east);
};

\draw[->,rounded corners] (m-2-3.mid) -| ++(1cm,-1cm) |- (m-7-3.east);
\draw[->,rounded corners] (m-3-3.mid) -| ++(1cm,-1cm) |- ([xshift=3mm]m-8-3.east);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

用户界面:

% user defined data
\def\list
{
    Jeff,
    Audrey,
    ,
    Donna,
    A.L.,
    ,
    Tootie,
    ,
    Dave,
    Mark,
    Al
}% list of elements


\const{_X1}{1}% column 1 width
\const{_X2}{2}% column 2 width
\const{_X3}{1}% column 3 width
\const{_X4}{1}% space for arrow arms
\const{_Y}{0.5}% row height

在此处输入图片描述

\documentclass[pstricks,border=3pt]{standalone}
\usepackage{pstricks-add,fp}
\psset{dimen=middle}

\makeatletter
\newcommand\const[3][\FPeval]{% #1=method, #2=name, #3=data
   \expandafter#1\csname#2\endcsname{#3}%
   \begingroup\edef\x{\endgroup
     \noexpand\pstVerb{/#2 \csname#2\endcsname\space def}}\x
}

\newcount\const@count
\def\FPnset#1#2{%
  \const@count=\z@
  \@for\next:=#2\do{\advance\const@count\@ne}%
  \edef#1{\number\const@count}%
}
\makeatother

% user defined data
\def\list
{
    Jeff,
    Audrey,
    ,
    Donna,
    A.L.,
    ,
    Tootie,
    ,
    Dave,
    Mark,
    Al
}% list of elements

\const{_X1}{0.6}% column 1 width
\const{_X2}{2}% column 2 width
\const{_X3}{1}% column 3 width
\const{_X4}{1.03}% space for arrow arms
\const{_Y}{0.5}% row height


% internal used constants
\const[\FPnset]{_N}{\list}% number of nodes
\const{_x1}{_X1*0.5}
\const{_x2}{_X1+_X2*0.5}
\const{_x3}{_X1+_X2+_X3*0.5}
\const{_OX1}{_X1}
\const{_OX2}{_X1+_X2}
\const{_OX3}{_X1+_X2+_X3}
\const{_y}{_Y/2}
\const{_OY}{_Y}

\const{CanvasHeight}{_OY*_N}
\const{CanvasWidth}{_OX3+_X4}


\def\node#1#2{%
    \const{counter}{round((#2+1):0)}%   
    \rput(!0 _OY neg counter mul){%
        \psframe(!_OX1 0)(!_OX3 _OY)%
        \rput(!_x1 _y){\counter}
        \rput(!_x2 _y){#1}%
        \psline(!_OX2 0)(!_OX2 _OY)%
        \pnode(!_x3 _y){node\counter}%   
    }}




\begin{document}

\begin{pspicture}(\CanvasWidth,-\CanvasHeight)
    \psforeach{\i}{\list}{\node{\i}{\the\psLoopIndex}}
    \psset
    {
        arrowscale=2,
        dotsize=1pt,
        arrows=*->,
        linearc=\pslinewidth,
        linecolor=blue,
    }
    \ncdiag[arm=1.5]{node2}{node11}
    \ncbar[arm=1]{node5}{node10}
    \ncdiag[arm=0.75,angle=180]{node11}{node9}
\end{pspicture}

\end{document}

相关内容