![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7765eee6baf04627cb824d8adc549064.png)
数据结构源码
接口
public interface Set<E> {
void add(E e);
void remove(E e);
boolean contains(E e);
int getSize();
boolean isEmpty();
}
实现类
public class LinkedListSet<E> implements Set<E> {
private LinkedList<E> list;
public LinkedListSet() {
list = new LinkedList<>();
}
@Override
public void add(E e) {
if (!list.contains(e)) {
list.addFirst(e);
}
}
@Override
public void remove(E e) {
list.removeElement(e);
}
@Override
public boolean contains(E e) {
return list.contains(e);
}
@Override
public int getSize() {
return list.getSize();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
}
数据结构拆解
维护字段和内部类
private LinkedList<E> list;
构造函数
public LinkedListSet() {
list = new LinkedList<>();
}
增
@Override
public void add(E e) {
if (!list.contains(e)) {
list.addFirst(e);
}
}
删
@Override
public void remove(E e) {
list.removeElement(e);
}
改
查
@Override
public boolean contains(E e) {
return list.contains(e);
}
@Override
public int getSize() {
return list.getSize();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}