TypeScript数据结构与算法(14)基于LinkedList实现的Set
源码如下:
import { DataStruct_LinkedList } from "../04-Linked-List/DataStruct_LinkedList";
import { Interface_Set } from "../Interface_Set";
/**
* Autor: Created by 李清风 on 2021-01-02.
* Desc: 基于链表实现的集合Set,不要求具有可比性
*/
export class DataStruct_LinkedList_Set<T> implements Interface_Set<T> {
private list: DataStruct_LinkedList<T>;
public constructor() {
this.list = new DataStruct_LinkedList<T>();
}
//不能包含重复元素
add(e: T): void {
if (!this.list.contains(e)) {
this.list.addFirst(e);
}
}
remove(e: T): void {
this.list.removeElement(e);
}
contains(e: T): boolean {
return this.list.contains(e);
}
getSize(): number {
return this.list.getSize();
}
isEmpty(): boolean {
return this.isEmpty();
}
}

浙公网安备 33010602011771号