단일 연결 리스트

gyujae·2022년 10월 6일
0
post-custom-banner
class Node {
    constructor(val) {
        this.val = val;
        this.next = null;
    }
}

class SinglyLinkedList {
    constructor() {
        this.head = null;
        this.tail = null;
        this.length = 0;
    }

    push(val) {
        var newNode = new Node(val);
        if(!this.head) {
            this.head = newNode;
        }else {
            this.tail.next = newNode;
        }
        this.tail = newNode;
        this.length += 1;
        return this;
    }

    pop() {
        if(!this.head) return undefined;
        var current = this.head;
        var newTail = current;
        while(current.next) {
            newTail = current;
            current = current.next;
        }
        this.tail = newTail;
        this.tail.next = null;
        this.length -= 1;
        if(this.length === 0) {
            this.head = null;
            this.tail = null;
        }
        return current;
    }

    shift() {
        if(!this.head) return undefined;
        var currentHead = this.head;
        this.head = currentHead.next;
        this.length--;
        if(this.length === 0) {
            this.tail = null;
        }
        return currentHead;
    }

    unshift(val) {
        var newNode = new Node(val);
        if(!this.head) {
            this.head = newNode;
            this.tail = this.head;
        }else {
            newNode.next = this.head;
            this.head = newNode;            
        }
        this.length++;
        return this;
    }

    get(index) {
        if(index < 0 || index >= this.length) return null;
        var currentIdx = 0;
        var currentNode = this.head;
        while(index !== currentIdx) {
            currentNode = currentNode.next;
            currentIdx++;
        }

        return currentNode;
    } 

    set(index, val) {
        var selectNode = this.get(index);
        if(!selectNode) return false;
        selectNode.val = val;
        return true;
    }

    insert(index, val) {
        if(index < 0 || index > this.length) return false;
        if(index === 0) {
            this.unshift(val);
            return true;
        }
        
        if(index + 1 === this.length) {
            this.push(val);
            return true;
        }

        var foundNode = this.get(index - 1);
        if(!foundNode) return false;
        var newNode = new Node(val);

        newNode.next = foundNode.next;
        foundNode.next = newNode;

        this.length++;

        return true;
    }

    remove(index) {
        if(index < 0 || index >= this.length) return undefined;
        if(index === this.length - 1) return this.pop();
        if(index === 0) return this.shift();
        var prevNode = this.get(index - 1);
        var removedNode = prevNode.next;
        prevNode.next = removedNode.next;
        this.length--;
        return removedNode.val;
    }

    reverse() {
        var node = this.head;
        this.head = this.tail;
        this.tail = node;
        var next;
        var prev = null;
        for(var i = 0; i < this.length; i++) {
            next = node.next;
            node.next = prev;
            prev = node;
            node = next;
        }
        return this;
    }
}

var list = new SinglyLinkedList();
list.push('Hello');
list.push('Good bye');
list.push("!");
list.push("<3");
list.push(":)");
post-custom-banner

0개의 댓글