编辑

编辑

我想绘制两个双向链表,我知道我可以使用 tikz 包。但我不知道怎么做。有人能给出以下双向链表的示例代码片段吗?

我发现了一些与绘制双向链表相关的问题,例如,我应该如何绘制单/双链表?。但是我不知道图中的“...”该怎么画。

在此处输入图片描述

答案1

可以说有些过度,但这里有一个使用 TikZ 库shapes.multipartchains的版本positioning

\documentclass[border=20pt,tikz]{standalone}
\usetikzlibrary{shapes.multipart,chains, positioning}
\begin{document}
\begin{tikzpicture}
    [
      double link/.style n args=2{% page 726
        on chain,
        rectangle split,
        rectangle split horizontal,
        rectangle split parts=2,
        draw,
        anchor=center,
        text height=1.5ex,
        node contents={#1\nodepart{two}#2},
      },
      start chain=going right,
    ]
    \node [on chain] {$\phi$};
    \node [join={by <-}, double link={a}{b}];
    \node [join={by <->}, double link={c}{d}];
    \node (a) [join={by <->}, double link={e}{f}];
    \node (b) [on chain, right=2.5pt of a.east] {$\cdots$};
    \node [double link={y}{z}, right=2.5pt of b.east];
    \node [join={by ->}, on chain] {$\phi$};
\end{tikzpicture}
\end{document}

双重麻烦

编辑

回应询问关于添加弧线的评论,有多种选择。这里有 4 种不同颜色的弧,以便于识别。为了取悦埃格尔,我认为我读到的希腊字母事实上,它应该是一个空集。

\documentclass[border=20pt,tikz]{standalone}
\usetikzlibrary{shapes.multipart,chains, positioning}
\begin{document}
\begin{tikzpicture}
    [
      double link/.style n args=2{% page 726
        on chain,
        rectangle split,
        rectangle split horizontal,
        rectangle split parts=2,
        draw,
        anchor=center,
        text height=1.5ex,
        node contents={#1\nodepart{two}#2},
      },
      start chain=going right,
    ]
    \node [on chain] {$\emptyset$};
    \node (c) [join={by <-}, double link={a}{b}];
    \node [join={by <->}, double link={c}{d}];
    \node (a) [join={by <->}, double link={e}{f}];
    \node (b) [on chain, right=2.5pt of a.east] {$\cdots$};
    \node [join={with c by <->, bend right, draw=blue}, join={with c by <->, bend left, draw=green}, join={with c by <->, out=90, in=90, draw=red}, join={with c by <->, out=-135, in=-45, draw=magenta}, double link={y}{z}, right=2.5pt of b.east];
    \node [join={by ->}, on chain] {$\emptyset$};
\end{tikzpicture}
\end{document}

各种弧

答案2

我发现一个有用的github 存储库例子tikz 中的双向链表。您可以从中获得启发。

示例代码:

\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}

\usepackage{tikz}
\usetikzlibrary{calc,shapes.multipart,chains,arrows,positioning}

\tikzset{
    squarecross/.style={
        draw, rectangle,minimum size=18pt, fill=orange!80,
        inner sep=0pt, text=black,
        path picture = {
            \draw[black]
            (path picture bounding box.north west) --
            (path picture bounding box.south east)
            (path picture bounding box.south west) --
            (path picture bounding box.north east);
        }
    }
}

\begin{document}
\begin{preview}
\begin{tikzpicture}[
        list/.style={
            very thick, rectangle split,
            rectangle split parts=3, draw,
            rectangle split horizontal, minimum size=18pt,
            inner sep=5pt, text=black,
            rectangle split part fill={blue!20, red!20, blue!20}
        },
        ->, start chain, very thick
      ]

  \node[list,on chain] (A) {\nodepart{second} 12};
  \node[list,on chain] (B) {\nodepart{second} 99};
  \node[list,on chain] (C) {\nodepart{second} 37};

  \node[squarecross]   (D) [right=of C] {};
  \node[squarecross]   (E) [left= of A] {};

  \path[*->] let \p1 = (A.three), \p2 = (A.center) in (\x1,\y2) edge [bend left] ($(B.one)+(0,0.2)$);
  \path[*->] let \p1 = (B.three), \p2 = (B.center) in (\x1,\y2) edge [bend left] ($(C.one)+(0,0.2)$);
  \draw[*->] let \p1 = (C.three), \p2 = (C.center) in (\x1,\y2) -- (D);

  \draw[*->] ($(A.one)+(0.2,0.1)$) -- (E);
  \path[*->] ($(B.one)+(0.1,0.1)$) edge [bend left] ($(A.three)+(0,-0.05)$);
  \path[*->] ($(C.one)+(0.1,0.1)$) edge [bend left] ($(B.three)+(0,-0.05)$);
\end{tikzpicture}
\end{preview}
\end{document}

输出:

在此处输入图片描述

答案3

无 TikZ 的建议。

在此处输入图片描述

\documentclass{article}
\newcommand\boxlist[2]{\begin{array}{|c|c|} \hline #1 & #2 \\ \hline \end{array}}
\begin{document}
\[
\phi \leftarrow
\boxlist{a}{b}
\leftrightarrow
\boxlist{c}{d}
\leftrightarrow
\boxlist{e}{f}
\leftrightarrow
\cdots
\leftrightarrow
\boxlist{x}{y}
\rightarrow \phi
\]
\end{document}

答案4

\documentclass{article}
\let\LA\leftarrow  \let\RA\rightarrow \let\LRA\leftrightarrow
\def\BOX(#1,#2){\tabular{|c|c|}\hline\strut#1&#2\\\hline\endtabular}

\begin{document}

 $\thickmuskip=0mu\emptyset\,
  \LA\BOX(a,b)\LRA\BOX(c,d)\LRA\BOX(e,f)\LRA\cdots\LA\BOX(y,z)\RA\,
  \emptyset$ 

\end{document}

如果你想要斜体 a,b,...,请\array使用 \tabular

在此处输入图片描述

相关内容