Published
Edited
Jun 21, 2018
Insert cell
md`# Javascript Stack implementation using LinkedList and LinkedListNode`
Insert cell
LinkedListNode = function LinkedListNode(value) {
this.value = value;
this.next = null;
}
Insert cell
LinkedList = function LinkedList(){
this.head = null;
this.tail = null;
this.length = 0;
}
Insert cell
LinkedList.prototype.push = function push(pushed){
var head = this.head;
this.head = pushed;
this.head.next = head;
this.length += 1;
return {stack: this, pushed};
}
Insert cell
LinkedList.prototype.pop = function pop(){
if(this.length === 0) return;
var popped = this.head;
this.head = popped.next;
this.length -= 1;
popped.next = null;
return { stack: this, popped };
}
Insert cell
stack = new LinkedList
Insert cell
stack.push(new LinkedListNode(1))
Insert cell
stack.push(new LinkedListNode(2))
Insert cell
stack.pop()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more