Skip to content

Instantly share code, notes, and snippets.

@peterkarn
Created May 29, 2022 06:52
Show Gist options
  • Save peterkarn/ed4a76f78a8db887d0c5e9e2002cd7b2 to your computer and use it in GitHub Desktop.
Save peterkarn/ed4a76f78a8db887d0c5e9e2002cd7b2 to your computer and use it in GitHub Desktop.
class Queue {
constructor() {
// Record the length of the queue , size
this.count = 0;
// Record the first element of the queue
this.lowestCount = 0;
// You can also use arrays here , But to get elements more efficiently , Use objects to store elements ,
// and Stack Very similar , It's just that the principle of adding and removing elements is different
this.items = {};
}
// Is it an empty queue
isEmpty() {
return this.count === 0;
}
// Number of queue elements
size() {
return this.count - this.lowestCount;
}
// Clear queue
clear() {
this.items = {};
this.count = 0;
this.lowestCount = 0;
}
// Add an item to the end of the queue
// The implementation method here and Stack The same way in the stack . take count As items The key in corresponds to the element as its value
enqueue(element) {
this.items[this.count] = element;
this.count++;
}
// Remove the first item , And return the removed element
dequeue() {
if (this.isEmpty()) {
return undefined;
}
const result = this.items[this.lowestCount];
// Delete the first element
delete this.items[this.lowestCount];
// The position of the first element added , Also move backward
this.lowestCount++;
return result;
}
// Returns the first element in the queue , First added , It will also be the first element to be removed , The queue doesn't change , Just value
peek() {
if (this.isEmpty()) {
return undefined;
}
return this.items[this.lowestCount];
}
toString() {
if (this.isEmpty()) {
return '';
}
let queueString = this.items[this.lowestCount];
for (let i = this.lowestCount + 1; i < this.count; i++) {
queueString += `,${this.items[i]}`;
}
return queueString;
}
}
class Deque {
constructor() {
this.count = 0;
this.lowestCount = 0;
this.items = {};
}
isEmpty() {
return this.count === 0;
}
// Add elements to the front end
addFront(element) {
if (this.isEmpty()) { // Empty queue
this.addBack(element);
} else {
this.lowestCount--;
this.items[this.lowestCount] = element;
}
}
// Add elements to the back end
addBack(element) {
this.items[this.count] = element;
this.count++;
}
// Remove... From the front end
removeFront() {
if (this.isEmpty()) {
return undefined;
}
const result = this.items[this.lowestCount];
// Delete the first element
delete this.items[this.lowestCount];
// The position of the first element added , Also move backward
this.lowestCount++;
return result;
}
// Remove... From the back end
removeBack() {
if (this.isEmpty()) {
return undefined;
}
this.count--;
const result = this.items[this.count];
delete this.items[this.count];
return result;
}
// Return the front most element
peekFront() {
if (this.isEmpty()) {
return undefined;
}
return this.items[this.lowestCount];
}
// Return the last element
peekBack() {
if (this.isEmpty()) {
return undefined;
}
return this.items[this.count - 1];
}
}
const dequeue = new Deque();
dequeue.addFront(1);
dequeue.addBack(2);
dequeue.addFront(5);
dequeue.addBack('x');
dequeue.removeBack();
console.log(dequeue);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment