단일 연결리스트
노드 class
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
push
class SinglyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(value) {
this.length += 1;
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
return this;
}
}
const list = new SinglyLinkedList();
list.push("1");
list.push("2");
list.push("3");
console.log(list.head);
console.log(list.head.next);
console.log(list.head.next.next);
pop
pop() {
if (!this.head) return undefined;
if (this.length === 1) {
const popNode = this.head;
this.head = null;
this.tail = null;
this.length--;
return popNode;
}
let check = this.head;
let newTail = this.head;
while (check.next) {
newTail = check;
check = check.next;
}
this.tail = newTail;
newTail.next = null;
this.length--;
return check;
}
shift
shift() {
if (!this.head) return undefined;
if (this.length === 1) {
const shiftNode = this.head;
this.head = null;
this.tail = null;
this.length--;
return shiftNode;
}
const shiftNode = this.head;
this.head = this.head.next;
this.length--;
return shiftNode;
}
unshift
unshift(value) {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head = newNode;
}
this.length++;
return this;
}
get
get(index) {
if (index < 0) return null;
if (!this.head) return null;
if (index > this.length - 1) return null;
let check = this.head;
for (let i = 0; i <= index; i++) {
if (i === index) return check;
check = check.next;
}
}
set
- 인덱스 위치의 노드를 주어진 value 값으로 업데이트하고 true를 리턴 target이 존재하지 않으면 false를 리턴
set(index, value) {
let targetNode = this.get(index);
if (!targetNode) return false;
targetNode.value = value;
return true;
}
insert
insert(index, value) {
const newNode = new Node(value);
if (index === 0) {
this.unshift(value);
return this;
}
const prevNode = this.get(index - 1);
const nextNode = this.get(index);
if (nextNode) {
newNode.next = nextNode;
prevNode.next = newNode;
this.length++;
return this;
}
if (prevNode) {
this.push(value);
return this;
}
return false;
}
remove
remove(index) {
if (index < 0 || index > this.length - 1) return false;
if (index === 0) {
this.shift();
return this;
}
if (index === this.length - 1) {
this.pop();
return this;
}
const prevNode = this.get(index - 1);
const targetNode = this.get(index);
prevNode.next = targetNode.next;
targetNode.next = null;
this.length--;
return this;
}
reverse
reverse() {
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;
}
return this;
}
전체 코드
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(value) {
this.length += 1;
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
return this;
}
pop() {
if (!this.head) return undefined;
if (this.length === 1) {
const popNode = this.head;
this.head = null;
this.tail = null;
this.length--;
return popNode;
}
let check = this.head;
let newTail = this.head;
while (check.next) {
newTail = check;
check = check.next;
}
this.tail = newTail;
newTail.next = null;
this.length--;
return check;
}
shift() {
if (!this.head) return undefined;
if (this.length === 1) {
const shiftNode = this.head;
this.head = null;
this.tail = null;
this.length--;
return shiftNode;
}
const shiftNode = this.head;
this.head = this.head.next;
this.length--;
return shiftNode;
}
unshift(value) {
const newNode = new Node(value);
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) return null;
if (!this.head) return null;
if (index > this.length - 1) return null;
let check = this.head;
for (let i = 0; i <= index; i++) {
if (i === index) return check;
check = check.next;
}
}
set(index, value) {
let targetNode = this.get(index);
if (!targetNode) return false;
targetNode.value = value;
return true;
}
insert(index, value) {
const newNode = new Node(value);
if (index === 0) {
this.unshift(value);
return this;
}
const prevNode = this.get(index - 1);
const nextNode = this.get(index);
if (nextNode) {
newNode.next = nextNode;
prevNode.next = newNode;
this.length++;
return this;
}
if (prevNode) {
this.push(value);
return this;
}
return false;
}
remove(index) {
if (index < 0 || index > this.length - 1) return false;
if (index === 0) {
this.shift();
return this;
}
if (index === this.length - 1) {
this.pop();
return this;
}
const prevNode = this.get(index - 1);
const targetNode = this.get(index);
prevNode.next = targetNode.next;
targetNode.next = null;
this.length--;
return this;
}
reverse() {
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;
}
return this;
}
}