Skip to content

Commit 56acb9b

Browse files
committed
Adds immutable queue impl using vector
1 parent dbc80b0 commit 56acb9b

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/data_structures/queue/vector.clj

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(ns data-structures.queue.vector)
2+
3+
(defn ->Queue
4+
[]
5+
[])
6+
7+
(defn enqueue
8+
[queue value]
9+
(conj queue value))
10+
11+
(defn dequeue
12+
[queue]
13+
(if (empty? queue)
14+
queue
15+
(subvec queue 1)))
16+
17+
(defn peek-queue
18+
[queue]
19+
(first queue))
20+
21+
(defn empty-queue?
22+
[queue]
23+
(empty? queue))
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(ns data-structures.queue.vector-test
2+
(:require
3+
[clojure.test :refer [deftest testing is]]
4+
[data-structures.queue.vector :as q]))
5+
6+
(deftest ->Queue-test
7+
(is (vector? (q/->Queue))))
8+
9+
(deftest enqueue-test
10+
(is (= [10 20 30] (-> (q/->Queue) (q/enqueue 10) (q/enqueue 20) (q/enqueue 30)))))
11+
12+
(deftest dequeue-test
13+
(is (= [] (-> (q/->Queue) (q/dequeue))))
14+
(is (= [20 30] (-> (q/->Queue) (q/enqueue 10) (q/enqueue 20) (q/enqueue 30) (q/dequeue)))))
15+
16+
(deftest peek-queue-test
17+
(is (nil? (-> (q/->Queue) (q/peek-queue))) "returns nil for empty queue")
18+
(is (= 10 (-> (q/->Queue) (q/enqueue 10) (q/enqueue 20) (q/enqueue 30) (q/peek-queue)))))
19+
20+
(deftest empty-queue?-test
21+
(is (q/empty-queue? (q/->Queue)))
22+
(is (false? (-> (q/->Queue) (q/enqueue 10) (q/enqueue 20) q/empty-queue?))))

0 commit comments

Comments
 (0)