2

I have a chain of nodes connected by arrows. I positioned the arrows exactly as I'd like using angles. However, the arrows at either end of the chain point to a node which holds some text. I can't seem to get the text of these nodes to align with the arrows. See the image below:

enter image description here

The text "NULL" should be aligned with the arrows at the end. How can I accomplish this? I tried to use yshift where the node is defined but that is ignored. Here is an MWE:

\documentclass{article} \usepackage{tikz} \usetikzlibrary{arrows.meta, bending,calc,shapes.multipart,chains,arrows,decorations.markings}

\begin{document}
 \begin{tikzpicture}[
      every edge/.style = {draw, -{Stealth[angle=32:10pt, bend]}},
      start chain = going right,
      list/.style = {rectangle split, rectangle split parts=3,
      rectangle split horizontal,
      draw, thick, minimum height=1cm,
      on chain},
    ]
    \node [on chain] (X) {NULL};
    \begin{scope}[nodes={list}]
      \node   (A) {\nodepart{second}$A_1$};
      \node   (B) {\nodepart{second}$A_2$};
      \node   (C) {\nodepart{second}$A_3$};
      \node   (D) {\nodepart{second}$A_4$};
      \node   (E) {\nodepart{second}$A_5$};
    \end{scope}
    \node [on chain] (Y) {NULL};
    %
    \draw   
    (A.170) edge (X.12)
    (A.10) edge (B.170)
    (B.195) edge (A.-15)
    (B.10) edge (C.170)
    (C.195) edge (B.-15)
    (C.10) edge (D.170)
    (D.195) edge (C.-15)
    (D.10) edge (E.170)
    (E.195) edge (D.-15)
    (E.10) edge (Y.168);
  \end{tikzpicture}
  \end{document}

1 Answer 1

4

You do not need the X and Y nodes to be on chain.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,bending,chains,shapes.multipart}
\begin{document}
 \begin{tikzpicture}[
      every edge/.style = {draw, -{Stealth[angle=32:10pt, bend]}},
      start chain = going right,
      list/.style = {rectangle split, rectangle split parts=3,
      rectangle split horizontal,
      draw, thick, minimum height=1cm,
      on chain},
    ]
    \begin{scope}[nodes={list}]
      \node   (A) {\nodepart{second}$A_1$};
      \node   (B) {\nodepart{second}$A_2$};
      \node   (C) {\nodepart{second}$A_3$};
      \node   (D) {\nodepart{second}$A_4$};
      \node   (E) {\nodepart{second}$A_5$};
    \end{scope}
    %
    \draw   
    (A.170) ++ (-1,0) coordinate[label=left:NULL] (X)
    (A.170) edge (X)
    (A.10) edge (B.170)
    (B.195) edge (A.-15)
    (B.10) edge (C.170)
    (C.195) edge (B.-15)
    (C.10) edge (D.170)
    (D.195) edge (C.-15)
    (D.10) edge (E.170)
    (E.195) edge (D.-15)
    (E.10)  ++ (1,0) coordinate[label=right:NULL] (Y) 
    (E.10) edge (Y)
    ;
  \end{tikzpicture}
\end{document}

enter image description here

BTW, you could shorten the code.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,bending,chains,shapes.multipart}
\begin{document}
 \begin{tikzpicture}[
      every edge/.style = {draw, -{Stealth[angle=32:10pt, bend]}},
      start chain = going right,
      list/.style = {rectangle split, rectangle split parts=3,
      rectangle split horizontal,
      draw, thick, minimum height=1cm,
      on chain},
    ]
    \begin{scope}[nodes={list}]
     \path foreach \X [count=\Y,remember=\X as \LastX] in {A,...,E}
      {node   (\X) {\nodepart{second}$A_{\Y}$}
      \ifnum\Y>1
       ([yshift=1.4mm]\LastX.east) edge ([yshift=1.4mm]\X.west)
       ([yshift=-1.4mm]\X.west) edge ([yshift=-1.4mm]\LastX.east)
      \fi};
    \end{scope}
    %
    \path ([yshift=1.4mm]A.west) ++ (-1,0) coordinate[label=left:NULL] (X) 
      ([yshift=1.4mm]A.west) edge (X)
      ([yshift=-1.4mm]E.east)  ++ (1,0) coordinate[label=right:NULL] (Y)  
      ([yshift=-1.4mm]E.east) edge (Y);
  \end{tikzpicture}
\end{document}
1
  • Oh great thanks! I'm still learning Tikz, I did not know loops were possible!
    – John
    Dec 7, 2020 at 14:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .