๐ซ Dictionary ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ ํค-๊ฐ ์ผ๋ก ์ด๋ฃจ์ด์ง ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ค.
โ ์ ํ๋ฒํธ๋ถ์์ ์ด๋ฆ์ ์ฐพ์ผ๋ฉด ํด๋น ๋ฒํธ๋ฅผ ๋ฐ๋ก ์ฐพ์ ์ ์๊ธฐ ๋๋ฌธ์ ๊ฒ์ ์์ ์์ฃผ ํธํ๊ฒ ๊ฐ์ ์ฐพ์ ์ ์๋ค.
๐ท ์๋ฐ์คํฌ๋ฆฝํธ Object ํด๋์ค๋ dictionary ์ฒ๋ผ ๋์ํ๋๋ก ๋์์ธ ๋์๋ค๊ณ ํ๋ค.
๐ ์ค๋์ Object ํด๋์ค๋ฅผ ํ์ฉํด์ Dictionary ํด๋์ค๋ฅผ ๋ง๋ค์ด๋ณด๊ณ , dictionary ์ฌ์ฉ์ ํน์ง๊ณผ ์ฅ์ ๋ค์ ์ดํด๋ณด์!
๐ Dictionary ํด๋์ค
Dictionary ํด๋์ค์ ๊ธฐ๋ฐ์ Object ํด๋์ค๊ฐ ์๋๋ผ Array ํด๋์ค๋ค.
์๋ฐ์คํฌ๋ฆฝํธ Object์ ํ๋กํผํฐ๋ฅผ ์ ๋ ฌํ ์๋ ์์ง๋ง dictionary์ ํค๋ ์ ๋ ฌํ ์ ์๋ค.
ํ์ง๋ง ์๋ฐ์คํฌ๋ฆฝํธ์์ ์์ํ์ ์ด ์๋ ๊ฐ์ ๋ชจ๋ ๋ ๊ฐ์ฒด์ด๋ฏ๋ก array๋ ๊ฐ์ฒด๋ค.
โผ ์์ ๋ ์ฑ ์ ๊ตฌํ์ ์ฐธ๊ณ ํ์ฌ ํด๋์ค๋ก ์ฌ๊ตฌ์ฑํ์ต๋๋ค!!
class Dictionary{
constructor(){
this.datastore = new Array();
}
// ์ถ๋ ฅ ํฌํผ ๋ฉ์๋
print(item){
console.log(item);
}
// ํค์ ๊ฐ ์ถ๊ฐ
add(key,value){
this.datastore[key] = value;
}
// ํค์ ํด๋นํ๋ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
find(key){
return this.datastore[key]
}
// ํค์ ํด๋นํ๋ ํค์ ๊ฐ ์ ๊ฑฐ
remove(key){
delete this.datastore[key];
}
// ํค์ ํด๋นํ๋ ํค์ ๊ฐ ์ ๊ฑฐ
showAll(){
Object.keys(this.datastore).forEach((key)=>this.print(key+" -> "+this.datastore[key]));
}
////////////// ์ถ๊ฐ์ ์ธ ํฌํผ ๋ฉ์๋๋ค //////////////
// dictionary ์ ํค-๊ฐ ๊ฐ์
// string ์ด ํค์ผ ๊ฒฝ์ฐ length ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ ์ ์๊ธฐ ๋๋ฌธ์
count(){
let n = 0;
Object.keys(this.datastore).forEach((key)=> ++n);
return n;
}
// dictionary ๋น์ฐ๊ธฐ
clear(){
Object.keys(this.datastore).forEach((key)=> delete this.datastore[key]);
}
////////////// ์ถ๊ฐ์ ์ธ ํฌํผ ๋ฉ์๋๋ค //////////////
}
const contacts = new Dictionary();
contacts.add("a",'010-1234-1234');
contacts.add("b",'010-1111-2222');
contacts.add("c",'010-2222-3333');
contacts.print(`b์ ์ ํ ๋ฒํธ : ${contacts.find('b')}`);
contacts.remove("c");
contacts.showAll();
contacts.clear();
contacts.print(`dictionary ์์ดํ
๊ฐ์ : ${contacts.count()}`);
/**
b์ ์ ํ ๋ฒํธ : 010-1111-2222
a -> 010-1234-1234
b -> 010-1111-2222
dictionary ์์ดํ
๊ฐ์ : 0
*/
๐ Dictionary ์ ๋ ฌํ๊ธฐ
Dictionary ์ ์ฃผ์ ๋ชฉ์ ์ ํค๋ก ๊ฐ์ ๋ฐํํ๋ ๊ฒ์ด๋ค.
์ค์ ๋ก dictionary๊ฐ ์ ์ฅ๋๋ ์์๊ฐ ์ค์ํ์ง ์์ ์๋ ์์ง๋ง ์ ๋ ฌ๋ ์ํ๋ก ์ ์ฅ๋๋ ๊ฒฝ์ฐ๊ฐ ํ์ํ ์๋ ์๋ค.
array์ ํค๊ฐ ์ซ์์ธ ๊ฒฝ์ฐ ์ ๋ ฌ์ด ๊ฐ๋ฅํ๋ค.
๋ค์์ ์ซ์ ํค ๊ฐ์ ํ array๋ฅผ ๊ฐ ๊ธฐ์ค์ผ๋ก ์ํ๋ฒณ ์์๋๋ก ์ ๋ ฌํ ๊ฒ์ด๋ค.
const print = item => console.log(item);
let arr = new Array();
arr[0] = "Millie";
arr[1] = "Danielle";
print(arr); // ["Millie", "Danielle"];
arr.sort();
print(arr); // ["Danielle", "Millie"];
ํ์ง๋ง string๊ฐ์ธ ํค๋ก๋ ๋์ผํ ๋ฐฉ์์ผ๋ก ๋์ํ๋๋ก ํ ์ ์๋ค.
๋์ ํค ๊ฐ์ผ๋ก ์ ๋ ฌํ๊ณ ์ ํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์๋ค.
class Dictionary{
constructor(){
this.datastore = new Array();
}
print(item){
console.log(item);
}
add(key,value){
this.datastore[key] = value;
}
find(key){
return this.datastore[key]
}
remove(key){
delete this.datastore[key];
}
showAll(){
// ๋ณ๊ฒฝ
Object.keys(this.datastore).sort().forEach((key)=>this.print(key+" -> "+this.datastore[key]));
}
count(){
let n = 0;
Object.keys(this.datastore).forEach((key)=> ++n);
return n;
}
clear(){
Object.keys(this.datastore).forEach((key)=> delete this.datastore[key]);
}
}
const contacts = new Dictionary();
contacts.add("b",'010-2342-3333');
contacts.add("a",'010-1234-1234');
contacts.add("d",'010-1111-2321');
contacts.add("f",'010-2424-3333');
contacts.add("e",'010-333-2222');
contacts.add("c",'010-1023-3333');
contacts.showAll();
/**
a -> 010-1234-1234
b -> 010-2342-3333
c -> 010-1023-3333
d -> 010-1111-2321
e -> 010-333-2222
f -> 010-2424-3333
*/
๐ 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 : Sets (2) | 2024.07.24 |
---|---|
Data Structures & Algorithms with Javascript : Hashing (0) | 2024.07.23 |
Data Structures & Algorithms with Javascript : Linked Lists (0) | 2024.07.21 |
Data Structures & Algorithms with Javascript : Queues (0) | 2024.07.20 |
Data Structures & Algorithms with Javascript : Stacks (0) | 2024.07.19 |