-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathstacks-queues.js
123 lines (112 loc) · 2.65 KB
/
stacks-queues.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.tail = null;
// undefined === unintentional non-value
// null === intentional non-value
// Linked Lists may also have a tail pointer
}
appendToHead(data) {
const newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
if (this.tail === null) {
this.tail = newNode;
}
}
appendToTail(data) { // adding to the end is linear (unless you have a tail pointer)
const newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
} else {
this.tail.next = newNode;
}
this.tail = newNode;
}
removeFromHead() { // removing from the head is constant
if (this.head === null) {
return;
}
this.head = this.head.next // constant
if (this.head === null) {
this.tail = null;
}
}
removeFromTail() { // removing from the tail is ALWAYS linear
if (this.head === null) {
return;
}
let curr = this.head;
let prev = null;
if (curr.next) {
prev = curr;
curr = curr.next;
}
prev.next = null;
this.tail = prev;
}
}
class Stack {
constructor() {
this.values = [];
// ['a', 'b', 'c', 'd']
// bottom top
}
push(data) {
// adding a value to the top
// arr.length = new value
// this.values[this.values.length] = data;
this.values.push(data)
}
pop() {
// removing a value from the top
// this.values.length--;
this.values.pop();
}
peek() {
// return the top value
return this.values[this.values.length - 1]
}
}
let stack = new Stack();
console.log(stack.values);
stack.push('a')
stack.push('b')
stack.push('c')
console.log(stack.values);
console.log(stack.peek()); // c
stack.pop();
console.log(stack.peek()); // b
stack.pop();
console.log(stack.peek()); // a
stack.pop();
console.log(stack.peek()); // undefined
class Queue {
constructor() {
// this.values = [];
this.values = new LinkedList();
}
enqueue(data) { // add to the back of the array
// this.values.push(data);
this.values.appendToTail(data);
}
dequeue() { // remove from the front of the array
// return this.values.shift();
this.values.removeFromHead()
}
}
let q = new Queue();
q.enqueue("Ben");
q.enqueue("Maya");
q.enqueue("Reuben");
console.log(q);
q.dequeue();
q.dequeue();
q.dequeue();
console.log(q);