-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUALdict.cpp
More file actions
55 lines (49 loc) · 1.12 KB
/
UALdict.cpp
File metadata and controls
55 lines (49 loc) · 1.12 KB
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
49
50
51
52
53
54
55
//dictionary implementation with an unsorted array-based list
template <typename Key, typename E>
class UALdict : public Dictionary<Key ,E>
{
private:
AList<KVpair<Key,E> >* list;
public:
UALdict(int size=defaultSize)
{list = new AList<KVpair<Key,E> >(size);}
~UALdict() {delete list;}
void clear() {list->clear();}
//insert an element: append to list
void insert(const Key& k, const E& e)
{
KVpair<Key,E> temp(k,e);
list->append(temp);
}
//use sequential search to find the element to remove
E remove(const Key& k)
{
E temp = find(k);
if (temp != NULL) list->remove();
return temp;
}
E removeAny()
{
Assert(size() != 0, "dictionary is empty");
list->moveToEnd();
list->prev();
KVpair<Key,E> e=list->remove();
return e.value();
}
//Find "k" using sequential search
E find(const Key& k) const
{
for (list->moveToStart();
list->currPos() < list->length();list->next())
{
KVpair<Key,E> temp = list->getValue();
if (k == temp.key())
return temp.value();
}
return NULL;
}
int size()
{
return list->length();
}
};