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 944f4c6

Browse files
committed
update: re-implementation of Queue, fix in tests
1 parent b53f544 commit 944f4c6

File tree

2 files changed

+35
-46
lines changed

2 files changed

+35
-46
lines changed

β€Žsrc/_DataStructures_/Queue/Queue.test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,5 @@ describe('Data Structure : Queue', () => {
6969
queue.destroy();
7070
expect(queue.length()).toEqual(0);
7171
});
72-
73-
it('Override and throw error for other LL methods', () => {
74-
expect(() => { queue.addAtBeginning(); }).toThrowError('Not Allowed');
75-
expect(() => { queue.addAt(); }).toThrowError('Not Allowed');
76-
expect(() => { queue.removeFromEnd(); }).toThrowError('Not Allowed');
77-
expect(() => { queue.getLast(); }).toThrowError('Not Allowed');
78-
expect(() => { queue.getAt(); }).toThrowError('Not Allowed');
79-
expect(() => { queue.removeAt(); }).toThrowError('Not Allowed');
80-
});
8172
});
8273
});

β€Žsrc/_DataStructures_/Queue/index.js

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,54 @@
1-
const { LinkedList: SinglyLinkedLists } = require('../LinkedList');
1+
const { LinkedList: SLL } = require('../LinkedList');
22

3-
class Queue extendsSinglyLinkedLists{
3+
class Queue {
44
constructor() {
5-
super();
6-
this.NotAllowed = 'Not Allowed';
5+
this.data = this.getStorage();
76
}
87

9-
enqueue(data) {
10-
returnthis.addAtEnd(data);
8+
enqueue(element) {
9+
this.data.enqueue(element);
1110
}
1211

1312
dequeue() {
14-
const node = this.removeFromBeginning();
15-
return node ? node.data : node;
13+
return this.data.dequeue();
1614
}
1715

1816
peek() {
19-
const node = this.getFirst();
20-
return node ? node.data : node;
17+
return this.data.peek();
2118
}
2219

2320
length() {
24-
return this.size;
21+
return this.data.length();
2522
}
2623

2724
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);
50-
}
51-
52-
removeAt() {
53-
throw new Error(this.NotAllowed);
25+
return this.data.destroy();
26+
}
27+
28+
// eslint-disable-next-line class-methods-use-this
29+
getStorage() {
30+
// encapsulating the internal implementation here
31+
const storage = new SLL();
32+
33+
return {
34+
enqueue(element) {
35+
return storage.addAtEnd(element);
36+
},
37+
dequeue() {
38+
const node = storage.removeFromBeginning();
39+
return node ? node.data : node;
40+
},
41+
peek() {
42+
const node = storage.getFirst();
43+
return node ? node.data : node;
44+
},
45+
length() {
46+
return storage.size;
47+
},
48+
destroy() {
49+
storage.delete();
50+
},
51+
};
5452
}
5553
}
5654

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /