Skip to content

Commit c6c3520

Browse files
committed
separate queue example into own file
1 parent 2043769 commit c6c3520

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

queues/index.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,4 @@ function createQueue() {
2626
}
2727
}
2828

29-
30-
const q = createQueue()
31-
32-
q.enqueue('Make an egghead lesson')
33-
q.enqueue('Help others learn')
34-
q.enqueue('Be happy')
35-
36-
q.dequeue()
37-
q.dequeue()
38-
console.log(q.peek())
39-
4029
exports.createQueue = createQueue

queues/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "intro-to-data-structures-and-algorithms",
33
"version": "1.0.0",
44
"description": "An egghead course to introduce data structures and algorithms in JavaScript",
5-
"main": "index.js",
5+
"main": "queues.js",
66
"scripts": {
77
"test": "jest",
88
"test:watch": "jest --watch"

queues/queues.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function createQueue() {
2+
const queue = []
3+
4+
return {
5+
enqueue(x) {
6+
queue.unshift(x)
7+
},
8+
dequeue() {
9+
if (queue.length === 0) {
10+
return undefined
11+
}
12+
return queue.pop()
13+
},
14+
peek() {
15+
if (queue.length === 0) {
16+
return undefined
17+
}
18+
return queue[queue.length - 1]
19+
},
20+
get length() {
21+
return queue.length
22+
},
23+
isEmpty() {
24+
return queue.length === 0
25+
}
26+
}
27+
}
28+
29+
30+
const q = createQueue()
31+
32+
q.enqueue('Make an egghead lesson')
33+
q.enqueue('Help others learn')
34+
q.enqueue('Be happy')
35+
36+
q.dequeue()
37+
q.dequeue()
38+
console.log(q.peek())
39+
40+
exports.createQueue = createQueue

0 commit comments

Comments
 (0)