-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
core.clj
49 lines (41 loc) · 920 Bytes
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(ns data-structures.stack.core)
(defn ->Stack
[length]
[length []])
(defn length
[stack]
(first stack))
(defn empty-stack?
[stack]
(let [[_ store] stack]
(= (count store) 0)))
(defn full-stack?
[stack]
(let [[length store] stack]
(= (count store) length)))
(defn push-in-stack
[stack val]
(assert (not (full-stack? stack)) "Stack is full")
(let [[length store] stack]
[length (conj store val)]))
(defn pop-from-stack
[stack]
(if (empty-stack? stack)
stack
(let [[length store] stack]
[length (pop store)])))
(defn peek-at-stack
[stack]
(when-not (empty-stack? stack)
(let [[_ store] stack]
(nth store (dec (count store))))))
(defn to-string
[stack]
(let [sb (StringBuilder.)]
(.append sb "[ ")
(let [[_ store] stack]
(doseq [el store]
(.append sb el)
(.append sb " ")))
(.append sb "]")
(.toString sb)))