9

I want to implement an array of linked lists in La TeX like it appeared in data structure (graphic):

the array elements are the numbers{1,2,3} and every number in the array is ahead of linked list{the first element in array is the head of the linked list a->b->null, where a and b are the nodes in the linked list, and so on to the other array elements}

|1|->a->b->null

|2|->c->null

|3|->d->null

can you help me in this?

1 Answers1

12

I tried to write a code which provides the simplest interface:

\begin{tikzpicture}
\foreach \index/\list in {1/{a,b,null}, 2/{c,null}, 3/{d,null}} {
   \node[array element] (aux) at (0,-\index) {\index};
   \LinkedList{\list}
}
\end{tikzpicture}

Result

This is the complete source code:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\usetikzlibrary{positioning}
\tikzset{
node of list/.style = { 
             draw, 
             fill=orange!20, 
             minimum height=6mm, 
             minimum width=6mm,
             node distance=6mm
   },
link/.style = {
     -stealth,
     shorten >=1pt
     },
array element/.style = {
    draw, fill=white,
    minimum width = 6mm,
    minimum height = 10mm
  }
}

\def\LinkedList#1{%
  \foreach \element in \list {
     \node[node of list, right = of aux, name=ele] {\element};
     \draw[link] (aux) -- (ele);
     \coordinate (aux) at (ele.east);
  } 
}

\begin{tikzpicture}
\foreach \index/\list in {1/{a,b,null}, 2/{c,null}, 3/{d,null}} {
   \node[array element] (aux) at (0,-\index) {\index};
   \LinkedList{\list}
}
\end{tikzpicture}
\end{document}

However, the above is not the standard way of representing linked lists. I prefer this one instead:

Result2

Which is obtained simply replacing the definition of \LinkedList above with this one:

\def\LinkedList#1{%
  \foreach \element in \list {
     \node[node of list, right = of aux, name=ele] {\element};
     \node[node of list, name=aux2, anchor=west] at ([xshift=-.4pt] ele.east) {};
     \draw[link] (aux) -- (ele);
     \coordinate (aux) at (aux2);
   }
   \fill (aux) circle(2pt);
}

and removing the null element in the list specification, i.e. the main loop is now:

\foreach \index/\list in {1/{a,b}, 2/{c}, 3/{d}} {

A small addition, if future readers need this kind of behavior:

\foreach [count=\i] \index/\list in {1/{a,b}, 20/{c}, 50/{d}} {
    \node[array element] (aux) at (0,-\i) {\index};
    \LinkedList{\list}
}

outputs the linked list with only the needed array cells rendered, eg.

a example linked list

Yixiang
  • 3
  • 3
JLDiaz
  • 55,732
  • Hi, I'm trying to use this solution for a presentation (beamer class) but it appears its resizing the squares for the array, for example if the array is 1, 200, 5000, each square has a different size. Also, Is there a way to replace the circles with a custom character? – aram Dec 18 '15 at 22:29
  • @Aram For the size of the squares, modify array element/.style and set, for example, minimum width=12mm, or the required amount. For the second question, change \fill (aux) circle(2pt); by \node at (aux) {*};, for example (replace the * for the desired character). – JLDiaz Dec 22 '15 at 09:23