RStack

0th

Percentile

The RStack reference class

The RStack reference class implements the data structure stack.

Keywords
RStack
Usage
RStack
Details

A stack is an ordered list of items following the Last-In-First-Out (LIFO) principle. The push method takes elements and push them into the stack, while the pop method returns and removes the last "pushed" element in the stack.

The elements in the stack are not necessarily to be of the same type, and they can even be of function type.

Format

An object of class R6ClassGenerator of length 24.

Mutable Methods

The mutable methods changes the nodes of the instance.

push(..., collapse=NULL)

The push method creates nodes containing the values in ... and collapse, and push them into the stach on the tail, which is equivalent to the enqueue in RQueue.

pop()

The pop method returns and removes the last pushed element in the stack. It returns NULL if the stack is empty.

References

For the details about the stack data structure, see Stack at Wikipedia.

See Also

R6DS for the introduction of the reference class and some common methods

Aliases
  • RStack
Examples
# NOT RUN {
### create a new instance

# to create a new instance of the class
stack <- RStack$new()

# the previous RStack instance will be removed by running the following
# and the memory allocated for that one will be cleared,
# as now stack has been pointed to another instance of the class.
stack <- RStack$new(0, 1, 2, collapse=list(3, 4))
# the following sentence is equivalent to the above
stack <- RStack$new(0, 1, 2, 3, 4)
# where the numbers 0, 1, 2, 3, 4 are pushed into the stack

### push elements

# it can be one single element
stack$push(5)
# it can be several elements separated by commas
# note the whole list will be one element of the stack
# because it is not passed through the collapse argument
stack$push(list(a=10,b=20), "Hello world!")
# the collapse argument takes a list whose elements will be collapsed
# but the elements' names will not be saved
stack$push(collapse = list(x=100,y=200))
# they can be used together
stack$push("hurrah", collapse = list("RStack",300))

### pop an element

# pop only one element at a time
val <- stack$pop()
# then we keep poping!
while(!is.null(val)) val <- stack$pop()

# }
Documentation reproduced from package R6DS, version 1.1.0, License: GPL-3

Community examples

Looks like there are no examples yet.