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.

I need to draw some singly and double linked lists for a CS paper and I don't know what package should I use. As far as I've searched over the Internet I can't seem to find a good one. Maybe TiKZ/PGF? If so, I would need a little example.

share|improve this question

3 Answers 3

up vote 18 down vote accepted

Here's a possible solution using TikZ: I used multipart rectangles for the double nodes and a chain to place the nodes.



\begin{tikzpicture}[list/.style={rectangle split, rectangle split parts=2,
    draw, rectangle split horizontal}, >=stealth, start chain]

  \node[list,on chain] (A) {12};
  \node[list,on chain] (B) {99};
  \node[list,on chain] (C) {37};
  \node[on chain,draw,inner sep=6pt] (D) {};
  \draw (D.north east) -- (D.south west);
  \draw (D.north west) -- (D.south east);
  \draw[*->] let \p1 = (A.two), \p2 = (A.center) in (\x1,\y2) -- (B);
  \draw[*->] let \p1 = (B.two), \p2 = (B.center) in (\x1,\y2) -- (C);
  \draw[*->] let \p1 = (C.two), \p2 = (C.center) in (\x1,\y2) -- (D);


enter image description here

share|improve this answer
Thank you! That's exactly what I was looking for! I was looking like a fool in the pgfmanual and I couldn't figure it out. If I could find an index with all the possible options for drawing with tikz I could make all this by myself but I didn't find any... For example now I need to draw a dotted arrow (the body of the arrow to be made from dots like this ...> but then again, I can't find the option, I've tried \draw[.>] or \draw[.] and it doesn't work... Anyway, thank you again! It works perfectly! –  andrasi May 28 '11 at 14:40
@andrasi: You're welcome. For the problem with the arrow, use the dotted option; in one of the arrows in my example, try \draw[dotted,*->]. –  Gonzalo Medina May 28 '11 at 14:52
Thanks! It works like a charm! –  andrasi May 28 '11 at 15:19
The lines which cross out the last rectangle are a bit too long - to solve this: \draw[shorten <= 1pt, shorten >= 1pt] (D.north east) -- (D.south west); \draw[shorten <= 1pt, shorten >= 1pt] (D.north west) -- (D.south east); –  bobb_the_builder Jun 4 at 16:47

Users only need to modify the following values and keep the names remain unchanged.

% user defined data
\def\list{12,99,37}% list of elements
\const{_W}{2}% node width
\const{_H}{1}% node height
\const{_D}{1.5}% arrow length

enter image description here


\newcommand\const[3][\FPeval]{% #1=method, #2=name, #3=data
     \noexpand\pstVerb{/#2 \csname#2\endcsname\space def}}\x


% user defined data
\def\list{12,99,37}% list of elements
\const{_W}{2}% node width
\const{_H}{1}% node height
\const{_D}{1.5}% arrow length

% internal used constants
\const[\FPnset]{_N}{\list}% number of nodes

\const{CanvasHeight}{_H}% Canvas Height = _H
\const{CanvasWidth}{_NL * _N + _HW}% Canvas Width = _NL*_N+_HW

    \rput(!_NL #2 mul 0){%
        \psframe(!_W _H)%
        \rput(!_QW _HH){#1}%
        \psline(!_HW 0)(!_HW _H)%
        \psline[arrowscale=2]{*->}(!_TQW _HH)(!_NL _HH)%     

    \rput(!_NL _N mul 0){%
        \psframe(!_HW _H)%
        \psline(!_HW _H)%
        \psline(!0 _H)(!_HW 0)%  




share|improve this answer

Graphviz is an easy-to-use tool that lets one specify graphs without worrying too much about how they appear. The dot language is easy. Here is a singly-linked list:

foo -> bar
bar -> baz
baz -> qux

Making this bidirectional is almost as easy. You could specify that each node is bidirectional. Even easier, specify that all nodes are bidirectional:

edge [dir=both]
foo -> bar
bar -> baz
baz -> qux

There are a number of avenues to import your .dot file as a graphic in your LaTeX document. If you are using pdflatex, pipe the output of the dot command (specifying output format=ps) that comes with graphviz to ps2pdf to generate a .pdf file. You can \includegraphics this .pdf into your document.

Don't like the mixed up fonts that result? Use dot2tex (http://www.fauskes.net/code/dot2tex/) to convert the .dot file to latex as a series of pstricks commands.

share|improve this answer
Sounds interesting! Could you add a screenshot of the output? –  Jake May 28 '11 at 14:00
Thank you! It looks nice, maybe I'll try it someday! –  andrasi May 28 '11 at 14:47
New to this forum: How to upload screenshots? And it looks like comments are only for very simple text. –  David Hammen May 28 '11 at 15:59
@David: Yes, comments only allow for very basic text markup. You can, however, edit your answer to include a screenshot. See Markdown help -- images, but basically you can just click the button with a photo on it. –  doncherry Jul 26 '11 at 21:19

Your Answer


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.