R/insert_back.rpqueue.R

Defines functions insert_back.rpqueue

Documented in insert_back.rpqueue

#' @export
#' @title Insert an element into the back of an rpqueue
#' 
#' @description Returns a version of the queue with the new element in the back position.
#' 
#' @details Runs in \eqn{O(1)} time worst-case. Does not modify the original. 
#' @param x rpqueue to insert onto.
#' @param e element to insert.
#' @param ... additional arguments to be passed to or from methods (ignored).
#' @return modified version of the rpqueue.
#' @references Okasaki, Chris. Purely Functional Data Structures. Cambridge University Press, 1999.
#' @examples
#' q <- rpqueue()
#' q <- insert_back(q, "a")
#' q <- insert_back(q, "b")
#' print(q)
#' 
#' q2 <- insert_back(q, "c")
#' print(q2)
#' print(q)
insert_back.rpqueue <- function(x, e, ...) {
  newq <- rpqueue()
  newq$l <- x$l
  newq$r <- insert_top(x$r, e)
  newq$lhat <- x$lhat
  return(makeequal(newq))
}

Try the rstackdeque package in your browser

Any scripts or data that you put into this service are public.

rstackdeque documentation built on May 2, 2019, 4:15 a.m.