js version queue data structure _02

js version queue data structure _02

The code is tested, but white Bai inevitably inadequate

In this post you can learn

  • What is the queue
  • Deque

Queue and stack is very similar, but the stack is characterized by last-out, and a FIFO queue

What is a queue

Here Insert Picture Description
An ordered set of items is to follow FIFO queue (FIFO) principle, its operation may be performed at both ends. Namely: the tail forward, head out

EDITORIAL: Not perfect, oh, like this private member variables modeled on the first section. After all, we must first learn ideas

Creating this class queue of it

        class Queue{
            constructor(){
                this.count=0;//用于记录队列内的元素个数
                this.lowesCount=0;//用于追踪对头元素
                this.items={};//用于存储队列元素的数据结构,当然也可以使用数组,但是使用对象获取元素更加高效
            }
        }

The method we want to achieve:

  • enqueue (e) to add elements to the tail
  • dequeue () away from a head element
  • peek () returns the first element in the queue, namely: Back head element
  • isEmpty () sentenced empty
  • size () Returns the number of elements in the queue
  • clear () Clear

Sentenced empty
ps: the role of the pointer is pointing to a piece of memory elements that we need, do not stick pointer in the structure c. It is a convenient pick up our stuff stuff

isEmpty() {
                // 什么时候为空呢,这里希望你有写c的数据结构的基础,为空时队头队尾指针指向同一地方
                return this.count === this.lowesCount;
            }

The team into the team:

			enqueue(e) {
                this.items[this.count] = e;
                this.count++
            }
            dequeue() {
                // 先判非空
                if (this.isEmpty()) {
                    return undefined;
                } else {
                    const result = this.items[this.lowesCount];
                    delete this.items[this.lowesCount];
                    // 指针后移
                    this.lowesCount++;
                    return result;
                }
            }

The other three :

      	  peek() {
                // 先判空
                if (this.isEmpty()) {
                    return undefined;
                } else {
                    return this.count[this.lowesCount];
                }

            }
            size(){
                // 你要搞清楚有元素时这个this.count是指向的队尾的后一个,放的时候可是先放,再让它加的1
                return this.count-this.lowesCount;
            }
            clear(){
                this.items={};
                this.count=0;
                this.lowesCount=0;
            }

Two pairs of side queue

Here Insert Picture Description
What is a double-ended queue it: that we also want to add an element to the team head, the tail out of the elements.
Take chestnuts give it life:
while waiting in line to buy something, then head of the team of people buying forgot to take something, you can return to the position of head of the team to pick up. The tail of the same people so impatient, you can also leave

In computer science, a common application is to store the deque series of undo. Whenever the user in the software into
the line operation, the operation is the presence of a double-ended queue (as in a stack in). When the user clicks the Undo button,
the operation will be ejected from the deque, indicating that it has been removed from the back. After performing a certain number of pre-defined operation,
the operation will be performed first removed from the front end deque. Since the deque while respecting the original FIFO and LIFO
A data we can say that it is the combination of the queue and the stack structure

Practical operation

Creating this class

class Deque { 
 constructor() { 
 this.count = 0; 
 this.lowestCount = 0; 
 this.items = {}; 
 } 
}

Similarly, we want to achieve the following functions:

  • isEmpty
  • clear
  • size
  • addFront (e) the first additive element teams
  • addBack (e) the tail additive element
  • removeFront team head out of the elements
  • removeBack the tail out of the elements
  • peekFront take the head elements
  • the tail elements take peekBack

Implementation code (primary code) :

Team head out into the tail, we have written above. Just a change of name on the line

            addFront(e) {
                if (this.isEmpty()) {
                    this.addBack(e)
                } else {
                    // 搞清楚现在谁指着队头呢
                    this.lowesCount--;
                    this.items[this.lowesCount] = e;
                }

            }
            addBack(e) {
                this.items[this.count] = e;
                this.count++
            }
            removeFront() {
                // 先判非空
                if (this.isEmpty()) {
                    return undefined;
                } else {
                    const result = this.items[this.lowesCount];
                    delete this.items[this.lowesCount];
                    // 指针后移
                    this.lowesCount++;
                    return result;
                }
            }
            removeBack() {
                if (this.isEmpty()) {
                    return undefined;
                } else {
                    // 谁指向队尾呢,this.count指向的是队尾的下一个
                    let result = this.items[this.count - 1];
                    delete this.items[this.count - 1];
                    this.count--;
                    return result;
                }
Published 68 original articles · won praise 82 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41086511/article/details/105299707