Logo

Programming-Idioms

  • JS

Idiom #293 Create a stack

Create a new stack s, push an element x, then pop the element into the variable y.

use strict;
my @s;
push @s, $x;
my $y = pop @s;

$x is defined before this code block. 'my' is needed to predeclare @s when the pragma 'use strict' is in effect, and to define $y.
const s = [1, 2, 3];
s.push(x);
const y = s.pop();

This is an array used as a stack.
var s = [];
s.add(x);
var y = s.removeLast();

For simple push / pop you can use the native List in Dart. For implementing a "real" Stack see attribution URL below.

New implementation...
< >
programming-idioms.org