Skip to content
关注微信公众号ouo.io - 縮短網址也可以賺錢

第1章 栈与队列

栈(基于数组)

js
class Stack {
    //定义——基于数组
    constructor() {
        this.items = [];
    }

    //1.将元素压入栈
    push(element) {
        this.items.push(element);
    }

    //2.从栈中取出元素
    pop() {
        return this.items.pop();
    }

    //3.查看栈顶元素
    peek() {
        return this.items[this.items.length - 1];
    }

    //4.判断栈是否为空
    isEmpty() {
        return this.items.length === 0;
    }

    //5.获取栈中元素的个数
    size() {
        return this.items.length;
    }

    //6.toString方法
    toString() {
        let resultString = '';
        this.items.forEach(ele => {
            resultString += ele + ' ';
        })
        return resultString;
    }

    //7.清空栈
    clear() {
        this.items = [];
    }
}

栈(基于对象)

js
class Stack {
    //定义——基于对象
    constructor() {
        this.count = 0;
        this.items = {};
    }

    //1.将元素压入栈
    push(element) {
        this.items[this.count] = element;
        this.count++;
    }

    //2.从栈中取出元素
    pop() {
        if (this.isEmpty()) {
            return undefined;
        }
        this.count--;
        const result = this.items[this.count];
        delete this.items[this.count];
        return result;
    }

    //3.查看栈顶元素
    peek() {
        if (this.isEmpty()) {
            return undefined;
        }
        return this.items[this.count - 1];
    }

    //4.判断栈是否为空
    isEmpty() {
        return this.count === 0;
    }

    //5.获取栈中元素的个数
    size() {
        return this.count;
    }

    //6.toString方法
    toString() {
        if (this.isEmpty()){
            return '';
        }
        let objString = `${this.items[0]}`;
        for (let i = 1;i <this.count;i++){
            objString = `${objString},${this.items[i]}`;
        }
        return objString;
    }

    //7.清空栈
    clear() {
        this.items = {};
        this.count = 0;
    }
}

队列(基于数组)

js
class Queue {
    //定义——基于数组
    constructor() {
        this.items = [];
    }

    //1.将元素加入到队列中
    enqueue(element) {
        this.items.push(element);
    }

    //2.从队列中删除前端元素
    dequeue() {
        return this.items.shift();
    }

    //3.查看前端的元素
    front() {
        return this.items[0];
    }

    //4.查看队列是否为空
    isEmpty() {
        return this.items.length === 0;
    }

    //5.查看队列中元素的个数
    size() {
        return this.items.length;
    }

    //6.toString方法
    toString() {
        let resultString = '';
        this.items.forEach(ele => {
            resultString += ele + ' ';
        })
        return resultString;
    }
}

队列(基于对象)

js
class Queue {
    //定义——基于对象
    constructor() {
        this.count = 0;
        this.lowestCount = 0;
        this.items = {};
    }

    //1.将元素加入到队列中
    enqueue(element) {
        this.items[this.count] = element;
        this.count++;
    }

    //2.从队列中删除前端元素
    dequeue() {
       if(this.isEmpty()) {
           return undefined;
       }
       const result = this.items[this.lowestCount];
       delete this.items[this.lowestCount];
       this.lowestCount++;
       return result;
    }

    //3.查看前端的元素
    front() {
        if(this.isEmpty()) {
            return undefined;
        }
        return this.items[this.lowestCount];
    }

    //4.查看队列是否为空
    isEmpty() {
        return this.count - this.lowestCount === 0;
    }

    //5.查看队列中元素的个数
    size() {
        return this.count - this.lowestCount;
    }

    //6.toString方法
    toString() {
        if (this.isEmpty()){
            return '';
        }
        let objString = `${this.items[this.lowestCount]}`;
        for (let i = this.lowestCount + 1;i <this.count;i++){
            objString = `${objString},${this.items[i]}`;
        }
        return objString;
    }
}

双端队列

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

    //1.在双端队列前端添加新的元素
    addFront(element) {
        if (this.isEmpty()) {
            this.addBack(element);
        } else if (this.lowestCount > 0) {
            this.lowestCount--;
            this.items[this.lowestCount] = element;
        } else {
            for (let i = this.count; i > 0; i--) {
                this.items[i] = this.items[i - 1];
            }
            this.count++;
            this.lowestCount = 0;
            this.items[0] = element;
        }
    }

    //2.在双端队列后端添加新的元素
    addBack(element) {
        //实现方法和Queue类中的enqueue方法相同
    }

    //3.从双端队列前端移除第一个元素
    removeFront() {
        //实现方法和Queue类中的dequeue方法相同
    }

    //4.从双端队列后端移除第一个元素
    removeBack() {
        //实现方法和Stack类中的pop方法一样
    }

    //5.该方法返回双端队列前端的第一个元素
    peekFront() {
        //实现方法和Queue类中的peek方法一样 
    }

    //6.该方法返回双端队列后端的第一个元素
    peekBack() {
        //实现方法和Stack类中的peek方法一样
    }
}
ouo.io - 縮短網址也可以賺錢
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0

预览:

评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3