2021년 1월 24일 (TIL Linked List)

Ji Taek Lim·2021년 1월 23일
0

오늘은 유투브를 보았다.
조금 개념을 잡기가 힘들었다.

MOUNT DEV
https://www.youtube.com/watch?v=ZBdE8DElQQU

const n1 = {
  data: 100,
};

const n2 = {
  data: 200,
};

n1.next = n2;

console.log(n1);

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

class LinkedList {
  constructor() {
    this.head = null;
    this.size = 0;
  }
  ///Insert first node

  insertFirst(data) {
    this.head = new Node(data, this.head);
    this.size++;
  }
  /// Insert last node
  insertLast(data) {
    let node = new Node(data);
    let current;

    // if empty. make head
    if (!this.head) {
      this.head = node;
    } else {
      current = this.head;

      while (current.next) {
        current = current.next;
      }

      current.next = node;
    }
    this.size++;
  }

  //If first index
  insertAt(data, index) {
    //If index is out of range
    if (index > 0 && index > this.size) {
      return;
    }
    // If first index
    if (index === 0) {
      this.head = new Node(data, this.head);
      return;
    }
    const node = new Node(data);
    let current, previous;

    // Set current to first
    current = this.head;
    let count = 0;

    while (count < index) {
      previous = current; // Node before index
      count++;
      current = current.next; /// Node after index
    }

    node.next = current;
    previous.next = node;

    this.size++;
  }

  // Get at index
  getAt(index) {
    let current = this.head;
    let count = 0;

    while (current) {
      if (count === index) {
        console.log(current.data);
      }
      count++;
      current = current.next;
    }
    return null;
  }

  // Remove at index
  removeAt(index) {
    if(index >0 && index > this.size) {
      return;
    }
    let current = this.head;
    let previous;
    let count = 0;

    //Remove first
    if(index ===0) {
      this.head = current.next;
    }else {
      while(count < index) {
        count++;
        previous = current;
        current = current.next;
      }

      previous.next = current.next;
    }

    this.size--;
  }

  // Clear list
  clearList() {
    this.head = null;
    this.size = 0;
  }
  // Print list data
  printListData() {
    let current = this.head;
    while (current) {
      console.log(current.data);
      current = current.next;
    }
  }
}

const ll = new LinkedList();

ll.insertFirst(100);
console.log(ll);
ll.insertFirst(200);
console.log(ll);
ll.insertFirst(300);
console.log(ll);
ll.insertLast(400);
console.log(ll);
ll.insertAt(500, 1);

console.log(ll);


ll.removeAt(2)
// ll.printListData();

console.log(ll)
ll.getAt();


{ data: 100, next: { data: 200 } }
LinkedList { head: Node { data: 100, next: null }, size: 1 }
LinkedList {
  head: Node { data: 200, next: Node { data: 100, next: null } },
  size: 2
}
LinkedList {
  head: Node { data: 300, next: Node { data: 200, next: [Node] } },
  size: 3
}
LinkedList {
  head: Node { data: 300, next: Node { data: 200, next: [Node] } },
  size: 4
}
LinkedList {
  head: Node { data: 300, next: Node { data: 500, next: [Node] } },
  size: 5
}
200

profile
임지택입니다.

0개의 댓글

관련 채용 정보