-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathLinkedListSet.h
48 lines (38 loc) · 890 Bytes
/
LinkedListSet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// Created by light on 19-11-8.
//
#ifndef ALG_LINKEDLISTSET_H
#define ALG_LINKEDLISTSET_H
#include "../linklist/LinkedList.h"
template<typename Key>
class LinkedListSet : public Set<Key> {
private:
LinkedList<Key> *linkedList;
public:
LinkedListSet() {
linkedList = new LinkedList<Key>;
}
~LinkedListSet() {
if (linkedList)
delete linkedList;
}
// O(n)
virtual void insert(Key key) {
if (!linkedList->contains(key)) linkedList->addFirst(key);
}
// O(n)
virtual void remove(Key key) {
linkedList->removeElement(key);
}
// O(n)
virtual bool contain(Key key) {
return linkedList->contains(key);
}
virtual int size() {
return linkedList->getSize();
}
virtual bool isEmpty() {
return linkedList->isEmpty();
}
};
#endif //ALG_LINKEDLISTSET_H