我已经做了一个双向链表,如下所示,但我仍然不满足开始和结束的标签注释。我知道现在这是一种不好的风格,但不知道如何简化它。
\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}
\usepackage{xifthen}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.multipart,chains,arrows,positioning}
\begin{document}
\begin{preview}
\tikzset{>=latex}
\def\N{6}
\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=M0 circle placed {at=(\tikzchaincount*360/\N:2.5cm)},very thick
]
\foreach \i in {1,...,\N} {
\node[list,on chain,rotate={360*\i/\N-90}] (P\i) {\nodepart{second} $P_{\i}$};
}
\foreach \i [evaluate=\i as \j using int(\i+1)] in {1,...,\N} {
\ifthenelse{\i=\N} {\pgfmathsetmacro\j{1}}{}
\path[*->,red] (P\j.three) edge [bend left] (P\i.north west);
\path[*->,blue] (P\i.one) edge [bend left] (P\j.south east);
}
\draw[->,rotate=360/\N-90] ([yshift=1cm]P1.north) -- node [above,rotate=360/\N] {begin} (P1.north);
\draw[->,rotate=-90] ([yshift=1cm]P\N.north) -- node [above,rotate=0] {end} (P\N.north);
\draw[->,dashed] (360/\N:2cm) arc (360/\N:270:2cm);
\end{tikzpicture}
\end{preview}
\end{document}
希望改进:
- 我们可以使用 Mod 来替换 ifthenelse 语句吗?
- 标签似乎需要与矩形一样旋转,我们是否可以对矩形执行相同的操作,然后只需要旋转一次。
输出:
答案1
我希望我正确理解了你想要做什么。除了你关心的两点之外,我还让它随着节点数而扩展,但它很快就变成了一个巨大的图表。
代码
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{calc,shapes.multipart,chains,arrows,positioning}
\begin{document}
\tikzset{>=latex}
\def\N{11}
\begin{tikzpicture}
[ list/.style=
{ very thick, draw, minimum size=18pt, inner sep=5pt, text=black,
rectangle split, rectangle split parts=3, rectangle split horizontal,
rectangle split part fill={blue!20, red!20, blue!20}
},
->, start chain=M0 circle placed {at=(\tikzchaincount*360/\N:{1.25/sin(180/\N)})}, very thick
]
\foreach \i in {1,...,\N} {
\node[list,on chain, rotate={360*\i/\N-90}] (P\i) {\nodepart{second} $P_{\i}$};
}
\foreach \i in {1,...,\N} {
\pgfmathtruncatemacro{\j}{mod(\i,\N)+1}
\path[*->,red] (P\j.three) edge [bend left] (P\i.north west);
\path[*->,blue] (P\i.one) edge [bend left] (P\j.south east);
}
\draw[->] (P1.north) -- node [above, sloped] {begin} ++ (360/\N:2cm);
\draw[->] (P\N.north) -- node [above, sloped] {end} ++ (360:2cm);
\draw[->,dashed] (360/\N:{1.25/sin(180/\N)*0.8}) arc (360/\N:270:{1.25/sin(180/\N)*0.8});
\end{tikzpicture}
\end{document}