如何绘制带有连接的数组和链表?

如何绘制带有连接的数组和链表?

我想绘制下图。我能够绘制带有一些箭头的数组,但无法绘制精确的图像。

在此处输入图片描述

看看我的代码:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pdftexcmds}
\usepackage{tikz} 
\usepackage{booktabs}

\usetikzlibrary{positioning,matrix, arrows.meta}
\usetikzlibrary{decorations.pathreplacing}

\date{February 2018}

\begin{document}

 \begin{tikzpicture}
\matrix (A) [ampersand replacement=\&,matrix of nodes, nodes={draw, minimum size=8mm},
    column sep=-\pgflinewidth]{
    3 \& 4 \& 4 \& 1 \& 4 \& 1 \& 2 \& 3\\};
\foreach \i [evaluate=\i as \ni using {int(\i)},
                evaluate=\i as \ntext using {int(\i-1)}] in {1,3,4,6} 
    \draw [{Stealth}-, red!70] (A-1-\ni.south west)--++(-90:5mm) node[below] {\ntext};
\end{tikzpicture}  

\end{document}

编译后的输出:

在此处输入图片描述

问题:如何用 tikz 绘制图像?

答案1

我明白,你喜欢用你的代码获取所显示的图像:

在此处输入图片描述

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                matrix,
                positioning,
                }
\usepackage{amsmath}

\begin{document}
    \begin{tikzpicture}[
    node distance = 21mm and 7mm,
    box/.style = {draw, minimum size=6mm, inner sep=0pt, outer sep=0pt, anchor=center},
      pin edge = {Straight Barb-, shorten <=1mm,semithick}
                        ]
\matrix (m) [matrix of nodes,
             nodes={box},
             column sep=-\pgflinewidth,
             inner sep=0pt,
             pin=120:Array]
{
  1 & 2 & 3 &  \quad$\dotsm$\quad\vphantom{4}  & k     \\
};
\node (h2) [box,below=of m-1-2] {2};
\node (h1) [box, left=of h2,
            pin=240:head]       {1};
\node (h3) [box,right=of h2]    {3};
\node (hk) [box,below right=of m-1-5] {$k$};
%
\draw[dashed,-Straight Barb, shorten <=1mm, shorten >=1mm]
    (m-1-1.south) edge [bend right]   (h1.north)
    (h1.north)    edge [bend right]   (m-1-1.south)
    (m-1-2.south) edge [bend right]   (h2.north)
    (h2.north)    edge [bend right]   (m-1-2.south)
    (m-1-3.south) edge [bend right]   (h3.north)
    (h3.north)    edge [bend right]   (m-1-3.south)
    (m-1-5.south) edge [bend right]   (hk.north)
    (hk.north)     to  [bend right]   (m-1-5.south);
%
\draw[-Straight Barb, transform canvas={yshift= 1mm}]
    (h1) edge   (h2)    (h2) edge   (h3);
\draw[Straight Barb-, transform canvas={yshift=-1mm}]
    (h1) edge   (h2)    (h2) edge   (h3);
%
\draw[loosely dotted, ultra thick, shorten <=3mm, shorten >=3mm]
    (h3) -- (hk);
    \end{tikzpicture}
\end{document}

相关内容