1. Code implementation

class Queue {
    constructor() {
        this.list = []
    }
    push(value) {
        this.list.push(value)
    }
    pop() {
        return this.list.shift()
    }
    peek() {
        return this.list[0]}isEmpty() {
        return this.list.length === 0
    }
    clear() {
        this.list = []
    }
    size() {
        return this.list.length
    }
    toString() {
        return this.list.join(",")}}Copy the code

2. Sample code (including test code)

Code link

3. The complexity

  • Team entry: the time and space complexity areO(1)
  • Outbound: The time complexity isO(N), the space complexity isO(1)
  • The queue will be realized based on the linked list, and the queue time complexity can be optimized asO(1)

4. Thoughts

  • First in, first out (FIFO)
  • First to come, eg when water is running in a pipe

5. Application scenarios

  • Synchronizing task queue
    • The preceding one is executed first, and then the next one
    • Code sample

6. Related articles

  • Es6 Implementation Stack Structure (including complexity)
  • Implementing priority queues (not updated)

7. Corrections are welcome