Data Structures Notion PDF
Data Structures Notion PDF
Stack:
function Stack () {
this.storage = {};
this.length = 0;
}
};
Stack.prototype.pop = function () {
if (this.length) {
const res = this.storage[this.length];
delete this.storage[this.length];
this.length--;
return res;
}
return null;
};
Stack.prototype.size = function () {
return this.length;
};
Queue:
function Queue () {
this.storage = {};
this.start = 1;
this.end = 1;
Queue.prototype.dequeue = function () {
if (this.size()) {
const res = this.storage[this.start];
delete this.storage[this.start];
this.start++;
return res;
}
return null;
};
Queue.prototype.size = function () {
return this.end - this.start;
};
Linked List:
LinkedList {
head: LinkedListNode { value: 'input', next: null },
tail: LinkedListNode { value: 'input', next: null }
LinkedList {
head: LinkedListNode {
value: 'input',
next: LinkedListNode { value: 'input2', next: null }
},
tail: LinkedListNode { value: 'input2', next: null }
}
LinkedList {
head: LinkedListNode { value: 'input2', next: null },
tail: LinkedListNode { value: 'input2', next: null }
}
function LinkedList () {
this.head = null;
this.tail = null;
LinkedList.prototype.removeHead = function () {
if (this.head) {
const prevHead = this.head;
if (prevHead.next) this.head = prevHead.next;
else this.head = this.tail = null;
return prevHead.value;
}
return null;
};
DoubleLinkedList {
head: LinkedListNode { value: 'input', next: null },
tail: LinkedListNode { value: 'input', next: null }
}
DoubleLinkedList {
head: <ref *1> LinkedListNode {
value: 'input',
next: LinkedListNode { value: 'input2', next: null, prev: [Circular *1] }
},
tail: <ref *2> LinkedListNode {
value: 'input2',
next: null,
prev: <ref *1> LinkedListNode {
value: 'input',
next: [Circular *2]
}
}
}
addToHead (value) {
const prevHead = this.head;
super.addToHead(value);
if (prevHead) prevHead.prev = this.head;
return true;
}
addToTail (value) {
const prevTail = this.tail;
super.addToTail(value);
if (prevTail) this.tail.prev = prevTail;
return true;
}
removeHead () {
const res = super.removeHead();
if (this.head && this.head.prev) this.head.prev = null;
return res;
}
removeTail () {
if (this.tail) {
const prevTail = this.tail;
if (prevTail.prev) {
this.tail = prevTail.prev;
this.tail.next = null;
} else this.tail = this.head = null;
return prevTail.value;
}
return null;
}
// REMOVE-END
}
Set {}
true
function Set () {}
Tree:
Tree {
value: 'input',
children: [ Tree { value: 'input2', children: [] } ]
}
A hash table has: size, storage, functions, and ‘used’ is to be used later for
resizing.
HashTable {
size: 10,
storage: { get: [Function: get], set: [Function: set] },
used: 0
}
HashTable {
size: 10,
storage: { get: [Function: get], set: [Function: set] },
used: 1
}
HashTable {
size: 10,
storage: { get: [Function: get], set: [Function: set] },
used: 2
}
input
this.size = size;
this.storage = Storage(size);
this.used = 0; // for self-resizing
HashTable.prototype._checkSize = function () {
const ratio = this.used / this.size;
const min = 10;
if (ratio > 0.75) {
this._resize(this.size * 2);
return 'resizing up';
}
if (this.size > min && ratio < 0.25) {
this._resize(Math.floor(this.size / 2));
return 'resizing down';
}
return true;
};
module.exports = HashTable;