typescript 实现双向链表的队列和栈功能,通过数组实现队列功能,通过双链表实现O(1)复杂度的获取链表中最小值功能

//实现一个链表
class LinkNode<T> {
    constructor(public value: T, public next: LinkNode<T> | null = null
        , public prev: LinkNode<T> | null = null) {
    }
}
class LinkList<T> {
    public head: LinkNode<T> | null;
    public end: LinkNode<T> | null;
    public add(value: LinkNode<T>): void {
        if (this.head) {
            this.end.next = value;
            value.prev = this.end;
            this.end = value;
        } else {
            this.head = value;
            this.end = value;
        }
    }
    //实现取出头部的元素的方法
    public pop(): T | null {
        if (!this.head) return null;
        let value = this.head.value;
        this.head = this.head.next;
        this.head.prev = null;
        return value
    }

    //实现取出尾部元素的方法

    public shift(): T | null {
        if (!this.end) return null;
        let value = this.end.value;
        this.end = this.end.prev;
        this.end.next = null;
        return value;
    }

    // 实现头部插入的方法
    public unshift(value: LinkNode<T>) {
        if (this.head) {
            this.head.prev = value;
            value.next = this.head;
            this.head = value;
        } else {
            this.head = value;
        }
    }
}

/**
 * 再用数组实现一个队列,循环数组
 */

class Queue {
    public arr: number[];
    private headIndex: number = 0;
    private endIndex: number = 0;
    private size: number = 0;

    constructor(public len: number) {
        this.arr = new Array(len).fill(null);
    }

    // 实现添加元素的方法
    public push(value: number): void {
        if (this.size === this.len) {
            console.log('队列已满');
            return;
        }

        this.arr[this.endIndex] = value;
        this.endIndex = this.getNextIndex(this.endIndex);
    }

    //实现一个取出顶部元素的方法

    public pop(): number | null {
        if (this.size === 0) return null;
        let value = this.arr[this.headIndex];
        this.arr[this.headIndex] = null;
        this.headIndex = this.getNextIndex(this.headIndex);
        return value;
    }
    private getNextIndex(index: number): number {
        return (index + 1) % this.arr.length;
    }
}

//再实现一个可以取出最小值都是O(1)的栈和队列用链表实现
class MinStack<T> {
    public head: LinkNode<T> | null;
    public end: LinkNode<T> | null;

    public head1: LinkNode<T> | null;
    public end1: LinkNode<T> | null;

    public add(value: LinkNode<T>): void {
        if (this.head) {
            this.end.next = value;
            value.prev = this.end;
            this.end = value;
            //深拷贝value
            let dad = value.value > this.end1.value ? this.end1.value : value.value;
            let value1 = new LinkNode(dad);
            this.end1.next = value1;
            value1.prev = this.end1
        } else {
            this.head = value;
            this.end = value;
            this.head1 = value;
            this.end1 = value;
        }
    }

    public getMin(): T | null {
        if (!this.head1) return null;
        return this.end1.value;
    }
    //实现取出头部的元素的方法
    public pop(): T | null {
        if (!this.head) return null;
        let value = this.head.value;
        this.head = this.head.next;
        this.head.prev = null;
        this.head1 = this.head1.next;
        this.head1.prev = null;

        return value
    }

    //实现取出尾部元素的方法

    public shift(): T | null {
        if (!this.end) return null;
        let value = this.end.value;
        this.end = this.end.prev;
        this.end.next = null;
        this.end1 = this.end1.prev;
        this.end1.next = null;
        return value;
    }

    // 实现头部插入的方法
    public unshift(value: LinkNode<T>) {
        if (this.head) {
            this.head.prev = value;
            value.next = this.head;
            this.head = value;
            this.head1 = value;
        } else {
            this.head = value;
            this.head1 = value;
            this.end = value;
            this.end1 = value;

        }
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值