# Hash tables using latex

Looking to create a hash table with linked lists in LaTeX like the one below:

• Welcome to TeX.SX. It would be very helpful if you could create an MWE. This will dramatically increase the possibility that somebody is willing to help you. – Stefan Pinnow Mar 18 '16 at 21:51
• It will also increase the chances that somebody understands what you want well enough to help you. Right now, I have no idea what you are trying to do. I know you want to make a table with # symbols and link something in the table to some list or lists. But that is rather vague. – cfr Mar 18 '16 at 21:58
• Possible duplicate of Using Tikz to draw a Hash Table – ArTourter Mar 18 '16 at 22:01

## 2 Answers

Also with tikz using nodes and the positioning library. This solution can be particularly useful in case of more complex connections.

\documentclass[tikz,border=1pt]{standalone}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}
\coordinate (0);
\foreach \t[count=\i from 0,evaluate=\i as\j using int(\i+1)] in {
110  ,
100  ,
90  ,
80  ,
70  ,
93  $\rightarrow$ 60 ,
50  ,
40  ,
96  $\rightarrow$ 30 ,
97  $\rightarrow$ 20 ,
120 $\rightarrow$ 10
}
\node at(\i.south)[anchor=north,draw,minimum height=2em,minimum width=1.5em,outer sep=0pt](\j){}
node at(\j.west)[align=right,left]{\i}
node at(\j.east)[align=left,right,xshift=-.7em]{$\rightarrow$ \t};
\end{tikzpicture}

\end{document}


Edit - as per the OP's comment:

You can also make room for numbers to be inserted in the table cells like this:

\begin{tikzpicture}
\coordinate (0);
\foreach \t/\n[count=\i from 0,evaluate=\i as\j using int(\i+1)] in {
110/  ,
100/1  ,
90/12  ,
80/  ,
70/122  ,
93  $\rightarrow$ 60/111 ,
50/56  ,
40/  ,
96  $\rightarrow$ 30/ ,
97  $\rightarrow$ 20/ ,
120 $\rightarrow$ 10/34
}
\node at(\i.south)[anchor=north,draw,minimum height=2em,minimum width=2.5em,outer sep=0pt](\j){\n}
node at(\j.west)[align=right,left]{\i}
node at(\j.east)[align=left,right,xshift=-.7em]{$\rightarrow$ \t};
\end{tikzpicture}


• ,@Katie Li - Please see my edit as per your comment below Werner's solution. – AboAmmar Mar 19 '16 at 1:24
• This is perfect, I appreciate your help! – Katie Li Mar 19 '16 at 3:03

Here is a tabular-like representation of a hashtable, wrapped inside a similarly-named environment:

\documentclass{article}

\usepackage{array}

\newenvironment{hashtable}[1][]
{\begin{tabular}[#1]{
@{}
> {\small} r <{\normalsize~\rlap{\fbox{\strut~~}}$~~\rightarrow$~}
@{} l @{}}}
{\end{tabular}}
\begin{document}

\begin{hashtable}
0 & 110 \\
1 & 100 \\
2 &  90 \\
3 &  80 \\
4 &  70 \\
5 &  93 $\rightarrow$ 60 \\
6 &  50 \\
7 &  40 \\
8 &  96 $\rightarrow$ 30 \\
9 &  97 $\rightarrow$ 20 \\
10 & 120 $\rightarrow$ 10
\end{hashtable}

\end{document}

• This is really helpful - thanks so much – Katie Li Mar 18 '16 at 22:02
• How would I edit the environment header to increase the width of the table? (i would like to add numbers inside but most of them extend past the size of the table) – Katie Li Mar 19 '16 at 0:20
• Inside the table? Where inside? I've made it such that the boxes are always empty using an \fbox. – Werner Mar 19 '16 at 0:24
• @KatieLi: Could you show a picture of what you mean? – Werner Mar 19 '16 at 0:40
• imgur.com/X8ye3vF – Katie Li Mar 19 '16 at 0:44