Logo

Programming-Idioms

  • Rust
  • Pascal

Idiom #293 Create a stack

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

uses contnrs;
s := TStack.Create;
s.Push(x);
y := s.Pop;
let mut s: Vec<T> = vec![];
s.push(x);
let y = s.pop().unwrap();

T is the type of the elements.

pop() returns an Option<T>.
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