Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
42
Task/Stack/Nim/stack.nim
Normal file
42
Task/Stack/Nim/stack.nim
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
type Stack[T] = distinct seq[T]
|
||||
|
||||
func initStack[T](initialSize = 32): Stack[T] =
|
||||
Stack[T](newSeq[T](initialSize))
|
||||
|
||||
func isEmpty[T](stack: Stack[T]): bool =
|
||||
seq[T](stack).len == 0
|
||||
|
||||
func push[T](stack: var Stack[T]; item: sink T) =
|
||||
seq[T](stack).add(item)
|
||||
|
||||
func pop[T](stack: var Stack[T]): T =
|
||||
if stack.isEmpty:
|
||||
raise newException(IndexDefect, "stack is empty.")
|
||||
seq[T](stack).pop()
|
||||
|
||||
func top[T](stack: Stack[T]): T =
|
||||
if stack.isEmpty:
|
||||
raise newException(IndexDefect, "stack is empty.")
|
||||
seq[T](stack)[^1]
|
||||
|
||||
func mtop[T](stack: var Stack[T]): var T =
|
||||
if stack.isEmpty:
|
||||
raise newException(IndexDefect, "stack is empty.")
|
||||
seq[T](stack)[^1]
|
||||
|
||||
func `mtop=`[T](stack: var Stack[T]; value: T) =
|
||||
if stack.isEmpty:
|
||||
raise newException(IndexDefect, "stack is empty.")
|
||||
seq[T](stack)[^1] = value
|
||||
|
||||
when isMainModule:
|
||||
|
||||
var s = initStack[int]()
|
||||
s.push 2
|
||||
echo s.pop
|
||||
s.push 3
|
||||
echo s.top
|
||||
s.mtop += 1
|
||||
echo s.top
|
||||
s.mtop = 5
|
||||
echo s.top
|
||||
Loading…
Add table
Add a link
Reference in a new issue