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 5bec3a5

Browse files
authored
Merge pull request #80 from knaxus/queue
Re-implementation of Queue
2 parents 01b9a6f + d48713c commit 5bec3a5

File tree

6 files changed

+109
-46
lines changed

6 files changed

+109
-46
lines changed

‎src/_DataStructures_/LinkedList/index.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ class LinkedList {
2222
}
2323

2424
addAtEnd(element) {
25+
const node = new Node(element, null);
26+
this.size += 1;
27+
2528
if (!this.head) {
26-
return this.addAtBeginning(element);
29+
this.head = node;
30+
this.tail = node;
31+
return node;
2732
}
28-
const node = new Node(element, null);
2933
this.tail.next = node;
3034
this.tail = node;
31-
this.size += 1;
3235
return node;
3336
}
3437

@@ -42,6 +45,7 @@ class LinkedList {
4245
const node = this.head;
4346
this.head = this.head.next;
4447
this.size -= 1;
48+
node.next = null;
4549
return node;
4650
}
4751

@@ -139,6 +143,7 @@ class LinkedList {
139143
const node = address;
140144
previous.next = address.next.next;
141145
this.size -= 1;
146+
node.next = null;
142147
return node;
143148
}
144149

‎src/_DataStructures_/Queue/Queue.test.js‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@ describe('Data Structure : Queue', () => {
1212
queue = new Queue();
1313
});
1414

15-
it('Should add() element to a queue', () => {
16-
queue.add(5);
17-
expect(queue.data).toEqual([5]);
15+
it('Should add element to a queue', () => {
16+
queue.enqueue(5);
17+
expect(queue.peek()).toEqual(5);
1818
});
1919

20-
it('Should remove() an element from the queue', () => {
21-
queue.add(2);
22-
queue.add(3);
20+
it('Should dequeue() an element from the queue', () => {
21+
queue.enqueue(2);
22+
queue.enqueue(3);
2323

24-
expect(queue.remove()).toEqual(2);
25-
expect(queue.data).toEqual([3]);
24+
expect(queue.dequeue()).toEqual(2);
25+
expect(queue.peek()).toEqual(3);
26+
expect(queue.length()).toEqual(1);
2627
});
2728

2829
describe('peek()', () => {
2930
beforeEach(() => {
30-
queue.add(2);
31-
queue.add(5);
31+
queue.enqueue(2);
32+
queue.enqueue(5);
3233
});
3334

3435
it('Should return the elemet to be removed using peek()', () => {
@@ -37,21 +38,21 @@ describe('Data Structure : Queue', () => {
3738

3839
it('Should not remove the element', () => {
3940
expect(queue.peek()).toEqual(2);
40-
expect(queue.remove()).toEqual(2);
41+
expect(queue.dequeue()).toEqual(2);
4142
});
4243
});
4344

4445
it('Should maintain the order of elements', () => {
4546
// first in first out
46-
queue.add(2);
47-
queue.add(1);
48-
queue.add(4);
49-
queue.add(3);
50-
51-
expect(queue.remove()).toEqual(2);
52-
expect(queue.remove()).toEqual(1);
53-
expect(queue.remove()).toEqual(4);
54-
expect(queue.remove()).toEqual(3);
47+
queue.enqueue(2);
48+
queue.enqueue(1);
49+
queue.enqueue(4);
50+
queue.enqueue(3);
51+
52+
expect(queue.dequeue()).toEqual(2);
53+
expect(queue.dequeue()).toEqual(1);
54+
expect(queue.dequeue()).toEqual(4);
55+
expect(queue.dequeue()).toEqual(3);
5556
});
5657
});
5758
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Queue {
2+
constructor() {
3+
this.data = [];
4+
}
5+
6+
add(element) {
7+
// add element to the start of the data
8+
return this.data.unshift(element);
9+
}
10+
11+
peek() {
12+
return this.data[this.data.length - 1];
13+
}
14+
15+
remove() {
16+
return this.data.pop();
17+
}
18+
}
19+
20+
module.exports = Queue;

‎src/_DataStructures_/Queue/index.js‎

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,57 @@
1-
class Queue {
1+
const { LinkedList: SinglyLinkedLists } = require('../LinkedList');
2+
3+
class Queue extends SinglyLinkedLists {
24
constructor() {
3-
this.data = [];
5+
super();
6+
this.NotAllowed = 'Not Allowed';
7+
}
8+
9+
enqueue(data) {
10+
return this.addAtEnd(data);
411
}
512

6-
add(element) {
7-
// add element to the start of the data
8-
return this.data.unshift(element);
13+
dequeue() {
14+
constnode=this.removeFromBeginning();
15+
return node ? node.data : node;
916
}
1017

1118
peek() {
12-
return this.data[this.data.length - 1];
19+
const node = this.getFirst();
20+
return node ? node.data : node;
21+
}
22+
23+
length() {
24+
return this.size;
25+
}
26+
27+
destroy() {
28+
this.delete();
29+
}
30+
31+
/** Override and throw error for other LL methods */
32+
addAtBeginning() {
33+
throw new Error(this.NotAllowed);
34+
}
35+
36+
addAt() {
37+
throw new Error(this.NotAllowed);
38+
}
39+
40+
removeFromEnd() {
41+
throw new Error(this.NotAllowed);
42+
}
43+
44+
getLast() {
45+
throw new Error(this.NotAllowed);
46+
}
47+
48+
getAt() {
49+
throw new Error(this.NotAllowed);
1350
}
1451

15-
remove() {
16-
returnthis.data.pop();
52+
removeAt() {
53+
thrownewError(this.NotAllowed);
1754
}
1855
}
1956

20-
module.exports = Queue;
57+
module.exports = Queue;

‎src/_DataStructures_/Queue/weave/index.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ function weaveQueues(first, second) {
44
const weaved = new Queue();
55

66
while (first.peek() || second.peek()) {
7-
if (first.peek()!==undefined) {
8-
weaved.add(first.remove());
7+
if (first.peek()) {
8+
weaved.enqueue(first.dequeue());
99
}
1010

11-
if (second.peek()!==undefined) {
12-
weaved.add(second.remove());
11+
if (second.peek()) {
12+
weaved.enqueue(second.dequeue());
1313
}
1414
}
1515
return weaved;

‎src/_DataStructures_/Queue/weave/weave.test.js‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ describe('Weave two queues using weaveQueues()', () => {
1010
const q1 = new Queue();
1111
const q2 = new Queue();
1212

13-
q1.add('Hello');
14-
q2.add(1);
15-
q1.add('World');
16-
q2.add(2);
17-
q2.add(3);
13+
q1.enqueue('Hello');
14+
q2.enqueue(1);
15+
q1.enqueue('World');
16+
q2.enqueue(2);
17+
q2.enqueue(3);
1818

1919
const q3 = weaveQueues(q1, q2);
2020

21-
expect(q3.remove()).toEqual('Hello');
22-
expect(q3.remove()).toEqual(1);
23-
expect(q3.remove()).toEqual('World');
24-
expect(q3.remove()).toEqual(2);
25-
expect(q3.remove()).toEqual(3);
21+
expect(q3.dequeue()).toEqual('Hello');
22+
expect(q3.dequeue()).toEqual(1);
23+
expect(q3.dequeue()).toEqual('World');
24+
expect(q3.dequeue()).toEqual(2);
25+
expect(q3.dequeue()).toEqual(3);
2626
});
2727
});

0 commit comments

Comments
(0)

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