Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 04b306e

Browse files
Queue ⚡
1 parent eaba737 commit 04b306e

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

‎examples/datastructures/queue.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Queue {
2+
constructor() {
3+
this.dataStore = [];
4+
}
5+
6+
enqueue(element) {
7+
this.dataStore.push(element);
8+
}
9+
10+
dequeue() {
11+
return this.dataStore.shift();
12+
}
13+
14+
clear() {
15+
this.dataStore = [];
16+
}
17+
18+
front() {
19+
return this.dataStore[0];
20+
}
21+
22+
back() {
23+
return this.dataStore[this.dataStore.length - 1];
24+
}
25+
26+
toString() {
27+
const str = "";
28+
for (let i = 0; i < this.dataStore.length; i++)
29+
str += this.dataStore[i] + " ";
30+
return str;
31+
}
32+
33+
empty() {
34+
return this.dataStore.length == 0;
35+
}
36+
37+
peek() {
38+
return this.dataStore[0];
39+
}
40+
41+
length() {
42+
return this.dataStore.length;
43+
}
44+
}
45+
46+
module.exports = Queue;

‎test/datastructures/queue.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const expect = require('chai').expect;
2+
const Queue = require('../../examples/datastructures/queue');
3+
4+
describe('=> QUEUE', function() {
5+
let numbers;
6+
before(function () {
7+
numbers = new Queue();
8+
});
9+
10+
it('should create an Empty Queue', function(done) {
11+
expect(numbers.dataStore).to.deep.equal([]);
12+
done();
13+
});
14+
15+
it('should enqueue elements into the Queue', function(done) {
16+
numbers.enqueue(1);
17+
numbers.enqueue(2);
18+
numbers.enqueue(3);
19+
numbers.enqueue(4);
20+
expect(numbers.dataStore).to.deep.equal([1,2,3,4]);
21+
done();
22+
});
23+
24+
it('should peek the Queue', function(done) {
25+
expect(numbers.peek()).to.deep.equal(1);
26+
done();
27+
});
28+
29+
it('should check if the Queue is empty', function(done) {
30+
expect(numbers.empty()).to.equal(false);
31+
done();
32+
});
33+
34+
it('should check the length of the Queue', function(done) {
35+
expect(numbers.length()).to.equal(4);
36+
done();
37+
});
38+
39+
describe('=> dequeue', function() {
40+
41+
it('should dequeue 1 element of the Queue', function(done) {
42+
numbers.dequeue();
43+
expect(numbers.dataStore).to.deep.equal([2,3,4]);
44+
done();
45+
});
46+
47+
it('should dequeue all elements of the Queue', function(done) {
48+
numbers.dequeue();
49+
numbers.dequeue();
50+
numbers.dequeue();
51+
expect(numbers.dataStore).to.deep.equal([]);
52+
done();
53+
});
54+
55+
});
56+
57+
58+
describe('=> clear', function() {
59+
it('should clear the Queue', function(done) {
60+
numbers.clear();
61+
expect(numbers.dataStore).to.deep.equal([]);
62+
done();
63+
});
64+
});
65+
66+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /