how to draw circular doubly linked list?

How should I draw a singly/double linked list?

  • I need to draw some http://en.wikipedia.org/wiki/Linked_list 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.

  • Answer:

    Here's a possible solution using TikZ: I used multipart rectangles for the double nodes and a chain to place the nodes. \documentclass{article} \usepackage{tikz} \usetikzlibrary{calc,shapes.multipart,chains,arrows} \begin{document} \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); \end{tikzpicture} \end{document}

andrasi at TeX - LaTeX Visit the source

Was this solution helpful to you?

Other answers

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 \documentclass[pstricks,border=3pt]{standalone} \usepackage{pstricks-add,fp} \psset{dimen=middle} \makeatletter \newcommand\const[3][\FPeval]{% #1=method, #2=name, #3=data \expandafter#1\csname#2\endcsname{#3}% \begingroup\edef\x{\endgroup \noexpand\pstVerb{/#2 \csname#2\endcsname\space def}}\x } \newcount\const@count \def\FPnset#1#2{% \const@count=\z@ \@for\next:=#2\do{\advance\const@count\@ne}% \edef#1{\number\const@count}% } \makeatother % 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{_QW}{_W/4} \const{_HW}{_W/2} \const{_TQW}{3*_W/4} \const{_HH}{_H/2} \const{_NL}{_TQW+_D} \const{CanvasHeight}{_H}% Canvas Height = _H \const{CanvasWidth}{_NL * _N + _HW}% Canvas Width = _NL*_N+_HW \def\node#1#2{% \rput(!_NL #2 mul 0){% \psframe(!_W _H)% \rput(!_QW _HH){#1}% \psline(!_HW 0)(!_HW _H)% \psline[arrowscale=2]{*->}(!_TQW _HH)(!_NL _HH)% }} \def\finish{% \rput(!_NL _N mul 0){% \psframe(!_HW _H)% \psline(!_HW _H)% \psline(!0 _H)(!_HW 0)% }} \def\DrawLinkedList{% \begin{pspicture}(\CanvasWidth,\CanvasHeight) \psforeach{\i}{\list}{\node{\i}{\the\psLoopIndex}}\finish \end{pspicture}} \begin{document} \DrawLinkedList \end{document}

I am who I say I am

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.

David Hammen

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.