Repository navigation
Expand file tree
/
Copy pathqueue.js
More file actions
72 lines (61 loc) · 1.69 KB
/
Copy pathqueue.js
File metadata and controls
72 lines (61 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const { NotImplementedError } = require('../extensions/index.js');
const { ListNode } = require('../extensions/list-node.js');
// const { ListNode } = require('../extensions/list-node.js');
/**
* Implement the Queue with a given interface via linked list (use ListNode extension above).
*
* @example
* const queue = new Queue();
*
* queue.enqueue(1); // adds the element to the queue
* queue.enqueue(3); // adds the element to the queue
* queue.dequeue(); // returns the top element from queue and deletes it, returns 1
* queue.getUnderlyingList() // returns { value: 3, next: null }
*/
class LinkedList{
constructor(){
this.head = null;
this.tail = null;
}
//add new item in LinkedList in end
append(value){
const newItem = new ListNode(value);
// if head or tail not yet
if (!this.head || !this.tail){
this.head = newItem;
this.tail = newItem
}
//if this inited
this.tail.next = newItem;
this.tail = newItem;
}
//Load from Array
createFromArray(arr){
arr.forEach(value => this.append(value));
}
}
class Queue {
constructor(){
this.qList = [];
}
getUnderlyingList() {
const u = new LinkedList();
u.createFromArray(this.qList);
return u.head;
//throw new NotImplementedError('Not implemented');
// remove line with error and write your code here
}
enqueue(value) {
this.qList.push(value);
//throw new NotImplementedError('Not implemented');
// remove line with error and write your code here
}
dequeue() {
return this.qList.shift();
//throw new NotImplementedError('Not implemented');
// remove line with error and write your code here
}
}
module.exports = {
Queue
};