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 d3a74b3

Browse files
Merge branch 'master' into graphs
2 parents 0958895 + 729aabe commit d3a74b3

File tree

19 files changed

+485
-436
lines changed

19 files changed

+485
-436
lines changed

β€ŽREADME.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
![GitHub contributors](https://img.shields.io/github/contributors/knaxus/problem-solving-javascript)
1010
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/knaxus/problem-solving-javascript/issues)
1111
![GitHub](https://img.shields.io/github/license/knaxus/problem-solving-javascript)
12-
![Twitter Follow](https://img.shields.io/twitter/follow/ashokdey_?label=%40ashokdey_&style=social)
1312

1413
Collection of interview questions with Unit Tests. Problems includes Data Structures, Logical and few Classical problems.
1514

@@ -30,8 +29,8 @@ Find the detailed contents and problem list here: [Table Of Contents](TOC.md)
3029

3130
| Name | Twitter | LinkedIn | Website |
3231
| ----------------------------------------- | ------------------------------------------- | --------------------------------------------- | ------------------------------------------ |
33-
| [Ashok Dey](https://github.com/ashokdey) |[ashokdey\_](https://twitter.com/ashokdey_) | [Ashok Dey](https://linkedin.com/in/ashokdey) | [https://ashokdey.in](https://ashokdey.in)|
34-
| [Ashu Deshwal](https://github.com/TheSTL) | [\_TheSTL\_](https://twitter.com/_TheSTL_)| - | - |
32+
| [Ashok Dey](https://github.com/ashokdey) |![Twitter Follow](https://img.shields.io/twitter/follow/ashokdey_?label=%40ashokdey_&style=social) | [Ashok Dey](https://linkedin.com/in/ashokdey) | [https://ashokdey.in](https://ashokdey.in)|
33+
| [Ashu Deshwal](https://github.com/TheSTL) | ![Twitter Follow](https://img.shields.io/twitter/follow/_TheSTL_?label=%40_TheSTL&style=social)|[Ashu Deshwal](https://www.linkedin.com/in/ashu-deshwal/)| - |
3534

3635
[Detailed list of contributors](https://github.com/knaxus/problem-solving-javascript/graphs/contributors)
3736

β€Žpackage-lock.json

Lines changed: 57 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Ž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 γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /