Spaces:
Sleeping
Sleeping
| /** | |
| * Copyright (c) 2017~2019, OBCon Inc. | |
| * All rights reserved. | |
| */ | |
| /** | |
| * @file | |
| * @copyright 2017~2019, OBCon Inc. | |
| * @author gye hyun james kim [pnuskgh@gmail.com] | |
| */ | |
| class Node { | |
| constructor(data) { | |
| this.prev = null; | |
| this.data = data; | |
| this.next = null; | |
| } | |
| } | |
| class LinkedList { | |
| constructor(isSingle=true) { | |
| this.size = 0; | |
| this.head = null; | |
| this.tail = null; | |
| this.isSingle = isSingle; | |
| } | |
| isEmpty() { | |
| return this.size == 0; | |
| } | |
| insert(item) { | |
| if (this.isSingle) { | |
| this._insert_single(item); | |
| } else { | |
| this._insert_double(item); | |
| } | |
| } | |
| _insert_single(item) { //--- Head에 데이터를 추가 한다. | |
| if (this.head == null) { | |
| this.head = new Node(item); | |
| } else { | |
| let tmp = this.head; | |
| this.head = new Node(item); | |
| this.head.next = tmp; | |
| } | |
| this.size = this.size + 1; | |
| } | |
| _insert_double(item) { //--- Head에 데이터를 추가 한다. | |
| if (this.head == null) { | |
| this.head = new Node(item); | |
| this.tail = this.head; | |
| } else { | |
| let tmp = this.head; | |
| this.head = new Node(item); | |
| this.head.next = tmp; | |
| tmp.prev = this.head; | |
| } | |
| this.size = this.size + 1; | |
| } | |
| insertAtTail() { | |
| if (this.tail == null) { | |
| this.tail = new Node(item); | |
| this.head = this.tail; | |
| } else { | |
| let tmp = this.tail; | |
| this.tail = new Node(item); | |
| this.tail.prev = tmp; | |
| tmp.next = this.tail; | |
| } | |
| this.size = this.size + 1; | |
| } | |
| remove(item) { | |
| if (this.isSingle) { | |
| this._remove_single(item); | |
| } else { | |
| this._remove_double(item); | |
| } | |
| } | |
| _remove_single(item) { //--- 값으로 데이터를 삭제 한다. | |
| let nodePrev = null; | |
| let nodeCurrent = this.head; | |
| for (let idx = 0; idx < this.size; idx++) { | |
| if (nodeCurrent.data == item) { | |
| if (idx == 0) { | |
| this.head = nodeCurrent.next; | |
| } else { | |
| nodePrev.next = nodeCurrent.next; | |
| } | |
| this.size = this.size - 1; | |
| break; | |
| } | |
| nodePrev = nodeCurrent; | |
| nodeCurrent = nodeCurrent.next; | |
| } | |
| } | |
| _remove_double(item) { | |
| } | |
| deleteAtHead() { | |
| if (this.isSingle) { | |
| return this._deleteAtHead_single(); | |
| } else { | |
| return this._deleteAtHead_double(); | |
| } | |
| } | |
| _deleteAtHead_single() { | |
| let rtval = null; | |
| if (this.head != null) { | |
| rtval = this.head.data; | |
| this.head = this.head.next; | |
| this.size = this.size - 1; | |
| } | |
| return rtval; | |
| } | |
| _deleteAtHead_double() { | |
| let rtval = null; | |
| if (this.head != null) { | |
| rtval = this.head.data; | |
| if (this.head == this.tail) { | |
| this.head = null; | |
| this.tail = null; | |
| } else { | |
| this.head = this.head.next; | |
| this.head.prev = null; | |
| } | |
| this.size = this.size - 1; | |
| } | |
| return rtval; | |
| } | |
| deleteAtTail() { | |
| let rtval = null; | |
| if (this.tail != null) { | |
| rtval = this.head.data; | |
| if (this.head == this.tail) { | |
| this.head = null; | |
| this.tail = null; | |
| } else { | |
| this.tail = this.tail.prev; | |
| this.tail.next = null; | |
| } | |
| this.size = this.size - 1; | |
| } | |
| return rtval; | |
| } | |
| find(item) { | |
| let rtFlag = false; | |
| let nodeCurrent = this.head; | |
| for (let idx = 0; idx < this.size; idx++) { | |
| if (nodeCurrent.data == item) { | |
| rtFlag = true; | |
| break; | |
| } | |
| nodeCurrent = nodeCurrent.next; | |
| } | |
| return rtFlag; | |
| } | |
| } | |
| module.exports = LinkedList; | |