Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

During this discussion an example image generated with TikZ was given by Christian R. but no code was provided:

enter image description here

It would be great to have the code snippet that implements it, because I think it has the basic pieces to typeset the most common data structures found in imperative languages.

share|improve this question
2  
related? –  Marc van Dongen Jan 15 '13 at 18:36

1 Answer 1

up vote 7 down vote accepted

Well, I guess I can help. :-)

Code

\documentclass{scrartcl}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows,calc}
\newcommand{\data}{data \nodepart{second} \phantom{null}}

\tikzstyle{ptr}  = [draw, -latex']
\tikzstyle{head} = [rectangle, draw, text height=3mm, text width=3mm, text centered, node distance=3cm, inner sep=0pt]
\tikzstyle{data} = [rectangle split, rectangle split parts=2, draw, text centered, minimum height=3em]

\begin{document}

\begin{tikzpicture}[node distance=2cm, auto]

    \node[head, label=below:head] (head) {};
    \node[data, right of=head]    (A) {\data};
    \node[data, right of=A]       (B) {\data};
    \node[data, right of=B]       (C) {\data};
    \node[above of=C,head,node distance=2cm,label=above:prev]   (prev){};
    \node[data, right of=C]       (D) {\data};
    \node[above of=D,head,node distance=2cm,label=above:curr]   (curr){};
    \node[data, right of=D]       (E) {\data};
    \node[data, right of=E]       (last) {data \nodepart{second} null};

    \draw[fill] (head.center)   circle (0.05);
    \draw[fill] (prev.center)   circle (0.05);
    \draw[fill] (curr.center)   circle (0.05);

    \path[ptr]  (head.center) --++(right:7.5mm)  |- (A.text west);
    \draw[fill] ($(A.south)!0.5!(A.text split)$) circle (0.05);
    \draw[ptr]  ($(A.south)!0.5!(A.text split)$) --++(right:10mm) |- (B.text west);
    \draw[fill] ($(B.south)!0.5!(B.text split)$) circle (0.05);
    \draw[ptr]  ($(B.south)!0.5!(B.text split)$) --++(right:10mm) |- (C.text west);
    \path[ptr] (prev) -- (C);
    \draw[fill] ($(C.south)!0.5!(C.text split)$) circle (0.05);
    \draw[ptr]  ($(C.south)!0.5!(C.text split)$) --++(right:10mm) |- (D.text west);
    \path[ptr] (curr) -- (D);
    \draw[fill] ($(D.south)!0.5!(D.text split)$) circle (0.05);
    \draw[ptr]  ($(D.south)!0.5!(D.text split)$) --++(right:10mm) |- (E.text west);
    \draw[fill] ($(E.south)!0.5!(E.text split)$) circle (0.05);
    \draw[ptr]  ($(E.south)!0.5!(E.text split)$) --++(right:10mm) |- (last.text west);

\end{tikzpicture}

\end{document}

The result (even though it can be seen already in the question)

image

I'm pretty sure the code can be simplified, so improvements are welcome.

share|improve this answer
    
Instead of explicit circles try \tikzstyle{ptr} = [draw, -latex',postaction={decorate,decoration={markings,mark=at position 0 with {\fill circle(0.05);}}}] –  Geoff Reedy Jan 15 '13 at 22:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.