首页 > 代码库 > js实现存储对象的数据结构hashTable和list

js实现存储对象的数据结构hashTable和list

以下代码是typescript语言来写的,其实和es6面向对象的写法基本一致。大家阅读后都明白这些方法的作用。

hash

hash结构用于处理和表现类似key/value的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。

实现该数据结构的几个方法:

函数名说明返回值
set(key,value)添加项
del(key)根据key删除一项
has(key)是否包含某个keybool
get(key)根据key值获取valuevalue
first()获取第一个valuevalue
last()获取最后一个valuevalue
count()获取项总数int
all()返回所有值的数组array
getByIndex(index)根据在数组中的index顺序获取valuevalue
foreach(callback)遍历所有值
indexOf(key)根据key值获取在数组中的顺序值index,int
insertAt(index,value,key)在指定index位置插入key,value
  1   export class HashTable<T> {  2         private items: { [key: string]: HashValue<T> };  3         @SerializeProperty({ list: true, type: Array })  4         private itemList: Array<T>;  5         constructor() {  6             super();  7             this.items = {};  8             this.itemList = [];  9         } 10  11         set(key: string, value: T): void { 12             var vl = new HashValue<T>(); 13             vl.key = key; 14             vl.value =http://www.mamicode.com/ value; 15             var index = this.itemList.length; 16             if (this.has(key)) { 17                 index = this.items[key].index; 18             } 19             vl.index = index; 20             this.itemList[index] = value; 21             this.items[key] = vl; 22         } 23  24         del(key: string): void { 25             if (this.has(key)) { 26                 var index = this.items[key].index; 27                 if (index > -1) { 28                     this.itemList.splice(index, 1); 29                 } 30                 delete this.items[key]; 31                 this.resetIndex(); 32             } 33         } 34  35         resetIndex(): void { 36  37             this.foreach((k, v: T) => { 38                 var index = this.itemList.indexOf(v); 39                 this.items[k].index = index; 40             }); 41         } 42  43         has(key: string): boolean { 44             return key in this.items; 45         } 46  47         get(key: string): T { 48             if (this.has(key)) { 49                 return this.items[key].value; 50             } 51             return null; 52         } 53  54         count(): number { 55             return this.itemList.length; 56         } 57  58         all(): Array<T> { 59             return this.itemList; 60         } 61  62         first() { 63             return this.itemList[0]; 64         } 65  66         last() { 67             return this.itemList[this.itemList.length - 1]; 68         } 69  70         getByIndex(index: number): T { 71             return this.itemList[index]; 72         } 73  74         //遍历 扩展 75         foreach(callback) { 76             for (var key in this.items) { 77                 callback(key, this.items[key].value); 78             } 79         } 80  81         //获取index 82         indexOf(key) { 83             if (this.has(key)) { 84                 return this.items[key].index; 85             } 86         } 87  88         //插入 89         insertAt(index: number, value: T, key: string) { 90             this.itemList.splice(index, 0, value); 91             var hashV = new HashValue<T>(); 92             hashV.index = index; 93             hashV.key = key; 94             hashV.value =http://www.mamicode.com/ value; 95             this.items[key] = hashV; 96             this.resetIndex(); 97         } 98  99         sort(callback: Function) {100             this.itemList.sort((a: T, b: T) => { return callback(a, b);     101           });102         }103     }

List

js实现链表List数据结构的几个方法:

函数名说明返回值
add(value)添加项
addList(list)添加另一个集合
pop()删除最后一项 并返回该项value
shift()删除第一项 并返回该项value
remove(index)根据索引值删除某一项
removeMany(index,count)删除从指定位置开始的某几项
clear()删除所有项
contains(value)是否包含某个值boolean
indexOf(value)根据值获取在数组中的顺序值int
get(index)根据index获取valuevalue
set(index,value)设置index位置的value
length()获取项总数int
all()返回所有值的数组array
foreach(callback)遍历所有值
reverseForeach(callback)倒序遍历所有值
sort(callback)根据某个排序规则进行排序
insert(index,value)在指定index位置插入value

 

 1  export class List<T> { 2         private items: Array<T>; 3  4         private checkIndex(index): boolean { 5             return !(index < 0 || isNaN(index) || index >= this.items.length); 6         } 7         constructor() { 8             super(); 9             this.items = new Array<T>();10         }11 12         length(): number {13             return this.items.length;14         }15 16         add(value: T): void {17             this.items.push(value);18         }19         addList(valueList: List<T>) {20             for (var i = 0; i < valueList.length(); i++) {21 22                 var value =http://www.mamicode.com/ valueList.get(i);23                 this.items.push(value);24             }25         }26         pop(): T {27             return this.items.pop();28         }29 30         shift() {31             this.items.shift();32         }33 34         remove(index: number): void {35             if (this.checkIndex(index)) {36               this.items.splice(index,1);37                 38             }39         }40         /**41          * 從指定索引處開始刪除指定個數的元素42          * @param from 43          * @param count 44          */45         removeMany(from: number, count: number) {46 47             if (this.checkIndex(from)) {48                 this.items.splice(from, count);49             }50         }51 52         clear(): void {53             this.items = [];54         }55 56         contains(value: T): boolean {57             for (var i in this.items) {58                 return value =http://www.mamicode.com/= this.items[i];59             }60             return false;61         }62 63         indexOf(value: T): number {64             return this.items.indexOf(value);65         }66 67         insert(index: number, value: T) {68             //this.checkIndex(index) && this.items.splice(index , 0, value);69             this.items.splice(index, 0, value);70         }71 72         get(index: number): T {73             return this.items[index];74         }75         set(index, value: T) {76             this.items[index] = value;77         }78         all(): Array<T> {79             return this.items;80         }81         foreach(callback:(i:number,item:T)=>any) {82             var len = this.items.length;83             for (var i = 0; i < len; i++) {84                 if (callback(i, this.items[i]) === false) break;85             }86         }87         reverseForeach(callback) {88             var len = this.items.length;89             for (var i = len - 1; i >= 0; i--) {90                 if (callback(i, this.items[i]) === false) break;91             }92         }93         sort(callback: Function) {94             this.items.sort((a: T, b: T) => { return callback(a, b); });95         }96     }

 

js实现存储对象的数据结构hashTable和list