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 cbe4a1e

Browse files
committed
update: implemented Queue using SLL
1 parent 63ac632 commit cbe4a1e

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed
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, Node } = 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+
const node = new Node(data, null);
11+
return this.addAtEnd(node);
412
}
513

6-
add(element) {
7-
// add element to the start of the data
8-
return this.data.unshift(element);
14+
dequeue() {
15+
constnode=this.removeFromBeginning();
16+
return node.data;
917
}
1018

1119
peek() {
12-
return this.data[this.data.length - 1];
20+
return this.getLast();
21+
}
22+
23+
size() {
24+
return this.length();
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+
getFirst() {
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;

0 commit comments

Comments
(0)

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