§ 2. The Data Structures

Losdoor onleft mladies, cue.

[Click.]

  1. Introduction to the Data Structures
  2. Constructor Function: List
  3. Constructor Function: Queue
  4. Constructor Function: Stack
  1. Introduction to the Data Structures

  2. 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]);
      };
    }
    
  3. 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;
    }
    
  4. 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;
    }