-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_const_iterator.h
More file actions
102 lines (85 loc) · 2.62 KB
/
list_const_iterator.h
File metadata and controls
102 lines (85 loc) · 2.62 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
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
#ifndef LIST_CONST_ITERATOR_H
#define LIST_CONST_ITERATOR_H
//added this to try to fix errors
class const_iterator {
// Give the parent class access to this class.
friend class List<Item_Type>;//changed
private:
// Data fields
/** A reference to the parent list */
const List<Item_Type>* parent;//changed
/** A pointer to the current DNode */
typename List<Item_Type>::DNode* current;//changed
// Member functions
/** Constructs an iterator that references a specific DNode.
Note: this constructor is private. Only the list class
can create one from scratch.
@param my_parent A reference to the list
@param position A pointer to the current DNode
*/
const_iterator(const List<Item_Type>* my_parent, DNode* position) :
parent(my_parent), current(position) {}//changed
/** Returns a reference to the currently referenced item.
@return A reference to the currently referenced item
@throws std::invalid_argument if this iterator is at end
*/
public:
const Item_Type& operator*() const {
if (current == NULL)
throw std::invalid_argument
("Attempt to dereference end()");
return current->data;
}
/** Returns a pointer to the currently referenced item.
Item_Type must be a class or struct. This restriction
is enforced by the compiler.
@return A pointer to the currently referenced item
@throws std::invalid_argument If this iterator is at end
*/
const Item_Type* operator->() const {
if (current == NULL)
throw std::invalid_argument
("Attempt to dereference end()");
return &(current->data);
}
const_iterator& operator++() {
if (current == NULL)
throw std::invalid_argument("Attempt to advance past end()");
current = current->next;
return *this;
}
const_iterator& operator--() {
if (current == parent->head)
throw std::invalid_argument("Attempt to move before begin()");
if (current == NULL) // Past last element.
current = parent->tail;
else
current = current->prev;
return *this;
}
const_iterator operator++(int) {
// Make a copy of the current value.
const_iterator return_value = *this;
// Advance self forward.
++(*this);
// Return old value.
return return_value; // Return the value prior to increment
}
const_iterator operator--(int) {
// Make a copy of the current value.
const_iterator return_value = *this;
// Move self backward.
--(*this);
// Return old value.
return return_value; // Return the value prior to decrement
}
//Compare for equality
bool operator ==(const const_iterator& other){
return current == other.current;
}
//Not equal
bool operator !=(const const_iterator&other){
return current != other.current;
}
};
#endif