As i wish

[2019.07.07] 매일 프로그래밍(이중 연결 리스트) 본문

Algorithm

[2019.07.07] 매일 프로그래밍(이중 연결 리스트)

어면태 2019. 7. 8. 10:53
        이중 연결 리스트 (Doubly linked list)를 구현하시오.

        - add(value): 주어진 value를 리스트 처음 노드로 등록.
        - search(value): value 를 가진 노드를 리턴.
        - remove(node): 주어진 노드를 연결 리스트에서 제거.

 

<html>
  <body>
    <script>
      (function main() {
        // 이중 연결 리스트 (Doubly linked list)를 구현하시오.

        // add(value): 주어진 value를 리스트 처음 노드로 등록.
        // search(value): value 를 가진 노드를 리턴.
        // remove(node): 주어진 노드를 연결 리스트에서 제거.

        class Node {
          constructor(data) {
            this.data = data;
            this.next = null;
            this.prev = null;
          }
        }

        class DoublyLinkedList {
          constructor() {
            this.head = null;
            this.tail = null;
          }

          append(value) {
            let node = new Node(value);

            if (!this.head) {
              this.head = node;
              this.tail = node;
            } else {
              node.prev = this.tail;
              this.tail.next = node;

              this.tail = node;
            }
          }

          search(value) {
            let current = this.head;
            while(!!current) {
              if (current.data === value) {
                break;
              } else {
                current = current.next;
              }
            }

            return current;
          }

          remove(node) {
            let current = this.head;
            let nodeVal = node.data;

            while(!!current) {
              if (current.data === nodeVal) {
                let currentPrevNode = current.prev,
                  currentNextNode = current.next;

                if (current.data === this.head.data) {
                  currentNextNode.prev = currentPrevNode;
                  this.head = currentNextNode;
                } else if (current.data === this.tail.data) {
                  currentPrevNode.next = currentNextNode;
                  this.tail = currentPrevNode;
                } else {
                  currentPrevNode.next = currentNextNode;
                  currentNextNode.prev = currentPrevNode;
                }

                break;
              } else {
                current = current.next;
              }
            }
          }

          print() {
            let current = this.head;
            while(!!current) {
              console.log(current);
              current = current.next;
            }
          }
        };

        const doublyLinkedList = new DoublyLinkedList();

        doublyLinkedList.append('first');
        doublyLinkedList.append('second');
        doublyLinkedList.append('third');
        doublyLinkedList.append('fourth');

        let searched = doublyLinkedList.search('fourth');
        console.log('Searched: ', searched);

        doublyLinkedList.remove({data: 'fourth'});
        doublyLinkedList.print();
      })();
    </script>
  </body>
</html>
Comments