MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Making a stack.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128473] Re: Making a stack.
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Tue, 23 Oct 2012 00:56:38 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <20121022060332.6F48D6868@smc.vnet.net>

SetAttributes[enqueue, HoldRest]

enqueue[x_, queue_Symbol] :=
  If[Head[queue] === List,
   AppendTo[queue, x], queue = {x}];

enqueue[3, q1]

{3}

x = 5;

enqueue[x, q1]

{3, 5}

Or if you want to require manual creation of queue

enqueue[x_, queue_Symbol] :=
  AppendTo[queue, x];

q2 = {};

enqueue[7, q2]

{7}

enqueue[x, q2]

{7, 5}


Bob Hanlon


On Mon, Oct 22, 2012 at 2:03 AM, Brentt <brenttnewman at gmail.com> wrote:
>
> Hi, I'm trying to make stack functions like enqueue and dequeue to
> demonstrate algorithms. I know enqueue would do pretty much the same thing
> as AppendTo, but I want the arguments reversed and it to be called
> "enqueue". I figured this would work:
>
> enqueue[x_, queue_] := AppendTo[queue, x];
>
> But it spits out red. I tried to tinker with the evaluation order but to no
> avail. Is anyway to make this work? I want the functions to work like they
> would in a procedural language (I'm not using a procedural language because
> I want to accompany the algorithms with visualizations---i.e. it's for a
> demonstration project). I know I can just use Set when I want to que
> something, but I was hoping to figure out a way to make the code look like
> procedural code, and I'm guessing Mathematica is flexible enough to do
> that.
>
> Any ideas?



  • Prev by Date: Tensor product of Matrices
  • Next by Date: Re: Parametric List Plot??
  • Previous by thread: Re: Making a stack.
  • Next by thread: Re: Making a stack.