A simple stack data structure in R, with supporting of assiocated methods, like push, pop and others.
an R6 class object
new()initialize a new object
simepleStack$new(items = list(), limit = Inf)
itemslist, list of items to add to the initial stack
limitint, how many items can be pushed to the stack, default is unlimited.
len()returns current length of the stack
simepleStack$len()
get()returns the full current stack of all items
simepleStack$get()
clear()remove all items in current stack
simepleStack$clear()
push()add item(s) to the stack
simepleStack$push(items, after = self$len())
itemslist, list of items to add to the stack
afterint, which position to push items after, default is after the current last item. 0 will be before the first item.
pop()remove item(s) from the stack and return as results
simepleStack$pop(len = 1, tail = FALSE)
lenint, how many items to pop from stack, default is 1 item a time.
tailbool, to pop in the reverse order (from the last item)? Default
is FALSE, pop from the top (first item).
clone()The objects of this class are cloneable with this method.
simepleStack$clone(deep = FALSE)
deepWhether to make a deep clone.
my_stack <- simepleStack$new() # check length my_stack$len() #> [1] 0 # add some thing my_stack$push(list(1, 2, 3)) # print current stack str(my_stack$get()) #> List of 3 #> $ : num 1 #> $ : num 2 #> $ : num 3 # check length my_stack$len() #> [1] 3 # add before the current first my_stack$push(list(0), after = 0) # print current stack str(my_stack$get()) #> List of 4 #> $ : num 0 #> $ : num 1 #> $ : num 2 #> $ : num 3 # pop one item my_stack$pop() #> [[1]] #> [1] 0 #> # print current stack str(my_stack$get()) #> List of 3 #> $ : num 1 #> $ : num 2 #> $ : num 3 # pop one item from the tail my_stack$pop(tail = TRUE) #> [[1]] #> [1] 3 #> # print current stack str(my_stack$get()) #> List of 2 #> $ : num 1 #> $ : num 2 # pop more than one items my_stack$pop(2) #> [[1]] #> [1] 1 #> #> [[2]] #> [1] 2 #> # print current stack str(my_stack$get()) # nothing left #> list()