-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointer_list.cpp
136 lines (123 loc) · 3.22 KB
/
pointer_list.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "pointer_list.h"
using namespace mtm;
PointerList::PointerList(): size(0), head(nullptr) {}
PointerList::~PointerList()
{
while(this->head)
{
Node *temp = this->head;
this->head = this->head->next;
delete temp->value;
delete temp;
}
}
PointerList::PointerList(const PointerList& list): size(list.size)
{
if(list.size > 0)
{
assert(list.head);
this->head = new Node(list.head->value);
Node* current_copy_node = this->head;
Node* current_original_node = list.head;
while(current_original_node->next)
{
current_copy_node->next = new Node(current_original_node->next->value->clone());
current_copy_node = current_copy_node->next;
current_original_node = current_original_node->next;
}
}
else
{
this->head = nullptr;
}
}
PointerList& PointerList::operator=(const PointerList& list)
{
if(this == &list){
return *this;
}
Node *new_head = nullptr;
if(list.size != 0)
{
assert(list.head);
new_head = new Node(list.head->value);
Node* current_list_node = list.head;
Node* current_new_head_node = new_head;
while(current_list_node->next)
{
current_new_head_node->next = new Node(current_list_node->next->value->clone());
current_new_head_node = current_new_head_node->next;
current_list_node = current_list_node->next;
}
}
while(this->head)
{
Node* temp = this->head;
this->head = this->head->next;
delete temp;
}
this->head = new_head;
this->size = list.size;
return *this;
}
bool PointerList::contains(BaseEvent* element) const
{
Node* current = head;
while(current)
{
if(*(element) == *(current->value))
{
return true;
}
current = current->next;
}
return false;
}
void PointerList::add(BaseEvent* element)
{
Node *new_node = new Node(element);
Node *current = this->head;
if(!current)
{
assert(this->size == 0);
this->head = new_node;
this->size++;
return;
}
if(*(new_node->value) < *(current->value))
{
new_node->next = current;
this->head = new_node;
this->size++;
return;
}
while(current->next && *(current->next->value) < *(new_node->value))
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
this->size++;
}
PointerList::ListIterator PointerList::end() const
{
return ListIterator(nullptr);
}
PointerList::ListIterator PointerList::begin() const
{
return ListIterator(this->head);
}
PointerList::ListIterator::ListIterator(Node* node):current(node){}
mtm::BaseEvent& PointerList::ListIterator::operator*() const{
return *(current->value);
}
PointerList::ListIterator& PointerList::ListIterator::operator++(){
this->current = this->current->next;
return *this;
}
bool PointerList::ListIterator::operator==(const ListIterator& other) const {
return this->current == other.current;
}
bool PointerList::ListIterator::operator!=(const ListIterator& other) const {
return !(*this==other);
}