LaTeX

TikZ : Linked List 그리기

ForceCore 2010. 8. 28. 21:47
사실 linked list가 어려운게 아니라 NULL 포인터 그리는게 어렵다.


Code:

%\documentclass[10pt,a4paper]{article}
%\usepackage{tikz}
%\begin{document}

\begin{tikzpicture}

% ground symbol
\def\ground{
-- +(0mm,-4.0mm) {
[yshift=-4mm]
+(-2mm,0mm) -- +(2mm,0mm)
+(-1mm,-1mm) -- +(1mm,-1mm)
+(-0.3mm,-2mm) -- +(0.3mm,-2mm)
}
}

% node style definition
\tikzstyle{pointer} = [
draw, fill=black!10, rectangle,
minimum height=4mm, minimum width=10mm ];
\tikzstyle{data} = [
draw, fill=black!10, rectangle,
minimum height=8mm, minimum width=10mm ];
\tikzstyle{plain} = [draw=none,fill=none];

% head
\node[plain] (head) at (0,4mm) {head};

% mid nodes
\node[pointer] (hn) at (2,4mm)    {};
\node[pointer] (hp) at (2,0)  {};
\node[data]    (hd) at (2,-6mm) {$-\infty$};

\node[pointer] (an) at (4,4mm)  {};
\node[pointer] (ap) at (4,0)    {};
\node[data]    (ad) at (4,-6mm) {7};

\node[pointer] (bn) at (6,4mm)  {};
\node[pointer] (bp) at (6,0)    {};
\node[data]    (bd) at (6,-6mm) {23};

\node[pointer] (tn) at (8,4mm)  {};
\node[pointer] (tp) at (8,0)    {};
\node[data]    (td) at (8,-6mm) {$\infty$};

% forward links
\draw[-stealth] (head) to (hn);
\draw[-stealth] (hn.center) to (an);
\draw[-stealth] (an.center) to (bn);
\draw[-stealth] (bn.center) to (tn);

% back links
\draw[-stealth] (tp.center) to (bp);
\draw[-stealth] (bp.center) to (ap);
\draw[-stealth] (ap.center) to (hp);

% "NULL" pointers
\draw (tn.center) -- ++( 15mm,0mm) -- ++(0mm,-6mm) \ground; % tail
\draw (hp.center) -- ++(-15mm,0mm) -- ++(0mm,-2mm) \ground; % head

\end{tikzpicture}
%\end{document}

ㅋㅋㅋ
눈겨여 봐야 할 것은, 마지막에 ground찍기 잔에 ++(15mm,0mm)이렇게 좌표를 찍어주었다는 것이다.
+(15mm,0mm)만 하면 그 뒤에 오는 ++(imm,-6mm)이 tn.center를 중심으로 계산되지만 ++를 하면 그만큼 이동한 곳을 중심으로 계산된다.