-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintsetlist.cpp
67 lines (59 loc) · 1.46 KB
/
intsetlist.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
#include "intset.hpp"
void IntSetList::list_insert(int elements)
{
for (struct node *iter = root_node; iter; iter = iter->next_id)
{
struct node *next_node = iter->next_id;
// find position of node
if (!next_node || elements < next_node->key_val)
{
struct node *new_node = new struct node;
new_node->key_val = elements;
iter->next_id = new_node;
new_node->next_id = next_node;
return;
}
// if elements in list.
else if (next_node->key_val == elements)
{
this->set_size--;
return;
}
}
}
IntSetList::IntSetList(unsigned int max_elements, int max_val) : IntSet(max_elements, max_val)
{
root_node = new struct node;
root_node->key_val = -1;
root_node->next_id = NULL;
}
void IntSetList::deleteAllNodes()
{
struct node *tmpNode = root_node;
while (tmpNode)
{
struct node *next = tmpNode->next_id;
delete tmpNode;
tmpNode = next;
}
set_size = 0;
root_node = NULL;
}
IntSetList::~IntSetList()
{
deleteAllNodes();
}
void IntSetList::insert(int element)
{
if (element > this->max_val || set_size >= this->max_elements) {
return;
}
list_insert(element);
this->set_size++;
}
void const IntSetList::report(int *v)
{
int m = 0;
for (struct node *iter = root_node->next_id; iter; iter = iter->next_id)
v[m++] = iter->key_val;
}