js version data structure _03 cycle queues

js version data structure _03 cycle queues

EDITORIAL, although the code test in preparation have been achieved. But limited white level. There are too ill-considered

In this post you will learn:

  • Circular queue
  • Queue-related applications: palindrome check

1. The circulation of the queue

Here Insert Picture Description
The so-called circular queue, but the queue is a variant of the ordinary. It inclusive common queue configured so as to connect the logical ring structure, because the size of the queue is fixed can be prevented more simply dummy overflow occurs.

Let's look to achieve it (still the same, chap private member variables lesson, or write the main idea)
the following functions:

  • isEmpty sentenced empty
  • isFull full sentence
  • put (e) the tail plug
  • poll () team head to take

Sentenced this circular queue full condition where I used to: it also inside a vacant position. That would have put the maximum position is set up five I put it 4 that is full. The main purpose is to prevent and sentenced waited in vain conditions

class SqQueue {
            constructor() {
                this.front = 0;
                this.rear = 0;
                this.items = {};
                this.MAX = 5; //这里我们人为规定此循环队列的长度为5
            }

            // 向队尾新加元素
            put(e) {
                // 先判满
                if (this.isFull()) {
                    return "full";
                } else {
                    this.items[this.rear] = e;
                    this.rear = (this.rear + 1) % this.MAX;//因为是个环所以不允许它有大于等于5的存在。环的指针下标在0-4.poll中同理
                }
            }
            // 从对头取出元素
            poll() {
                    // 先判空
                    if (this.isEmpty()) {
                        console.log(11);
                        return undefined;
                    } else {
                        const res = this.items[this.front];
                        delete this.items[this.front];
                        this.front = (this.front + 1) % this.MAX;
                        return res;
                    }
                }
                // 判空
            isEmpty() {
                    return this.front === this.rear;
                }
                // 判满
            isFull() {
                // 采用的是此循环队列仅空余一个为满
                return this.front == ((this.rear + 1) % this.MAX);
            }
        }

//部分测试代码:
        let q = new SqQueue();
        q.put(1);
        q.put(2);
        q.put(3);
        q.put(4);
        console.log(q.poll());
        console.log(q.poll());
        console.log(q.poll());
        q.put(2);
        q.put(3);
        q.put(4);
        console.log(q.poll());
        console.log(q.poll());
        console.log(q.poll());
        console.log(q.poll());
        q.put(4);
        console.log(q);

Application: Palindrome Checker

What is a palindrome: palindrome is a positive and negative can read through the word, phrase, number or a series of sequences of characters, such as madam or racecar.
There are many checks palindrome algorithm is probably the easiest is to look at the output of the palindrome string direction, and then make a comparison to the original string. Arguably the most simple is the stack of (not considered here use down cycle), but here to see the use of double-ended queue will be simpler than using the stack.
Implemented
on a class deque have, where they directly use the
class deque


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

            size() {
                // 你要搞清楚有元素时这个this.count是指向的队尾的后一个,放的时候可是先放,再让它加的1
                return this.count - this.lowesCount;
            }
            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;
                }
            }
            isEmpty() {
                // 什么时候为空呢,这里希望你有写c的数据结构的基础,为空时队头队尾指针指向同一地方
                return this.count === this.lowesCount;
            }
        }
        
       

Palindrome inspection methods to achieve:

 function anagrams(s) {
            // 对字符串做一些预处理
            if (s === undefined || s === null || (s !== null && s.length == 0)) {
                return false;
            }
            // 字母全变小写并去除空格,trim不可去除内部的
            s = s.toLocaleLowerCase().split(" ").join("");
            let q = new Queue();
            let flag = true;
            // 搞一个双端队列的实例
            // 回文入队
            for (let i = 0; i < s.length; i++) {
                q.addBack(s.charAt(i));
            }
            // 两端出队的同时进行比较
            while (q.size() > 1 && flag) {
                let l = q.removeBack();
                let r = q.removeFront();
                if (l !== r) {
                    flag = false;
                }
            }
            // q.size==1时肯定是个回文,就一个字符不用比较了就
            return flag;
        }
Published 68 original articles · won praise 82 · views 10000 +

Guess you like

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