TypeScript實現數據結構(一)棧,隊列,鏈表

最近在學習typescript,就想着用typescript自己練習一些基本的數據結構,記錄一下,讀者有什麼想法和建議也可以交流一下。

class Stack<T>{
    private items = null;
    constructor() {
        this.items = new Array<T>();
    }
    push(data: T): void {
        this.items.push(data);
    }
    pop(): T {
        return this.items.pop();
    }
    top(): T {
        return this.items[this.items.length - 1];
    }
    isEmpty(): boolean {
        return this.items.length === 0;
    }
    size(): number {
        return this.items.length;
    }
    clear(): void {
        this.items = new Array<T>();
    }
    print(): void {
        console.log(this.items);
    }
}

隊列

class Queue<T>{
    private items = null;
    constructor() {
        this.items = new Array<T>();
    }
    enqueue(data: T): void {
        this.items.push(data);
    }
    dequeue(): T {
        return this.items.shift();
    }
    head(): T {
        return this.items[0];
    }
    size(): number {
        return this.items.length;
    }
    clear(): void {
        this.items = new Array<T>();
    }
    isEmpty(): boolean {
        return this.items.length === 0;
    }
    tail(): T {
        return this.items[this.items.length - 1];
    }
    print(): void {
        console.log(this.items);
    }
}


鏈表

class LinkNode<T>{
    public data: T;
    public next: LinkNode<T>;
    constructor(data: T) {
        this.data = data;
        this.next = null;
    }
}

class LinkList<T>{
    private head: LinkNode<T>;
    private tail: LinkNode<T>;
    private length: number;
    constructor(){
        this.head = null;
        this.tail = null;
        this.length = 0;
    }
    append(data: T): void {
        let newNode = new LinkNode<T>(data);
        if (this.head === null) {
            this.head = newNode;
            this.tail = newNode;
        } else {
            this.tail.next = newNode;
            this.tail = newNode.next;
        }
        this.length++;
    }
    print(): void{
        let cur = this.head;
        while(cur != null) {
            console.log(cur.data);
            cur = cur.next;
        }
    }
    insert(index: number, data: T): void {
        let newNode = new LinkNode<T>(data);
        if (index === 0) {
            // 頭結點
            newNode.next = this.head;
            this.head = newNode;
            this.length++;
        } else if (index > 0 && index < this.length) {
            // 中間節點
            let cur = this.head;
            for(let i = 1; i < index; i++) {
                cur = cur.next;
            }
            newNode.next = cur.next;
            cur.next = newNode;
            this.length++;
        } else if (index === this.length) {
            // 尾節點
            this.tail.next = newNode;
            this.tail = newNode;
            this.length++;
        }
    }
    remove(index) : void {
        if (index >= 0 && index < this.length) {
            // 合法範圍
            let cur = this.head;
        }
        
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章