Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
54
Task/Stack/R/stack.r
Normal file
54
Task/Stack/R/stack.r
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
library(proto)
|
||||
|
||||
stack <- proto(expr = {
|
||||
l <- list()
|
||||
empty <- function(.) length(.$l) == 0
|
||||
push <- function(., x)
|
||||
{
|
||||
.$l <- c(list(x), .$l)
|
||||
print(.$l)
|
||||
invisible()
|
||||
}
|
||||
pop <- function(.)
|
||||
{
|
||||
if(.$empty()) stop("can't pop from an empty list")
|
||||
.$l[[1]] <- NULL
|
||||
print(.$l)
|
||||
invisible()
|
||||
}
|
||||
})
|
||||
|
||||
stack$empty()
|
||||
# [1] TRUE
|
||||
stack$push(3)
|
||||
# [[1]]
|
||||
# [1] 3
|
||||
stack$push("abc")
|
||||
# [[1]]
|
||||
# [1] "abc"
|
||||
# [[2]]
|
||||
# [1] 3
|
||||
stack$push(matrix(1:6, nrow=2))
|
||||
# [[1]]
|
||||
# [,1] [,2] [,3]
|
||||
# [1,] 1 3 5
|
||||
# [2,] 2 4 6
|
||||
# [[2]]
|
||||
# [1] "abc"
|
||||
# [[3]]
|
||||
# [1] 3
|
||||
stack$empty()
|
||||
# [1] FALSE
|
||||
stack$pop()
|
||||
# [[1]]
|
||||
[1] "abc"
|
||||
# [[2]]
|
||||
# [1] 3
|
||||
stack$pop()
|
||||
# [[1]]
|
||||
# [1] 3
|
||||
stack$pop()
|
||||
# list()
|
||||
stack$pop()
|
||||
# Error in get("pop", env = stack, inherits = TRUE)(stack, ...) :
|
||||
# can't pop from an empty list
|
||||
Loading…
Add table
Add a link
Reference in a new issue