Reverse an rstack

Share:

Description

Returns a reversed version of an rstack, where the old last element (generally inaccessible) is now the top (and thus now accessible).

Usage

1
2
## S3 method for class 'rstack'
rev(x)

Arguments

x

rstack to reverse.

Details

This method runs in O(N) in the size of the rstack, though it works behind-the-scenes for efficiency by converting the input stack to a list, reversing the list, and building the result as a new rstack. The original is thus left alone, preserving O(1) amortized time for the original (assuming the "cost" of reversing is charged to the newly created stack) at the cost of additional memory usage. But, if the stack is not being used in a preserved manner, e.g. s <- rev(s), the garbage collector will be free to clean up the original data if it is no longer usable.

Value

a reversed version of the rstack.

See Also

as.list.rstack for converting an rstack to a list.

Examples

1
2
3
4
5
6
7
8
s <- rstack()
s <- insert_top(s, "a")
s <- insert_top(s, "b")
s <- insert_top(s, "c")

r <- rev(s)
print(r)
print(s)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.