www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - queue container?

--bcaec53aec3eee132204b02a8b1a
Content-Type: text/plain; charset=ISO-8859-1

I need a Queue (put on the front and pop from the back).  The best I can
come up with so far is:

    Array!string list;
    list.insertBack("a1");
    list.insertBefore(list.opSlice(0,1), "a2");
    list.insertBefore(list.opSlice(0,1), "a3");
    list.insertBefore(list.opSlice(0,1), "a4");
    list.insertBefore(list.opSlice(0,1), "a5");

    while (!list.empty())
    {
        string s = list.back();
        list.removeBack();
        writeln("x: ", s);
    }

Having to insertBack when the list empty and insertBefore with the range,
seems strange to me. Is there a better way?

John

--bcaec53aec3eee132204b02a8b1a
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div><div>I need a Queue (put on the front and pop from the back). =A0The b=
est I can come up with so far is:</div><div><br></div><div><div><font class=
=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 Arr=
ay!string list;</font></div>
<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0 =A0 list.insertBack(&quot;a1&quot;);</font></div><div><font class=
=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 lis=
t.insertBefore(list.opSlice(0,1), &quot;a2&quot;);</font></div>
<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0 =A0 list.insertBefore(list.opSlice(0,1), &quot;a3&quot;);</font></=
div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, mo=
nospace">=A0 =A0 list.insertBefore(list.opSlice(0,1), &quot;a4&quot;);</fon=
t></div>
<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0 =A0 list.insertBefore(list.opSlice(0,1), &quot;a5&quot;);</font></=
div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, mo=
nospace"><br>
</font></div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new=
&#39;, monospace">=A0 =A0 while (!list.empty())</font></div><div><font clas=
s=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 {<=
/font></div>
<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0 =A0 =A0 =A0 string s =3D list.back();</font></div><div><font class=
=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 =A0=
 =A0 list.removeBack();</font></div>
<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0 =A0 =A0 =A0 writeln(&quot;x: &quot;, s);</font></div><div><font cl=
ass=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0 =A0 =
}</font></div></div>
<div><br></div><div>Having to insertBack when the list empty and insertBefo=
re with the range, seems strange to me. Is there a better way?</div><div><b=
r></div><div>John<br><div><br></div><div><div><br></div><div><br></div>
</div></div></div>

--bcaec53aec3eee132204b02a8b1a--
Oct 25 2011