[2020. 10. 20 TIL] Data Structure - Stack, Queue
9941 ワード
Stackの定義
A LIFO data structure!
The last element added to the stack will be the first element removed from the stack
Property
Method
class Stack {
constructor() {
this.storage = {};
this.top = 0;
}
size() {
return this.top;
}
push(element) {
this.storage[this.top] = element;
this.top++;
}
pop() {
let topVal = this.storage[this.top-1];
delete this.storage[this.top-1];
if(this.top > 0){
this.top--;
}
return topVal;
}
}
Queueの定義
A FIFO data structure
First In First Out
先入先出
How do we use them in programming?
JavascriptによるQueueの実装
class Queue {
constructor() {
this.storage = {};
this.front = 0;
this.rear = 0;
}
size() {
return this.rear;
}
enqueue(element) {
this.storage[this.rear] = element;
this.rear++;
}
dequeue() {
let frontVal = this.storage[this.front];
delete this.storage[this.front];
for(let i=1 ; i<this.rear ; i++){
let curr = this.storage[i];
this.storage[i-1] = curr;
}
if(this.rear > 0){
this.rear--;
}
delete this.storage[this.rear];
return frontVal;
}
}
Reference
この問題について([2020. 10. 20 TIL] Data Structure - Stack, Queue), 我々は、より多くの情報をここで見つけました https://velog.io/@young_mason/2020.-10.-20-TIL-Data-Structure-Stack-Queueテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol