Skip to content

Instantly share code, notes, and snippets.

@russellsimpkins
Created June 13, 2024 17:49
Show Gist options
  • Save russellsimpkins/260198e09a14f391cca55b275d709bf3 to your computer and use it in GitHub Desktop.
Save russellsimpkins/260198e09a14f391cca55b275d709bf3 to your computer and use it in GitHub Desktop.
Apps Script Queue Implementation
/**
* @class
* Class representing an Item in a Queue
*
* @author russellsimpkins@gmail.com
*/
class Item {
constructor(v) {
this.v = v;
this.n = null;
}
val() {
return this.v;
}
setNext(i) {
this.n = i;
}
next() {
return this.n;
}
}
/**
* @class
* Class representing a Queue
*
* This is a basic linked list impelemetion for a FIFO queue.
*
* @author russellsimpkins@gmail.com
*/
class Queue {
constructor(item) {
if (undefined == item) {
this.items = 0;
this.start = null;
this.end = null;
this.item = null;
return;
}
this.items = 1;
this.item = item;
this.start = item;
this.end = item;
}
add(item) {
if (this.items == 0) {
this.items = 1;
this.item = item;
this.start = item;
this.end = item;
} else {
this.items++;
let tail = this.end;
tail.setNext(item);
this.end = item;
}
}
pop() {
let item = this.start;
if (null != item.next()) {
this.start = item.next();
}
this.items--;
return item;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment