Losdoor onleft mladies, cue.
[Click.]
- Introduction to the Data Structures
- Constructor Function: List
- Constructor Function: Queue
- Constructor Function: Stack
Introduction to the Data Structures
Constructor Function: List
function List() { let list = []; Object.defineProperty(this, 'length', { get: function () { return list.length; } }); this.elementAt = function (i) { return list[i]; }; this.addAtHead = function (e) { return list.unshift(e); }; this.addAtTail = function (e) { return list.push(e); }; this.removeAtHead = function () { return list.shift(); }; this.removeAtTail = function () { return list.pop(); }; this.clear = function () { const oldList = list; list = []; return oldList; }; this.forEach = function (f) { return list.forEach(f); }; this.forSome = function (f, lower, upper) { if (lower === undefined) lower = 0; if (upper === undefined) upper = list.length; for (let i = lower; i < upper; i++) f(list[i]); }; }
Constructor Function: Queue
function Queue() { const list = new List(); Object.defineProperty(this, 'length', { get: function () { return list.length; } }); this.add = list.addAtTail; this.remove = list.removeAtHead; this.clear = list.clear; }
Constructor Function: Stack
function Stack() { const list = new List(); Object.defineProperty(this, 'length', { get: function () { return list.length; } }); this.push = list.addAtHead; this.pop = list.removeAtHead; this.clear = list.clear; }