1월 6일 (월)

연결 리스트 만들기

  • 디자인을 더 정교하게 깊게 고민한 후에.. 다른 사람이 만든 것들을 검색하는 게 좋았을 것 같다

  • 단축키 더 알아보기

  • 처음 페어프로그래밍 해보니 vscode 안에서 터미널 사용하는 게 더 편리하다는 것도 알고. 여러모로 얻는게 많았음.

    const Node = function (id, titleIndex, maxNum) {
        this.id = id;
        this.title = "제목" + String(titleIndex + 1);
        this.time = Math.floor((Math.random() * maxNum + 1));
        this.next = null;
    }

    const LinkedList = function () {
        this.head = null;
        this.add = add;
    }

    function add(id, data) {
        let newNode = null;
        for (let i = 0; i < data.length; i++) {
            if (data[i].id === id) newNode = data[i];
        }
        var current = this.head;
        if (current === null) {
            this.head = newNode;
            return;
        }
        while (current.next != null) {
            current = current.next;
        }
        current.next = newNode;
    }

    function makeId(length) {
        const idArray = [];
        const alphabetArr = ['a', 'b', 'c', 'd', 'e', 'f']
        const strLength = 4;
        while (idArray.length < length) {
            let idString = '';
            checkId(strLength, alphabetArr, idString, idArray);
        }
        return idArray;
    }

    function checkId(strLength, alphabetArr, idString, idArray) {
        for (let i = 0; i < strLength; i++) {
            const randomIndex = Math.floor(Math.random() * alphabetArr.length);
            idString += alphabetArr[randomIndex];
            if (i === (strLength - 1) && idArray.indexOf(idString) === -1) {
                idArray.push(idString);
            }
        }
    }

    function init(length) {
        const titleIndex = makeId(length);
        const video = [];

        for (let i = 0; i < length; i++) {
            let temp = i;
            if (i === 0) temp = "head";
            video.push(new Node(titleIndex[i], i, 15, temp));
        }
        return video;
    }

    function printData(length, video) {
        console.log("----- 영상클립 -----");
        for (let i = 0; i < length; i++) {
            console.log(video[i].title + "(" + video[i].id + ")" + ":" + video[i].time);
        }
    }

    function printConsole(linkedList, data) {
        // Nodejs readline module
        const readline = require("readline");
        const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout
        });

        rl.setPrompt("> ");
        rl.prompt();
        rl.on("line", (text) => {
            const strArray = text.split(" "),
                cmd = strArray[0],
                id = strArray[1],
                index = strArray[2];

            switch (cmd) {
                case "add":
                    linkedList.add(id, data);
                    break;

                default:
                    break;
            }

            printLinkedList(linkedList);
            rl.prompt();
        });
    }

    function printLinkedList(linkedList) {
        let str = "";
        function isNull(node) {
            str += "[" + node.id + "," + node.time + "]";
            if (node.next === null) return;
            node = node.next;
            str += "---";
            isNull(node);
        }
        isNull(linkedList.head);
        str += "---[end]";
        console.log(str);
    }

    function main() {
        const length = 13,
            data = init(length),
            linkedList = new LinkedList();
        printData(length, data);
        printConsole(linkedList, data);
    }

    main();

Last updated