数据结构篇(2) ts实现单链表

interface NodeItem {
    prev: NodeItem | null
    next: NodeItem | null
    data: any
}

class NodeItem {
    prev: NodeItem | null = null
    next: NodeItem | null = null
    constructor(data: any) {
        this.data = data ? data : null;
    }
}

interface ListNode {
    head: any
    size: number
    currentNode: NodeItem | null
    find(item:any):NodeItem | null  // 在单链表中寻找item元素
    insert(element:any,item:any):void  // 向单链表中插入元素
    remove(item:any):void // 在单链表中删除一个节点
    append(element:any):void // 在单链表的尾部添加元素
    findLast():NodeItem  // 获取单链表的最后一个节点
    isEmpty():boolean // 判断单链表是否为空
    show():void  // 显示当前节点
    getLength():number  // 获取单链表的长度
    advance(n:number, currNode:NodeItem):void  // 从当前节点向前移动n个位置
    display():void // 单链表的遍历显示
    clear():void  // 清空单链表
}

class ListNode {
    head: any = null
    size: number = 0
    currentNode: NodeItem | null = null
    constructor() {
        this.head = new NodeItem('head');
    }

    find(item:any):NodeItem | null {
        let currNode = this.head;
        while(currNode) {
            if(currNode.data === item) {
                return currNode;
            }
            currNode = currNode.next;
        }
        return null;
    }
    insert(element:any,item:any):any {
        if(!this.find(element)) {
            return;
        }
        let prevNode = this.find(element);
        if(prevNode == null) {
            return ;
        }
        let currNode = new NodeItem(item);
        currNode.next = prevNode.next;
        prevNode.next = currNode;
        this.size++;
    }  
    remove(item:any):void {
        if(!this.find(item)) {
            return ;
        }
        if(item === 'head') {
            if(!this.isEmpty()) {
                return ;
            } else {
                this.head.next = null;
                return;
            }
        }
        let currNode = this.head;
        while(currNode) {
            if(currNode&&currNode.next.data === item) {
                break;
            }
            currNode = currNode.next;
        }
        currNode.next = currNode.next.next;
        this.size--;
    }
    append(element:any):void {
        let rear = this.findLast();
        let currentNode = new NodeItem(element);
        rear.next = currentNode;
        this.size++;
    }
    findLast():NodeItem {
        let currNode:NodeItem = this.head;
        while(currNode.next) {
            currNode = currNode.next;
        }
        return currNode;
    }
    isEmpty():boolean {
        if(this.head.next) {
            return true;
        }
        return false;
    }
    show():void {
        console.log(this.currentNode?.data);
    }
    getLength():number {
        let i = 0;
        let currNode = this.head;
        while(currNode) {
            i++;
            currNode = currNode.next;
        }
        return i;
    }
    advance(n:number, currNode:NodeItem = this.head):NodeItem {
        this.currentNode = currNode;
        while((n--)&&this.currentNode.next) {
            this.currentNode = this.currentNode?.next;
        }
        return this.currentNode;
    }
    display():void {
        let currentNode = this.head;
        while(currentNode) {
            console.log(currentNode.data);
            currentNode = currentNode.next;
        }
    }
    clear():void {
        this.head.next = null;
        this.size = 0;
    }
}

let myList = new ListNode();
let arr = [3, 4, 5, 6, 7, 8, 9];

for(let i=0; i<arr.length; i++){
    myList.append(arr[i]);
}


myList.insert(3,2);

myList.display();
posted @ 2022-03-28 23:31  ajajaz  阅读(130)  评论(0编辑  收藏  举报