๐ฉ๐ป๐ป ์ด์ 3์ฅ์์ array๋ฅผ ์ ์ฅ ๋ฉ์ปค๋์ฆ์ผ๋ก ํ์ฉํ๋ list์ ๋ํด ์ดํด๋ดค๋ค.
๐ ์ด๋ฒ์๋ ๋ค๋ฅธ ์ข ๋ฅ์ list์ธ "linked-list"์ ๋ํด ์ดํด๋ณด์.
โ๏ธ ์ด๋ค ์ ์์ list๋ณด๋ค ํจ์จ์ ์ผ๋ก ์ฐ์ผ ์ ์์์ง linked list๋ ๋ฌด์์ด๊ณ , class์ผ๋ก ๊ตฌํ, ๊ทธ๋ฆฌ๊ณ ์ข ๋ฅ์ ๋ํด์ ๋ค๋ค๋ณด์.
โน๏ธ Array์ ํ๊ณ
๋๋ถ๋ถ์ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์์ array๋ ๊ณ ์ ๋ ๊ธธ์ด๊ฐ ํ ๋น๋๋ค.
๋ฐ์ดํฐ ์๋ฆฌ๋จผํธ๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ๋ค๋ ๊ฑธ ๋ฐ์ํ์ฌ ์๋ฆฌ๋จผํธ๋ฅผ ์ ์๋๋ก ์ด๋์์ผ์ผ ํ๊ธฐ ๋๋ฌธ์ ๋ฒ๊ฑฐ๋กญ๋ค.
ํ์ง๋ง ์๋ฐ์คํฌ๋ฆฝํธ array๋ ์ด๋ฐ ๋ณต์กํ ์๊ฐ์ ํ ํ์ ์์ด split()ํจ์๋ก ๋ฐฐ์ด ์์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ ์ ์๋ค.
์ด๋ ์๋ฐ์คํฌ๋ฆฝํธ array๋ ๊ฐ์ฒด๋ก ๊ตฌํ๋๊ธฐ ๋๋ฌธ์ด๋ค. ๊ฐ์ฒด๋ก ์ด๋ฃจ์ด์ก๊ธฐ ๋๋ฌธ์ c++์ด๋ ์๋ฐ์ array๋ณด๋ค ๋นํจ์จ์ ์ผ ์ ์๋ค. array ์๋ฃ๊ตฌ์กฐ๋ฅผ ํ์ฉํ ์ฐ์ฐ์ด ๋๋ฆฌ๋ค๊ณ ์๊ฐํ๋ค๋ฉด,๋๋ค ์์์ ์ ๊ทผํด์ผํ๋ ๊ฒฝ์ฐ ์ ์ธํ๊ณ ๋ linked list๊ฐ ์ข์ ๋์์ผ ์ ์๋ค.
โ๏ธ๐ฅ Linked List ์ ์
Linked List๋ 'nodes' ๋ผ๊ณ ๋ถ๋ฆฌ๋ ๊ฐ์ฒด์ ์งํฉ์ด๋ค.
๊ฐ node๋ ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ํตํด ๋ค์ ๋ ธ๋์ ์ฐ๊ฒฐ๋๊ณ , ์ด ์ฐธ์กฐ๋ฅผ 'link'๋ผ๊ณ ํ๋ค.
array์ ์๋ฆฌ๋จผํธ๋ค์ ์์น์ ์ํด ์ฐธ์กฐ๋์ง๋ง linked list์ ์๋ฆฌ๋จผํธ๋ค์ ๋ค๋ฅธ ์๋ฆฌ๋จผํธ์์ ๊ด๊ณ์ ์ํด ์ฐธ์กฐ๋๋ค.
๋ค์ linked list์ ์๋ฅผ ์ดํด๋ณด์.
linked list์ ์์์์๋ถํฐ bread๋ milk๋ฅผ, eggs๋ bread๋ฅผ, bacon์ eggs๋ฅผ ์ฐธ์กฐํ๋ค. linked list์ ๋์ null node๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
linked-list์ ์์์ ํ์ํ๊ธฐ ์ํด head๋ผ๋ ํน์ํ node๋ฅผ ์ถ๊ฐํ ์ ์๋ค.
linked list์ ์๋ก์ด ๋ ธ๋๋ฅผ ์ถ๊ฐํ๋ ์์ ์ ์์ฃผ ํจ์จ์ ์ด๋ค.
์๋ก์ด ๋ ธ๋๋ฅผ ์ถ๊ฐํด์ฃผ๊ธฐ ์ํด์ ์ถ๊ฐํด์ค ์์น์ ์ด์ ๋ ธ๋๊ฐ ์๋ก ์ถ๊ฐํด์ฃผ๋ ๋ ธ๋๋ฅผ ๊ฐ๋ฆฌํค๊ณ ,
์๋ก์ด ๋ ธ๋๊ฐ ๋ค์ ๋ ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋๋ก ํด์ฃผ๋ฉด ๋๋ค.
๋ค์๊ณผ ๊ฐ์ด Eggs์ Bacon ๋ ธ๋ ์ฌ์ด์ ์๋ก์ด ๋ ธ๋ Cookies๋ฅผ ์ถ๊ฐํ๋ ์ฐ์ฐ์ ์๊ฐํด๋ณด์.
Eggs๋ bread๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์์์ง๋ง, ์ด link๋ฅผ ์ด์ Eggs๊ฐ Cookies๋ฅผ ๊ฐ๋ฆฌํค๊ณ , Cookies๊ฐ Bacon์ ๊ฐ๋ฆฌํค๋๋ก ํ๋ฉด ๋๋ค.
linked list์์ ๋ ธ๋๋ฅผ ์ญ์ ํ๋ ์ฐ์ฐ๋ ๊ฐ๋จํ๋ค.
์ญ์ ํ๋ ค๋ ๋ ธ๋๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๋ ๋งํฌ๊ฐ ์ญ์ ํ๋ ค๋ ๋ ธ๋๊ฐ ๊ฐ๋ฆฌํค๊ณ ์๋ ๋ ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋๋ก ํ๋ฉด ๋๋ค.
โ๏ธ๐ฅ Linked List ๊ตฌํ
linked list ๋ 2๊ฐ์ ํด๋์ค๋ก ๊ตฌํํ ์ ์๋ค.
Node๋ linked list์ ๋ ธ๋๋ฅผ ์ถ๊ฐํ๊ธฐ ์ํ ํด๋์ค๊ณ , LinkedList๋ ๋ ธ๋ ์ถ๊ฐ, ์ญ์ , ๋ฆฌ์คํธ ๋ณด์ฌ์ฃผ๊ธฐ ๋ฑ ์ฐ์ฐ์ ๋ด๋นํ๊ธฐ ์ํ ํด๋์ค๋ก ๋ง๋ค ์ ์๋ค.
โผ ์์ ๋ ์ฑ ์ ๊ตฌํ์ ์ฐธ๊ณ ํ์ฌ ํด๋์ค๋ก ์ฌ๊ตฌ์ฑํ์ต๋๋ค!!
class Node{
constructor(element){
this.element = element;
this.next = null;
}
}
class LinkedList{
constructor(){
this.head = new Node("head");
}
// ๋ค์ ๋
ธ๋๋ฅผ ์ฐพ๊ธฐ ์ํ ํฌํผ ํจ์
find(item){
let currNode = this.head; // ์์
while(currNode.element != item){
currNode = currNode.next; // ๋
ธ๋๋ฅผ ๋๋ฉด์ ์ฐพ์ผ๋ ค๋ item์ ํ์ฌ ๋
ธ๋๋ก ์ค์
}
return currNode;
}
// ๋
ธ๋ ์ถ๊ฐ
insert(newElement, item){
let newNode = new Node(newElement); // ์๋ก์ด ๋
ธ๋ ์ถ๊ฐ
let current = this.find(item); // ์ถ๊ฐํ๋ ค๋ ์์น
newNode.next = current.next; // ์ถ๊ฐํ๋ ค๋ ์์น ์ด์ ์ด ์๋ก์ด ๋
ธ๋ ๊ฐ๋ฆฌํค๋๋ก
current.next = newNode; // ์ถ๊ฐํ๋ ค๋ ์์น์ ๋
ธ๋๊ฐ ์๋ก์ด ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋๋ก
}
// ์ด์ ๋
ธ๋ ์ฐพ๊ธฐ
findPrevious(item){
let currNode = this.head;
while(!(currNode.next == null) && (currNode.next.element != item)){
currNode = currNode.next;
}
return currNode;
}
// ๋
ธ๋ ์ ๊ฑฐ
remove(item){
let previousNode = this.findPrevious(item);
if(!(previousNode.next == null)){
previousNode.next = previousNode.next.node;
}
}
// ๋
ธ๋ ์ถ๋ ฅ
display(){
let currNode = this.head;
const print = (item)=> console.log(item);
while(!(currNode.next == null)){
print(currNode.next.element);
currNode = currNode.next;
}
}
}
const cities = new LinkedList();
cities.insert("Seoul","head");
cities.insert("Incheon","Seoul");
cities.insert("Busan","Incheon");
cities.insert("Jeju","Busan");
cities.display();
console.log('\nremove Busan\n');
cities.remove("Busan");
cities.display();
/**
Seoul
Incheon
Busan
Jeju
remove Busan
Seoul
Incheon
*/
๐งฌ ์ด์ค Linked list
linked list๋ฅผ ์ฒซ ๋ ธ๋์์ ๋ง์ง๋ง ๋ ธ๋๊น์ง ์ํํ๋ ๊ฑด ๊ฐ๋จํ์ง๋ง ์ด์ ๋ ธ๋๋ฅผ ๊ฑฐ์ฌ๋ฌ ์ํํ๋ ๊ฑด ์ฝ์ง ์๋ค.
Node ํด๋์ค์ ์ด์ ๋ ธ๋ ์ ๋ณด๋ฅผ ๋ด๊ณ ์์ผ๋ฉด ์ญ์ํํ๋ ๊ณผ์ ์ด ์ฌ์์ง๋ค.
๋ ธ๋๋ฅผ linked list์ ์ถ๊ฐํ ๋ ์ถ๊ฐ๋ก ์ด์ ๊ณผ ๋ค์ ๋ ธ๋์ ๋งํฌ๋ฅผ ํ ๋นํ๋ ์ฐ์ฐ์ ์ถ๊ฐํด์ค์ผํ๋ค.
โผ ์์ ๋ ์ฑ ์ ๊ตฌํ์ ์ฐธ๊ณ ํ์ฌ ํด๋์ค๋ก ์ฌ๊ตฌ์ฑํ์ต๋๋ค!!
class Node{
constructor(element){
this.element = element;
this.next = null;
this.previous = null; // ์ด์ ๋
ธ๋ ์ ๋ณด
}
}
class LinkedList{
constructor(){
this.head = new Node("head");
}
// ๋ค์ ๋
ธ๋๋ฅผ ์ฐพ๊ธฐ ์ํ ํฌํผ ํจ์
find(item){
let currNode = this.head; // ์์
while(currNode.element != item){
currNode = currNode.next;
}
return currNode;
}
// ๋
ธ๋ ์ถ๊ฐ
insert(newElement, item){
let newNode = new Node(newElement);
let current = this.find(item);
newNode.next = current.next;
newNode.previous = current; // ์ถ๊ฐ
current.next = newNode;
}
// ์ํํ์ง ์๊ณ ๋ง์ง๋ง ๋
ธ๋๋ก ์ด๋
findLast(item){
let currNode = this.head;
while(!(currNode.next == null)){
currNode = currNode.next;
}
return currNode;
}
// ๋
ธ๋ ์ ๊ฑฐ
remove(item){
let currNode = this.find(item);
if(!(currNode.next == null)){
currNode.previous.next = currNode.next;
currNode.next.previous = currNode.previous;
currNode.next = null;
currNode.previous = null;
}
}
// ์ญ์ผ๋ก ์ถ๋ ฅ
displayReverse(){
let currNode = this.head;
currNode = this.findLast();
const print = (item)=> console.log(item);
while(!(currNode.previous ==null)){
print(currNode.element);
currNode = currNode.previous;
}
}
}
const cities = new LinkedList();
cities.insert("Seoul","head");
cities.insert("Incheon","Seoul");
cities.insert("Busan","Incheon");
cities.insert("Jeju","Busan");
cities.displayReverse();
console.log('\nremove Busan\n');
cities.remove("Busan");
cities.displayReverse();
/**
Jeju
Busan
Incheon
Seoul
remove Busan
Jeju
Incheon
Seoul
*/
๐ ์ํ linked list
์ํ linked list๋ ๋จ์ผ linked list์ node ์ข ๋ฅ์ ๊ตฌ์กฐ๊ฐ ๋น์ทํ๋ค.
๋ค๋ฅธ ์ ์ ์ํ linked list๋ ์์ฑ๋ ๋ head ๋ ธ๋์ next ์์ฑ์ด ์๊ธฐ ์์ ์ ๊ฐ๋ฆฌํจ๋ค๋ ๊ฒ์ด๋ค.
head.next = head์ผ๋ก ํ ๋น๋ ์ ์๋ค. ๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์ด ๊ผฌ๋ฆฌ๋ฅผ ๋ฌด๋ ํํ๋ก ์ด๋ฃจ์ด์ ธ ์๋ค.
์ํ linked list๋ฅผ ์ฌ์ฉํ๋ฉด ์ด์ค linked list๋ฅผ ์ฌ์ฉํ๋ฉด์ ์ด์ ๋ ธ๋๋ฅผ ์ค๊ฐ๋ ์ค๋ฒํค๋๋ฅผ ์ค์ผ ์ ์์ด์ ์ข๋ค.
์ํ linked list๋ ์์ผ๋ก ๊ณ์ ์ด๋ํ๋ฉด head๋ก ์ด๋ํ๊ธฐ ๋๋ฌธ์ ์ ๊ทผํ๋ ค๋ ๋ ธ๋๋ฅผ ์ฐพ์ผ๋ ค๊ณ ์๋ค๊ฐ๋ค ํ ํ์๊ฐ ์๋ค.
class Node {
constructor(element) {
this.element = element;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = new Node("head");
this.head.next = this.head;
this.current = this.head;
}
// ๋
ธ๋ ์ฐพ๊ธฐ ํฌํผ ํจ์
find(item) {
let currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
}
return currNode;
}
// ์๋ก์ด ๋
ธ๋ ์ถ๊ฐ
insert(newElement, item) {
let newNode = new Node(newElement);
let current = this.find(item);
newNode.next = current.next;
current.next = newNode;
}
// ์ด์ ๋
ธ๋ ์ฐพ๊ธฐ
findPrevious(item) {
let currNode = this.head;
while (!(currNode.next == null) && (currNode.next.element != item)) {
currNode = currNode.next;
}
return currNode;
}
// ๋
ธ๋ ์ ๊ฑฐ
remove(item) {
let previousNode = this.findPrevious(item);
if (!(previousNode.next == null)) {
previousNode.next = previousNode.next.next;
}
}
// ๋ชจ๋ ๋
ธ๋ ํ๋ฆฐํธ
display() {
let currNode = this.head;
const print = (item) => console.log(item);
while (!(currNode.next == null) && !(currNode.next.element == "head")) {
print(currNode.next.element);
currNode = currNode.next;
}
}
// n ๋
ธ๋ ์์ผ๋ก ์ด๋
advance(n) {
for (let i = 0; i < n; i++) {
if (this.current.next != this.head) {
this.current = this.current.next;
} else {
console.log("๋ฆฌ์คํธ ๋งจ ๋์
๋๋ค. ๋ ์ด์ ์์ผ๋ก ๊ฐ ์ ์์ต๋๋ค.");
break;
}
}
}
// n node ๋ค๋ก ์ด๋
back(n) {
for (let i = 0; i < n; i++) {
let prevNode = this.findPrevious(this.current.element);
if (prevNode != this.head) {
this.current = prevNode;
} else {
console.log("๋ฆฌ์คํธ ๋งจ ์์
๋๋ค. ๋ ์ด์ ๋ค๋ก ์ด๋ํ ์ ์์ต๋๋ค.");
break;
}
}
}
// ํ์ฌ ๋
ธ๋ ๋ฐ์ดํฐ ๋ณด์ด๊ธฐ
show() {
console.log("ํ์ฌ ๋
ธ๋: " + this.current.element);
}
}
// Usage example
const cities = new LinkedList();
cities.insert("Seoul", "head");
cities.insert("Incheon", "Seoul");
cities.insert("Busan", "Incheon");
cities.insert("Jeju", "Busan");
cities.display();
console.log("\nRemove Busan\n");
cities.remove("Busan");
cities.display();
console.log("\nCurrent node operations\n");
cities.show(); // Show current node
cities.advance(2); // Move forward 2 nodes
cities.show(); // Show current node
cities.back(1); // Move backward 1 node
cities.show(); // Show current node
๐ ๋ง์น๋ฉด์
โบ๏ธ ์ค๋์ ํ๊ฐ ๋ฌด์์ธ์ง, ์ด๋ค ์ฃผ์ ์ฐ์ฐ์ด ์๊ณ , ํ๋ฅผ ์ฌ์ฉํด์ ์ ๋ ฌํ๋ ์์์ ์ฐ์ ์์ ํ์ ๋ํด์ ์ดํด๋ดค๋ค.
์ด ์ฑ ์ด ์ฐ์ด๋ 2014์๋ ํด๋์ค ๋ฌธ๋ฒ์ด ๊ณต์ ๊ท์ ์ ์์๋ ๋๋ผ function์ผ๋ก ์ฐ์ด๊ณ ์๋ค.๋ฌผ๋ก ๊ทธ๋ฅ function์ผ๋ก ์จ๋ ๋์ง๋ง ํด๋์ค๋ฅผ ์ธ ๊ฒฝ์ฐ๊ฐ ๋ณ๋กค ์์ด์ ํ๊ธฐ ์ฐจ์์์ ์ฐ๋๊น ๋ซ๋ฐฐ๋.
์ด์ ๋ฌตํ๋๋ ํด๋์ค ๋ฌธ๋ฒ์ ๋ค์ ์ฌ์ฉํด ๋ด์ ์ข์๋ค.
๐ Reference
- Data Structures and Algorithms Using Javaโ Script by Michael McMillian (O’Reilly). Copyright 2014 Michael McMillan, 978-1-449-36493-9
'๐ค data structures & algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Data Structures & Algorithms with Javascript : Hashing (0) | 2024.07.23 |
---|---|
Data Structures & Algorithms with Javascript : Dictionaries (0) | 2024.07.22 |
Data Structures & Algorithms with Javascript : Queues (0) | 2024.07.20 |
Data Structures & Algorithms with Javascript : Stacks (0) | 2024.07.19 |
Data Structures & Algorithms with Javascript : Lists (0) | 2024.07.18 |