theprimeagen / fem-algos Goto Github PK
View Code? Open in Web Editor NEWFrontEnd Master algorithms!
Home Page: https://theprimeagen.github.io/fem-algos
FrontEnd Master algorithms!
Home Page: https://theprimeagen.github.io/fem-algos
First of all, this is a great course (still working through it...).
One thing that really tripped me off is the Queue.enque()
method:
type Node<T> = {
value: T;
next?: Node<T>;
}
export default class Queue<T> {
public length: number;
private head?: Node<T>;
private tail?: Node<T>;
constructor() {
this.head = this.tail = undefined;
this.length = 0;
}
enqueue(item: T): void {
const newNode = {value: item} as Node<T>;
this.length++;
if (!this.tail) {
this.tail = this.head = newNode;
return;
}
this.tail.next = newNode;
this.tail = newNode;
}
deque(): T | undefined {
if (!this.head) {
return undefined;
}
this.length--;
const head = this.head;
this.head = this.head.next;
if (this.length === 0) {
this.tail = undefined;
}
return head.value;
}
peek(): T | undefined {
return this.head?.value;
}
}
The confusing part is these two lines:
this.tail.next = newNode;
this.tail = newNode;
This doesn't have the effect as explained in the video lecture (first point the element at this.tail.next
to the new node and then set this.tail
to the same node). The second line - this.tail = newNode
- has as the effect that this.tail.next
is always undefined (upon returning from the method).
But in one very special case, when this.tail
and this.head
point to the same Node
object (the line: this.tail = this.head = newNode;
in the if-condition) it does affect the this.head.next
.
I still have trouble comprehending this implementation w/o using a debugger. Has someone found a less obscrure implementation?
In your lesson on queues you mention that it is mind-boggling that deque is spelled this way, but as a method to remove an item from a queue, it actually should be spelled dequeue as you surmised.
Deque would be pronounced "deck" and refers to a double-ended queue data type.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.