Learn R Programming

rstackdeque (version 1.1.1)

rev.rstack: Reverse an rstack

Description

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

Usage

"rev"(x)

Arguments

x
rstack to reverse.

Value

a reversed version of the rstack.

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.

See Also

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

Examples

Run this code
s <- rstack()
s <- insert_top(s, "a")
s <- insert_top(s, "b")
s <- insert_top(s, "c")

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

Run the code above in your browser using DataLab