2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,19 +1,18 @@
|
|||
PriorityQueue <- function() {
|
||||
keys <- values <- NULL
|
||||
insert <- function(key, value) {
|
||||
temp <- c(keys, key)
|
||||
ord <- order(temp)
|
||||
keys <<- temp[ord]
|
||||
values <<- c(values, list(value))[ord]
|
||||
ord <- findInterval(key, keys)
|
||||
keys <<- append(keys, key, ord)
|
||||
values <<- append(values, value, ord)
|
||||
}
|
||||
pop <- function() {
|
||||
head <- values[[1]]
|
||||
head <- list(key=keys[1],value=values[[1]])
|
||||
values <<- values[-1]
|
||||
keys <<- keys[-1]
|
||||
return(head)
|
||||
}
|
||||
empty <- function() length(keys) == 0
|
||||
list(insert = insert, pop = pop, empty = empty)
|
||||
environment()
|
||||
}
|
||||
|
||||
pq <- PriorityQueue()
|
||||
|
|
@ -23,5 +22,5 @@ pq$insert(5, "Make tea")
|
|||
pq$insert(1, "Solve RC tasks")
|
||||
pq$insert(2, "Tax return")
|
||||
while(!pq$empty()) {
|
||||
print(pq$pop())
|
||||
with(pq$pop(), cat(key,":",value,"\n"))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue