标记第一个和最后一个元素以及入队和出队的代码

标记第一个和最后一个元素以及入队和出队的代码
\documentclass[tikz,margin=1cm]{standalone}
\usetikzlibrary{arrows.meta}


\tikzset{
    queue element/.style={
        draw,very thin,
        minimum width=1cm,minimum height=.1cm,
        fill=brown!30, 
        font=\sffamily\footnotesize
    },
    >={[scale=0.8]Triangle}
}

\newcommand\x{1}
\newcommand\y{5}
%Enqueue:
\newcommand\m{1}
\newcommand\n{6}
%Dequeue:
\newcommand\s{2}
\newcommand\f{6}


\begin{document}
\begin{tikzpicture}

\scope[yshift=-1cm] 
\foreach \i/\name in {\x,...,\y}{
        \node[queue element] (\i) at (1*\i,0){\i};
        \draw[<-] ([yshift=.2cm]\x.north) -- ++ (0,.5) node[above] {front};
     }  
   \draw[<-] ([yshift=.2cm]\y.north) -- ++ (0,.5) node[above] {rear};
\endscope

%Enqueue:
\scope[yshift=-3cm] 
\foreach \i in {\m,...,\n}{
        \node[queue element] (\i) at (1*\i,0){\i};
        \draw[<-] ([yshift=.2cm]\m.north) -- ++ (0,.5) node[above] {front};
        }  
   \draw[<-] ([yshift=.2cm]\n.north) -- ++ (0,.5) node[above] {rear};
    \path (-2.5,0) node[right] {Enqueue:};
\endscope

%Dequeue:
\scope[yshift=-5cm] 
\foreach \i in {\s,...,\f}{
        \node[queue element] (\i) at (1*\i,0){\i};
        \draw[<-] ([yshift=.2cm]\s.north) -- ++ (0,.5) node[above] {front};
        }  
      \draw[<-] ([yshift=.2cm]\f.north) -- ++ (0,.5) node[above] {rear};
      \path (-2.5,0) node[right] {Dequeue:};
\endscope

\end{tikzpicture}
\end{document}

如果有人需要这个(红色圈出),只需将 draw 放在 foreach 外面作为最后一个元素,将 第一个放在 foreach 中(就像我在我的代码中所做的那样):

在此处输入图片描述

答案1

像这样:

在此处输入图片描述

您只需要在队列中的最后一个元素上方添加节点:

\documentclass[tikz,margin=1cm]{standalone}
\usetikzlibrary{arrows.meta}

\tikzset{
 queue element/.style={
        draw, very thin,
        minimum width=1cm ,minimum height=1em,
        fill=brown!30,
        font=\sffamily\footnotesize
        },
    >={[scale=0.8]Triangle}
        }
\newcommand\x{1}
\newcommand\y{5}

\newcommand\m{1}
\newcommand\n{6}

\newcommand\s{2}
\newcommand\f{6}


\begin{document}
    \begin{tikzpicture}
\foreach \i in {\x,...,\y}{
\node[queue element] (\i) at (\i,0) {\i};
     }
\draw[<-] ([yshift=5mm]\x.north) -- ++ (0,5mm) node[above] {front};
\draw[<-] ([yshift=5mm]\y.north) -- ++ (0,5mm) node[above] {tail};

\begin{scope}[yshift=-3cm]
\foreach \i in {\m,...,\n}{
\node[queue element] (\i) at (\i,0) {\i};
        }
\draw[<-] ([yshift=.2cm]\m.north) -- ++ (0,.5) node[above] {front};
\draw[<-] ([yshift=.2cm]\n.north) -- ++ (0,.5) node[above] {tail};
\node[right] at (-22mm,0) {Enqueue:};
\end{scope}

\begin{scope}[yshift=-5cm]
\foreach \i in {\s,...,\f}{
\node[queue element] (\i) at (1*\i,0){\i};
        }
\draw[<-] ([yshift=.2cm]\s.north) -- ++ (0,.5) node[above] {front};
\draw[<-] ([yshift=.2cm]\f.north) -- ++ (0,.5) node[above] {tail};
\node[right] at (-22mm,0) {Dequeue:};
\end{scope}
    \end{tikzpicture}
\end{document}

注意:请保持一致的 LaTeX 使用!

答案2

欢迎来到 TeX.SE!!!

我会用 来\pic表示“队列”。就像这个例子一样,尝试(近似地)复制 Zarko 的图片:

编辑:我改变了全局变量的使用,因为它可能会导致整个文档(不是standalone一个文档)中出现一些冲突。请参阅 Egreg 的评论。

\documentclass[tikz,border=3pt]{standalone}

\tikzset
{%
  pics/queue/.style 2 args={% #1 = first element, #2 = last element
    code={%
      \foreach[count=\j]\i in {#1,...,#2}
      {%
        \draw[pic actions] (\j,-0.25) rectangle ++ (1,0.5);
        \node at (\j+0.5,0) {$\i$};
      }
      \draw[latex-]    (1.5,0.5) --++ (0,1) node[above] {front};
      \draw[latex-] (#2+0.5,0.5) --++ (0,1) node[above] {rear};
  }},
}

\begin{document}
\begin{tikzpicture}
  % \pics
  \pic[fill=orange!30] at (1,0) {queue={1}{5}};
  \pic[fill=orange!30] at (0,3) {queue={1}{6}};
  \pic[fill=orange!30] at (0,7) {queue={1}{5}};
  % labels
  \node[right] at (-1,3) {\strut Enqueue:};
  \node[right] at (-1,0) {\strut Dequeue:};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容