Queue definition data structures and constructors

The queue data structure

  1. The definition of the queue
    • Linear queue is a special table, only the delete operation is allowed in the head of the table, insert the linear data structure in the end of the list, this structure is called a queue; Further there FIFO which, after a backward Characteristics.
    • Speaking of linear structure, have to look at the logical structure data, logical structure data into a linear structure, a set of structures, and graphical tree structure, shown below, is a special stack linear table, a linear structure a.
  2. JavaScript => constructor data structure of the queue structure (node ​​environment)
/**
 * @description 数据结构之队列结构的构造函数
 */
module.exports = function Queue() {

    // 初始化队列仓库
    const queue = []

    // 入列
    this.enqueue = item => queue.push(item)

    // 出列
    this.dequeue = () => queue.shift()

    // 获取列头
    this.head = () => queue[0]

    // 获取列尾
    this.tail = () => queue[queue.length - 1]

    // 列的大小
    this.size = () => queue.length

    // 清空列
    this.clear = () => queue = []
}

Guess you like

Origin www.cnblogs.com/guojbing/p/10990814.html