Learn R Programming

container (version 0.3.0)

Deque: Deque (double-ended queue)

Description

Deques are a generalization of stacks and queues typically with methods to add, remove and access elements at both sides of the underlying data sequence. As such, the Deque can also be used to mimic both stacks and queues.

Usage

Deque

Arguments

Format

An object of class R6ClassGenerator of length 24.

Inherited methods

Inherits all methods from Container class.

R6 constructor

Deque$new(x=list())

Deque methods

addleft(elem)

Add elem to left side of the Deque.

count(elem)

Count number of elem occurences.

pop()

Remove and return element from the right side of the Deque.

popleft()

Remove and return an element from the left side of the Deque.

peek()

Peek at last element on the right side without removing it.

peekleft()

Peek at first element on the left side without removing it.

reverse()

Reverse all elements of the Deque in-place.

rotate(n=1)

Rotate the Deque elements n steps to the right. If n is negative, rotate to the left.

Details

Inherits from Container and extends it by pop and peek methods, element counting, and reverse and rotate functionality.

See Also

Container

Examples

Run this code
# NOT RUN {
# addleft
d <- Deque$new(1L)$addleft(2)
d$values()                                          # 2 1
Deque$new(0L)$addleft(3:1)$values()                 # 3 2 1 0

# count
Deque$new(c("Lisa", "Bob", "Bob"))$count("Bob")     # 2

# peek and pop
d <- Deque$new(1:3)
d$peek()                # 3
d$pop()                 # 3
d$pop()                 # 2
d$pop()                 # 1
#' \dontrun{
#' d$pop()              # Error: pop at empty Deque
#' }

Deque$new(1:3)$reverse()$values()   # 3 2 1

Deque$new(1:3)$rotate()$values()    # 3 1 2
Deque$new(1:3)$rotate(2)$values()   # 2 3 1
Deque$new(1:3)$rotate(-1)$values()  # 2 3 1
# }

Run the code above in your browser using DataLab