Priority queue: Difference between revisions
R: return key and value
(R: return key and value) |
|||
Line 3,546:
keys <- values <- NULL
insert <- function(key, value) {
keys <<- append(keys, key,
values <<- append(values, value,
}
pop <- function() {
head <- list(key=keys[1],value=values[[1]])
values <<- values[-1]
keys <<- keys[-1]
Line 3,567:
pq$insert(2, "Tax return")
while(!pq$empty()) {
}</lang>With output:<lang R>
setRefClass("PriorityQueue",
fields = list(keys = "numeric", values = "list"),
methods = list(
insert = function(key,value) {
insert.order <- findInterval(key, keys)
keys <<- append(keys, key, insert.order)
values <<- append(values, value, insert.order)
},
pop = function() {
head <- list(key=keys[1],value=values[[1]])
keys <<- keys[-1]
values <<- values[-1]
return(head)
},
empty = function() length(keys) == 0
))</lang>The only change in the example would be in the instantiation:<lang R>pq <- PriorityQueue$new()</lang>.
=={{header|Racket}}==
|