1. 程式人生 > 實用技巧 >JS 實現資料結構

JS 實現資料結構

使用JS實現資料結構。

1.棧

棧作為簡單的資料結構,JS對其實現的方法也相對簡單。

程式碼:

class Stack {
  constructor() {
    this.stack = [];
  }
  push(item) {
    this.stack.push(item);
  }
  pop() {
    this.stack.pop();
  }
  peek() {
    return this.stack[this.getCount() - 1];
  }
  getCount() {
    return this.stack.length;
  }
  clear() {
    
this.stack = []; } isEmpty() { return this.getCount() === 0; } getStack() { console.log(this.stack); } }

示例程式碼:

function Stacked() {
    let stack = new Stack();
    stack.push(1);
    stack.push(2);
    stack.push(3);
    stack.push(4);
    stack.push(5);
    stack.push(6);
    stack.getStack();
    stack.pop();
    console.log(stack.peek());
}

佇列

佇列有著先進先出的特點,在使用JS來實現單佇列是很簡單的一件事。

程式碼:

class Queue {
  constructor() {
    this.queue = [];
  }
  enQueue(item) {
    this.queue.push(item);
  }
  deQueue() {
    return this.queue.shift();
  }
  getHeader() {
    return this.queue[0];
  }
  getLength() {
    return this.queue.length;
  }
  clear() {
    
this.queue = []; } isEmpty() { return this.getLength() === 0; } getQueue() { console.log(this.queue); } }

有一種佇列,每一個值都帶有自己的優先順序,這種佇列成為優先順序佇列。

程式碼:

class PriorityQueue {
  constructor() {
    this.queue = [];
  }

  // 向佇列新增元素(一個或多個)
  // 引數obj的資料格式:{element, priority}
  enQueue(obj) {
    if (obj instanceof Array) {
      for (let i = 0, data; (data = obj[i]); i++) {
        this.enQueue(data);
      }
    } else {
      let added = false;
      for (let i = 0, data; (data = this.queue[i]); i++) {
        // 最小優先順序,即將priority值小的元素插入到佇列的前面
        if (obj.priority < data.priority) {
          this.queue.splice(i, 0, obj);
          added = true;
          break;
        }
      }

      // 如果元素沒有插入到佇列中,則預設加到佇列的尾部
      if (!added) this.queue.push(obj);
    }
  }

  deQueue() {
    return this.queue.shift();
  }

  getHeader() {
    return this.queue[0];
  }

  getLength() {
    return this.queue.length;
  }

  clear() {
    this.queue = [];
  }

  isEmpty() {
    return this.getLength() === 0;
  }

  getQueue() {
    this.queue.forEach(function (item) {
      console.log(`${item.element} - ${item.priority}`);
    });
  }
}

為充分利用向量空間,克服假溢位現象的方法是:將向量空間想象為一個首尾相接的圓環,並稱這種向量為迴圈向量。儲存在其中的佇列稱為迴圈佇列

程式碼:

class SqQueue {
  constructor() {
    this.queue = new Array();
    this.first = 0; // 隊頭
    this.last = 0;  // 隊尾
    this.size = 0;  // 實際大小
  }

  enQueue(item) {
    // 每當插入一個數據時,進行判斷,如果此時first和last相等且last與佇列長度相等時,將佇列伸長,保證足夠的空間填裝資料
    if (
      this.first === this.last % this.queue.length &&
      this.last >= this.getLength()
    ) {
      this.resize(this.getLength() + 1);
    }
    this.queue[this.last] = item;
    this.size++;
    this.last = this.last + 1;
  }

  deQueue() {
    if (this.isEmpity()) {
      throw Error("queue is empty");
    }
    let r = this.queue[this.first];
    this.queue.shift();
    this.size--;
    return r;
  }
  
  getHeader() {
    if (this.isEmpity()) {
      throw Error("queue is empty");
    }
    return this.queue[this.first];
  }

  getLength() {
    return this.queue.length;
  }

  isEmpity() {
    return this.first === this.last && this.getLength() === 0;
  }

  resize(length) {
    // 生成新陣列,代替原陣列。
    let q = new Array(length);
    for (let i = 0; i < length; i++) {
      q[i] = this.queue[(i + this.first) % this.getLength()];
    }
    this.queue = q;
    this.first = 0;
    this.last = this.size;
  }

  getSqQueue() {
    console.log(this.queue);
  }
}

上述程式碼可在我的GitHub上下載,歡迎大家指點批評。