Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
26
Task/Priority-queue/R/priority-queue-1.r
Normal file
26
Task/Priority-queue/R/priority-queue-1.r
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
PriorityQueue <- function() {
|
||||
keys <- values <- NULL
|
||||
insert <- function(key, value) {
|
||||
ord <- findInterval(key, keys)
|
||||
keys <<- append(keys, key, ord)
|
||||
values <<- append(values, value, ord)
|
||||
}
|
||||
pop <- function() {
|
||||
head <- list(key=keys[1],value=values[[1]])
|
||||
values <<- values[-1]
|
||||
keys <<- keys[-1]
|
||||
return(head)
|
||||
}
|
||||
empty <- function() length(keys) == 0
|
||||
environment()
|
||||
}
|
||||
|
||||
pq <- PriorityQueue()
|
||||
pq$insert(3, "Clear drains")
|
||||
pq$insert(4, "Feed cat")
|
||||
pq$insert(5, "Make tea")
|
||||
pq$insert(1, "Solve RC tasks")
|
||||
pq$insert(2, "Tax return")
|
||||
while(!pq$empty()) {
|
||||
with(pq$pop(), cat(key,":",value,"\n"))
|
||||
}
|
||||
5
Task/Priority-queue/R/priority-queue-2.r
Normal file
5
Task/Priority-queue/R/priority-queue-2.r
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
1 : Solve RC tasks
|
||||
2 : Tax return
|
||||
3 : Clear drains
|
||||
4 : Feed cat
|
||||
5 : Make tea
|
||||
17
Task/Priority-queue/R/priority-queue-3.r
Normal file
17
Task/Priority-queue/R/priority-queue-3.r
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
PriorityQueue <-
|
||||
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
|
||||
))
|
||||
1
Task/Priority-queue/R/priority-queue-4.r
Normal file
1
Task/Priority-queue/R/priority-queue-4.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
pq <- PriorityQueue$new()
|
||||
Loading…
Add table
Add a link
Reference in a new issue