Data와 다음 노드를 가리켜 링크를 형성할 수 있는 Next를 포함한 노드 모음이 연결리스트다.
이 중 단방향으로 흐르는 연결리스트를 단일 연결리스트라고 한다.
가장 첫번째 요소를 Head, 마지막 요소는 Tail이며 새로운 노드가 앞 뒤로 추가되는 기능은 Head와 Tail을 기능에 맞게 유동적으로 변경시켜주어 구현할 수 있다.
const log = console.log;
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(val) {
let newNode = new Node(val);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.length++;
return this;
}
pop() {
if (!this.head) return undefined;
let current = this.head;
let newTail = current;
while (current.next) {
newTail = current;
current = current.next;
}
this.tail = newTail;
this.tail.next = null;
this.length--;
if (this.length === 0) {
this.head = null;
this.tail = null;
}
return current;
}
shift() {
if (!this.head) return undefined;
let currentHead = this.head;
this.head = currentHead.next;
this.length--;
if (this.length === 0) {
this.tail = null;
}
return currentHead;
}
unshift(val) {
let newNode = new Node(val);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head = newNode;
}
this.length++;
return this;
}
get(index) {
if (index < 0 || index >= this.length) return null;
let counter = 0;
let current = this.head;
while (counter !== index) {
current = current.next;
counter++;
}
return current;
}
set(index, val) {
let foundNode = this.get(index);
if (foundNode) {
foundNode.val = val;
return true;
}
return false;
}
insert(index, val) {
if (index < 0 || index > this.length) return false;
if (index === this.length) return !!this.push(val);
if (index === 0) !!this.unshift(val);
let newNode = new Node(val);
let prev = this.get(index - 1);
[prev.next, newNode.next] = [newNode, prev.next];
this.length++;
return true;
}
remove(index) {
if (index < 0 || index >= this.length) return undefined;
if (index === 0) return this.shift();
if (index === this.length - 1) return this.pop();
let previousNode = this.get(index - 1);
let removed = previousNode.next;
previousNode.next = removed.next;
this.length--;
return removed;
}
revers() {
let node = this.head;
this.head = this.tail;
this.tail = node;
let next;
let prev = null;
for (let i = 0; i < this.length; i++) {
next = node.next;
node.next = prev;
prev = node;
node = next;
}
}
print() {
const arr = [];
let cur = this.head;
while (cur) {
arr.push(cur.val);
cur = cur.next;
}
console.log(arr);
}
}
let list = new SinglyLinkedList();
list.push(100);
list.push(201);
list.push(250);
list.push(350);
push - O(1), 나머지 O(N)
Array와 비교
단방향 연결 리스트는 스택 혹은 큐 자료 구조를 구현하기 위한 발판이다.