logo

Polyglot CheatSheet - Stacks

Last Updated: 2022-04-08

Java

import java.util.Stack;

Stack<Integer> stack = new Stack<>();

stack.push(i);
stack.pop();

stack.isEmpty();

Python

List can be used as a stack:

stack = []

stack.append(1)
stack.pop()

# Check empty
not stack

Use collections.deque for quicker append() and pop():

from collections import deque

stack = deque()

stack.append(1)
stack.pop()