Skip to content

Commit b81b739

Browse files
authored
Merge pull request #15 from Code-by-practice/develop
Develop
2 parents 8f49437 + b0e893f commit b81b739

File tree

4 files changed

+185
-3
lines changed

4 files changed

+185
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- [x] Ch0 Array
66
- [x] Ch1 Stack
7-
- [ ] Ch2 Queue
7+
- [x] Ch2 Queue
88
- [ ] Ch3 Linked List
99
- [ ] Ch4 Sorting (Bubble, Insertion, merge, quick, selection)
1010
- [ ] Ch5 Tree

codes/Ch1_Stack/Eg1_stack.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ Stack.prototype.pop = function () {
3131
* @function print
3232
*/
3333
Stack.prototype.print = function () {
34-
console.log('Stack: ', this.stack.join(' '));
34+
console.log('Stack: ', this.stack.join(' '))
3535
}
3636

3737
/**
3838
* Get the peek of the stack
3939
* @function peek
4040
*/
4141
Stack.prototype.peek = function () {
42-
return this.stack[this.stack.length - 1];
42+
return this.stack[this.stack.length - 1]
4343
}
4444

4545
/**

codes/Ch2_Queue/Eg1_queue.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* Data Structure: Queue
3+
* ES5 version
4+
* Run `node Eg1_queue.js` and see the output
5+
*/
6+
function Queue () {
7+
this.queue = []
8+
}
9+
10+
/**
11+
* Push data into the queue
12+
* @params {number} data - Data to be pushed into the queue
13+
* @function enqueue
14+
*/
15+
Queue.prototype.enqueue = function (data) {
16+
console.log('Push %d into the queue', data)
17+
this.queue.push(data)
18+
}
19+
20+
/**
21+
* Shilf the queue
22+
* @function dequeue
23+
*/
24+
Queue.prototype.dequeue = function () {
25+
console.log('Dequeue the queue')
26+
return this.queue.shift()
27+
}
28+
29+
/**
30+
* Print the queue as string
31+
* @function print
32+
*/
33+
Queue.prototype.print = function () {
34+
console.log('Queue: ', this.queue.join(' '))
35+
}
36+
37+
/**
38+
* Get the peek of the queue
39+
* @function peek
40+
*/
41+
Queue.prototype.peek = function () {
42+
return this.queue[0]
43+
}
44+
45+
/**
46+
* Get the queue length
47+
* @function length
48+
*/
49+
Queue.prototype.length = function () {
50+
return this.queue.length
51+
}
52+
53+
/**
54+
* Create queue instance
55+
*/
56+
var queue = new Queue()
57+
58+
/**
59+
* Add few data into the queue
60+
*/
61+
queue.enqueue(5)
62+
queue.enqueue(10)
63+
queue.enqueue(15)
64+
queue.enqueue(20)
65+
66+
/**
67+
* Print the queue to verify data got added into the queue
68+
*/
69+
queue.print()
70+
71+
/**
72+
* Get queue length
73+
*/
74+
console.log('Current queue length is %d', queue.length())
75+
76+
/**
77+
* Get the peek of the queue
78+
*/
79+
console.log('%d is the queue peek: ', queue.peek())
80+
81+
/**
82+
* Pop data from the queue
83+
*/
84+
queue.dequeue()
85+
86+
/**
87+
* Again check the queue print
88+
*/
89+
queue.print()
90+

codes/Ch2_Queue/Eg1_queue_es6.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Data Structure: Queue
3+
* ES5 version
4+
* Run `node Eg1_queue_es6.js` and see the output
5+
*/
6+
class Queue {
7+
constructor () {
8+
this.queue = []
9+
}
10+
11+
/**
12+
* Push data into the queue
13+
* @params {number} data - Data to be pushed into the queue
14+
* @function enqueue
15+
*/
16+
enqueue (data) {
17+
console.log('Push %d into the queue', data)
18+
this.queue.push(data)
19+
}
20+
21+
/**
22+
* Shilf the queue
23+
* @function dequeue
24+
*/
25+
dequeue () {
26+
console.log('Dequeue the queue')
27+
return this.queue.shift()
28+
}
29+
30+
/**
31+
* Print the queue as string
32+
* @function print
33+
*/
34+
print () {
35+
console.log('Queue: ', this.queue.join(' '))
36+
}
37+
38+
/**
39+
* Get the peek of the queue
40+
* @function peek
41+
*/
42+
peek () {
43+
return this.queue[0]
44+
}
45+
46+
/**
47+
* Get the queue length
48+
* @function length
49+
*/
50+
length () {
51+
return this.queue.length
52+
}
53+
}
54+
55+
/**
56+
* Create queue instance
57+
*/
58+
var queue = new Queue()
59+
60+
/**
61+
* Add few data into the queue
62+
*/
63+
queue.enqueue(5)
64+
queue.enqueue(10)
65+
queue.enqueue(15)
66+
queue.enqueue(20)
67+
68+
/**
69+
* Print the queue to verify data got added into the queue
70+
*/
71+
queue.print()
72+
73+
/**
74+
* Get queue length
75+
*/
76+
console.log('Current queue length is %d', queue.length())
77+
78+
/**
79+
* Get the peek of the queue
80+
*/
81+
console.log('%d is the queue peek: ', queue.peek())
82+
83+
/**
84+
* Pop data from the queue
85+
*/
86+
queue.dequeue()
87+
88+
/**
89+
* Again check the queue print
90+
*/
91+
queue.print()
92+

0 commit comments

Comments
 (0)