Learn R Programming

R6DS (version 1.2.0)

RDeque: The RDeque reference class

Description

The RDeque reference class implements the data structure double-ended queue (deque).

Usage

RDeque

Arguments

Format

An object of class R6ClassGenerator of length 24.

Immutable Methods

The immutable methods do not change the instance.

peekleft()

This method returns the leftmost (front) element of the deque. It returns NULL if the deque is empty.

peek()

This method returns the rightmost (rear) element of the deque. It returns NULL if the deque is empty.

Mutable Methods

The mutable methods change the instance.

appendleft(..., collapse=NULL)

The appendleft method appends the elements in ... and collapse into the deque to the left (front).

Note that if you append elements in this order:

instance$appendleft(elem1, elem2, elem3)

The order of them inside the deque will be

elem3, elem2, elem1, ...

and elem3 will be the new front of the deque.

append(..., collapse=NULL)

The append method appends the elements in ... and collapse into the deque to the right (rear).

popleft()

The popleft method returns and removes the leftmost (front) element in the deque. It returns NULL if the deque is empty.

pop()

The pop method returns and removes the rightmost (rear) element in the deque. It returns NULL if the deque is empty.

Details

A deque is an ordered list of elements generalizing the queue data structure. One can append and pop (return and remove) elements from both sides (left and right, front and rear) of the deque.

The elements in the deque are not necessarily to be of the same type, and they can be any R objects.

References

For the details about the deque data structure, see Deque at Wikipedia.

See Also

RStack, RQueue, and R6DS for the introduction of the reference class and some common methods

Examples

Run this code
# NOT RUN {
### create a new instance

# to create a new instance of the class
deque <- RDeque$new()

# the previous RDeque instance will be removed if you run
deque <- RDeque$new(0, 1, 2, collapse=list(3, 4))
# the following sentence is equivalent to the above
deque <- RDeque$new(0, 1, 2, 3, 4)
# where the numbers 0, 1, 2, 3, 4 are enqueued into the deque

### append and appendleft

# it can be one single element
deque$append(5)
# it can be several elements separated by commas
# note the whole list will be one element of the deque
# because it is not passed through the collapse argument
deque$append(list(a=10,b=20), "Hello world!")
# the collapse argument takes a list whose elements will be collapsed
# but the elements' names will not be saved
deque$append(collapse = list(x=100,y=200))
# they can be used together
deque$append("hurrah", collapse = list("RDeque",300))

# this string will be the new head
deque$appendleft("a string")
# we can update the head by
deque$appendleft("string3","string2","string1")
# "string1" will be the leftmost

### peekleft and peek
deque$peekleft()
# "string1"
deque$peek()
# 300

### popleft and pop

val <- deque$popleft()
# "string1"
val <- deque$pop()
# 300

# then we keep dequeuing!
while(!is.null(val)) val <- deque$pop()

# }

Run the code above in your browser using DataLab