1

Possible Duplicate:
How should I draw a singly/double linked list?

I am currently trying to create an image of a linked list like this one: enter image description here

This is my current try:

enter image description here

What I miss is the dot at the start of the arrow and the second box for each list element.

This is my try:

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

\usepackage{tikz}
\usetikzlibrary{trees,arrows,positioning, calc} 

\tikzset{
    squarecross/.style={
        draw, fill=black!20, rectangle,minimum size=18pt,
        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);
        }
    },
    listelement/.style={
        draw, fill=black!20, rectangle,minimum size=18pt,
        inner sep=0pt, text=black,
        path picture = {% 
            \draw[black]
            ($(path picture bounding box.north west)$) --
            ($(path picture bounding box.north west)+(1,0)$) --
            ($(path picture bounding box.south west)+(1,0)$) -- 
            ($(path picture bounding box.south west)$);
        }
    }
}

\usetikzlibrary{trees,arrows,positioning, calc}

\begin{document}
\begin{preview}
\begin{tikzpicture}[font=\sffamily,very thick]
\node [listelement] (a) {12};
\node [listelement] (b) [right=of a] {99};
\node [listelement] (c) [right=of b] {37};
\node [squarecross] (d) [right=of c] {};
\draw [->] (a) -- (b);
\draw [->] (b) -- (c);
\draw [->] (c) -- (d);
\end{tikzpicture}
\end{preview}
\end{document}

How can I make it work? Why does the following part not work?

path picture = {% 
    \draw[black]
    ($(path picture bounding box.north west)$) --
    ($(path picture bounding box.north west)+(1,0)$) --
    ($(path picture bounding box.south west)+(1,0)$) -- 
    ($(path picture bounding box.south west)$);
}
Martin Thoma
  • 18,799

1 Answers1

1

The path picture is used to put a picture inside the path region so when you draw something out of the path region they are clipped out. Just to illustrate the scaling

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc} 

\tikzset{
    listelement/.style={
        draw, fill=black!20, rectangle,minimum size=5mm,
        inner sep=0pt, text=black,
        path picture = {%
        \pgftransformscale{3}
            \draw[black]
            ($(path picture bounding box.north west)$) rectangle ++(1mm,-1mm);
        }
    }
}
\begin{document}
\begin{tikzpicture}[font=\sffamily,very thick]
\node [listelement] (a) {12};
\end{tikzpicture}
\end{document}

enter image description here

percusse
  • 157,807