반응형
    
    
    
  class Node {
  constructor(data, next = null) {
    this.data = data;
    this.next = next;
  }
}
class LinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
    this.size = 0;
  }
  insertFirst(data) {
    let newNode = new Node(data, null);
    if (this.isEmpty()) {
      this.head = newNode;
      this.tail = newNode;
      this.size++;
      return;
    }
    newNode.next = this.head;
    this.head = newNode;
    this.size++;
  }
  insertLast(data) {
    let newNode = new Node(data, null);
    if (this.isEmpty()) {
      this.head = newNode;
      this.tail = newNode;
      this.size++;
      return;
    }
    this.tail.next = newNode;
    this.tail = newNode;
    this.size++;
  }
  insertAt(idx, data) {
    let newNode = new Node(data, null);
    if (this.isEmpty()) {
      this.head = newNode;
      this.tail = newNode;
      this.size++;
      return;
    } else if (idx == 1) {
      this.insertFirst(data);
      return;
    }
    let cur = this.head;
    let count = 1;
    while (cur != null) {
      if (count + 1 == idx) {
        break;
      }
      cur = cur.next;
      count += 1;
    }
    newNode.next = cur.next;
    cur.next = newNode;
    this.size++;
  }
  search(data) {
    let idx = 1;
    let cur = this.head;
    while (cur != null) {
      if (cur.data == data) {
        console.log(`${idx}번째에 '${data}'가 있습니다.`);
        return;
      }
      cur = cur.next;
      idx++;
    }
    console.log(`'${data}'가 존재하지 않습니다.`);
  }
  removeFirst() {
    if (this.isEmpty()) {
      return;
    } else if (this.getSize() == 1) {
      this.head = null;
      this.tail = null;
      this.size--;
      return;
    }
    let cur = this.head;
    this.head = this.head.next;
    cur.next = null;
    this.size--;
  }
  removeLast() {
    if (this.isEmpty()) {
      return;
    } else if (this.getSize() == 1) {
      this.head = null;
      this.tail = null;
      this.size--;
      return;
    }
    let cur = this.head;
    while (cur != null) {
      if (cur.next == this.tail) {
        break;
      }
      cur = cur.next;
    }
    cur.next = null;
    this.tail = cur;
    this.size--;
  }
  remove(data) {
    if (this.isEmpty()) {
      return;
    }
    let cur = this.head;
    if (cur.data == data) {
      this.head = cur.next;
    } else {
      while (cur != null) {
        if (cur.next.data == data) {
          break;
        }
        cur = cur.next;
      }
      cur.next = cur.next.next;
    }
    this.size--;
  }
  getSize() {
    return this.size;
  }
  isEmpty() {
    return !this.size;
  }
  print() {
    let cur = this.head;
    console.log(`=== 크기 : ${this.getSize()} ===`);
    while (cur != null) {
      console.log(cur.data);
      cur = cur.next;
    }
  }
}
const linkedList = new LinkedList();
linkedList.insertFirst("화");
linkedList.print();
linkedList.insertFirst("월");
linkedList.print();
linkedList.insertLast("금");
linkedList.print();
linkedList.insertAt(3, "목");
linkedList.print();
linkedList.insertAt(3, "수");
linkedList.print();
linkedList.search("수");
linkedList.search("토");
linkedList.remove("월");
linkedList.print();
linkedList.remove("금");
linkedList.print();
linkedList.remove("수");
linkedList.print();
linkedList.remove("화");
linkedList.print();
linkedList.remove("목");
linkedList.print();
linkedList.removeFirst();
linkedList.print();
linkedList.removeFirst();
linkedList.print();
linkedList.removeLast();
linkedList.print();
linkedList.removeLast();
linkedList.print();
linkedList.removeLast();
linkedList.print();반응형
    
    
    
  'Basic > JavaScript' 카테고리의 다른 글
| FormData를 이용하여 json과 여러 파일을 같이 보내는 방법 (0) | 2023.07.03 | 
|---|---|
| Javascript 퀵 소트 코드 (0) | 2023.07.01 | 
| js로 버전 비교하는 방법 (0) | 2023.05.09 | 
| mocha test를 parrelel하게 할때의 장단점 (0) | 2023.05.08 | 
| "Unhandled Error : TypeError: Converting circular structure to JSON 에러 해결 (0) | 2023.05.07 |