0
\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}

If anyone needs this(circled red) just put draw outside of foreach for last elemen and 1st in foreach(like i did in my code):

enter image description here

Dovla
  • 1
  • Welcome to TeX.sx. Could you please clarify your question by explaining what exactly you want? Your question title doesn't completely explain what it is you're struggling with (at least I don't fully understand what you're asking us). Additionally, please note that you can highlight your code as such by putting three backticks in the lines above and below it (which I've already done for you). – Skillmon Dec 29 '22 at 17:27
  • Unrelated, but \the\numexpr does nothing useful where you use it; it would do if you need to do arithmetic with \x and friends. – egreg Dec 29 '22 at 17:35
  • This is a combination of this concept and testing for \i=\s and \i=\f. You could also give your nodes proper names, say (i\i), and then you can reference them with (i\s) and (i\f). The chains library could make this even easier by providing (chain-begin) and (chain-end) names for the first and last node in a chain. And the pin option might make it even easier to add an edge to a node. – Qrrbrbirlbel Dec 29 '22 at 18:32

2 Answers2

2

Like this:

enter image description here

You only need to addnodes above last element in your queue:

\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}

Note: please be consistent in use of LaTeX!

Zarko
  • 296,517
1

Welcome to TeX.SE!!!

I'd do this with a \pic for the 'queues'. Like this example, trying to replicate (approximately) Zarko's picture:

Edit: I changed the use of a global variable because it could cause some conflicts in a complete document (a not standalone one). See Egreg's comment.

\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}

enter image description here

Juan Castaño
  • 28,426
  • 1
    For a standalone document, \global\let\last\j might be good, but in a real document it would be risky, possibly disastrous if \last is already defined. – egreg Dec 30 '22 at 13:20
  • @egreg, what would be a more robust way to do it? And thanks for pointing it, BTW – Juan Castaño Dec 30 '22 at 13:24
  • Well, the user does know what are the first and the last elements, because they're encoded in \x and \y (or similarly for the other blocks). No need to “remember” values, they're already given. – egreg Dec 30 '22 at 13:28
  • Thanks again, @egreg. It's corrected now! – Juan Castaño Dec 30 '22 at 14:04