Logo

Programming-Idioms

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

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
var s = [];
s.add(x);
var y = s.removeLast();
type Stack[T any] struct {
	items []T
}

func (s *Stack[T]) Push(t T) {
	s.items = append(s.items, t)
}

func (s *Stack[T]) Pop() T {
	n := len(s.items)
	t := s.items[n-1]
	var zero T
	s.items[n-1] = zero
	s.items = s.items[:n-1]
	return t
}

var s = new(Stack[string])
s.Push(x)
y := s.Pop()
const s = [1, 2, 3];
s.push(x);
const y = s.pop();
import java.util.Stack;
Stack<T> s = new Stack<>();
s.push(x);
T y = s.pop();
uses contnrs;
s := TStack.Create;
s.Push(x);
y := s.Pop;
use strict;
my @s;
push @s, $x;
my $y = pop @s;
s = []
s.append(x)
y = s.pop()
s = []
s.push(x)
y = s.pop
let mut s: Vec<T> = vec![];
s.push(x);
let y = s.pop().unwrap();