# 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