Stack<T> (LIFO)

Arrays and collections

int_stack <- numeric(0)

# The top of the stack is the first element
int_stack <- c(1, int_stack)
int_stack <- c(3, int_stack)
int_stack <- c(5, int_stack)

# Look at the top without removing it
top <- int_stack[1]
# top = 5

first <- int_stack[1]
int_stack <- int_stack[-1]
# first = 5
second <- int_stack[1]
int_stack <- int_stack[-1]
# second = 3
third <- int_stack[1]
int_stack <- int_stack[-1]
# third = 1

is_empty <- length(int_stack) == 0
# is_empty = TRUE

cat("top =", top, "\n")
cat("first =", first, "\n")
cat("second =", second, "\n")
cat("third =", third, "\n")
cat("is_empty =", is_empty, "\n")