如何用链接列表制作这个数组?

如何用链接列表制作这个数组?

我想用 tikz 或不使用 tikz 绘制图表(下面给出了图表的图像)。我能够在一定程度上使用 texstack exchange 以前的问题,但无法对其进行修改以使其适合我。

参见代码和图片:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\usetikzlibrary{positioning}
\tikzset{
node of list/.style = { 
             draw, 
             fill=orange!20, 
             minimum height=6mm, 
             minimum width=6mm,
             node distance=6mm
   },
link/.style = {
     -stealth,
     shorten >=1pt
     },
array element/.style = {
    draw, fill=white,
    minimum width = 6mm,
    minimum height = 10mm
  }
}

\def\LinkedList#1{%
  \foreach \element in \list {
     \node[node of list, right = of aux, name=ele] {\element};
     \draw[link] (aux) -- (ele);
     \coordinate (aux) at (ele.east);
  } 
}

\begin{tikzpicture}
\foreach \index/\list in {1/{a,b,null}, 2/{c,null}, 3/{d,null}} {
   \node[array element] (aux) at (0,-\index) {\index};
   \LinkedList{\list}
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

问题:我添加了以下几件事,第一件事是增加数组大小,第二件事是使链接列表具有 3 个单元格大小的节点,最后一件事是在一个链接列表上用一些文本画一个圆圈

答案1

如上所述,使用库的可选实现matrix,它具有用于矩阵中每个节点的样式和坐标的许多控制命令;此外positioning,,fitarrows.meta允许稍微夸张的实现......

结果: 在此处输入图片描述

梅威瑟:

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

\begin{document}
    \begin{tikzpicture}[
        %Environment config
        >={Stealth[inset=0pt,length=6pt]},
        blue,
        thick,
        %Environment Styles
        MyMatrix/.style={
            matrix of nodes,
            font=\scriptsize,
            line width=0.75pt,
            column sep=-0.5pt,
            row sep=-0.5pt,
            text height=9pt,
            text width =12pt,
            text depth =3pt,
            align=center,
            nodes={draw=blue},
            nodes in empty cells
        }
    ]
    % Start Drawing the thing
    \matrix[
        MyMatrix,
        label=270:\sf Array,
        column 1/.style={nodes={draw=none}}
    ] at (0,0) (M1){%Matrix contents
    1   &|[fill=blue!20]|   \\
    2   &   \\
        &   \\
        &|[fill=blue!40]|\\
        &   \\
        &   \\
    n   &|[fill=blue!60]|   \\
    };

    \matrix[
        MyMatrix,
        right=2cm of M1-1-2.east,
        row 1/.style={nodes={fill=blue!10}}
    ](LL1){%Matrix contents
        e   &f  &   &[1cm]g &h  &   &[1cm]  &&\\
    };

    \matrix[
        MyMatrix,
        right=1.5cm of M1-4-2.east,
        row 1/.style={nodes={fill=blue!30}}
    ](LL2){
        a   &b  &   &[1cm]c &d  &   &[1cm]e&f&\\
    };

    \matrix[
        MyMatrix,
        right=1.5cm of M1-7-2.east,
        row 1/.style={nodes={fill=blue!40}},
        row 1 column 3/.style={text width =20pt,}
    ](LL3){
        i   &j  &   &[1cm]      &   &   &[1cm]  &&\\
    };

    %Draw details:
    \draw[loosely dotted] (M1-3-1.north)--(M1-6-1.south);
    \foreach \x [count=\i] in {1,4,7}{
    \draw[->] (M1-\x-2.center)--(LL\i-1-1.west);
    \draw[->] (LL\i-1-3.east)--(LL\i-1-4.west);
    \draw[->] (LL\i-1-6.east)--(LL\i-1-7.west);
    }
    \node[draw,blue,ellipse,inner xsep=-20pt,fit=(LL2)](SELECT1){};
    \draw[<-,blue](SELECT1.5) -- ++(1,0.5) node[align=center,anchor=west,font=\sf]{Size \\ almost};

    \end{tikzpicture}
\end{document}

答案2

我会以稍微不同的方式编写宏。您的圆可以绘制为椭圆形。请注意,还有更多可能性。

  • 您可以将其shapes.multiparts用于画面。
  • 您可以使用chains 作为画面。
  • 您可以使用matrix来放置画面。如果画面宽度不同,这尤其有意义。

然而,这个答案的目的只是稍微修改您已有的内容。

\documentclass{article}
\usepackage{tikz}
\newcounter{tableau}
\begin{document}
\thispagestyle{empty}
\usetikzlibrary{positioning,fit,shapes.geometric}
\tikzset{
node of list/.style = { 
             draw, 
             fill=orange!20, 
             minimum height=6mm, 
             minimum width=6mm,
             %node distance=6mm
   },
link/.style = {
     -stealth,
     shorten >=1pt
     },
array element/.style = {
    draw, fill=white,
    minimum width = 6mm,
    minimum height = 10mm
  }
}


\newcommand{\LinkedList}[2][]{% 
    \stepcounter{tableau}
  \foreach \element [count=\Z,evaluate=\Z as \LastZ using {int(\Z-1)}] in #2 {
    \ifnum\Z=1
     \node[#1,name=tableau-\thetableau-1,node of list] {$\element$};
     \xdef\FitList{(tableau-\thetableau-1)}
    \else
     \node[name=tableau-\thetableau-\Z,right=-\pgflinewidth of
     tableau-\thetableau-\LastZ,node of list] {$\element$};
     \xdef\FitList{(tableau-\thetableau-1) (tableau-\thetableau-\Z)}
    \fi

  } 
  \node[fit=\FitList,inner sep=0pt,name=tableau-\thetableau]{};
}

\begin{tikzpicture}
\foreach \index/\list in {1/{e,f,~}, 2/{g,h,~}, 3/{~,~,~}} {
   \LinkedList[anchor=north,xshift=\index*3cm,xshift=1cm]{\list}
}
\foreach \index/\list in {1/{a,b,~}, 2/{c,d,~}, 3/{e,f,~}} {
   \LinkedList[yshift=-2cm,xshift=\index*3cm,xshift=1cm]{\list}
}
\foreach \index/\list in {1/{i,j,~}, 2/{~,~,~}, 3/{e,f,~}} {
   \LinkedList[anchor=south,yshift=-4cm,xshift=\index*3cm,xshift=1cm]{\list}
}
\node[fit=(tableau-4) (tableau-6),ellipse,draw,inner sep=2pt,name=elli,
label={[right=5cm,yshift=6mm,align=center,name=elli-label]{Size\\ almost}}]{};
\draw[-latex] (elli-label) to[bend right=30] (elli.north east);

\node[name=array-1,draw,left=2.5cm of tableau-1-1,minimum height=6mm, 
             minimum width=6mm,label={[font=\small\sffamily]180:1}]{};
\foreach \X [count=\Y] in {2,...,8}
{
\ifnum\X=2
\node[name=array-\X,draw,below=-\pgflinewidth of array-\Y,minimum height=6mm, 
             minimum width=6mm,label={[font=\small\sffamily,name=lab-\X]180:\X}]{};
\else            
\ifnum\X=8
\node[name=array-\X,draw,below=-\pgflinewidth of array-\Y,minimum height=6mm, 
             minimum width=6mm,label={[font=\small\sffamily,name=lab-\X]180:$n$}]{};
\else
\node[name=array-\X,draw,below=-\pgflinewidth of array-\Y,minimum height=6mm, 
             minimum width=6mm]{};
\fi
\fi
}            

\foreach \X [count=\Z] in {2,3}
{
\foreach \Y in {0,3,6}
{
\pgfmathtruncatemacro{\Start}{\Y+\Z}
\pgfmathtruncatemacro{\Target}{\Y+\X}
\draw[-latex] (tableau-\Start.east) to[bend left=15] (tableau-\Target.west);
}
}
\foreach \X in {1,4,7}
{
\draw[-latex] (array-\X) to[bend left=15] (tableau-\X.west);
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容