Learn R Programming

container (version 0.3.0)

dequeS3: Deque (double-ended queue) constructors

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, deque can also be used to mimic both stacks and simple queues.

Usage

deque(x = list())

as.deque(x)

is.deque(x)

addleft(x, ...)

count(x, ...)

peekleft(x)

popleft(x)

reverse(x)

rotate(x, ...)

Arguments

x

initial elements passed to constructor or object of class Deque passed to member methods.

...

further arguments

S3 methods for Deque objects

addleft(deq, elem)

Add elem to left side of the deq.

count(deq, elem)

Count number of elem occurences.

pop(deq)

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

popleft(deq)

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

peek(deq)

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

peekleft(deq)

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

reverse(deq)

Reverse all elements of the deq in-place.

rotate(deq, n=1L)

Rotate the deq 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, Deque, +.Deque

Examples

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

# count
count(deque(c("Lisa", "Bob", "Bob")), "Bob")     # 2

# peek and pop
d <- deque(1:3)
peek(d)                # 3
pop(d)                 # 3
pop(d)                 # 2
pop(d)                 # 1
# }
# NOT RUN {
d$pop()              # Error: pop at empty Deque
# }
# NOT RUN {
d <- deque(1:3)
print(d)
reverse(d)   # 3 2 1
print(d)

rotate(d)
values(d)                           # 1 3 2
values(rotate(d, -1))               # 3 2 1
values(rotate(d, 2))               # 2 1 3
# }

Run the code above in your browser using DataLab