Linked List & Iterators
Dec 2, 2018 at 2:06am UTC
I'm having issues with my linked list and including iterators I get the error code
helpme.cpp:281:27: error: no member named 'begin' in 'LL<char>::It'
LL<char> first(that.begin(), that.end());
~~~~ ^
helpme.cpp:270:13: note: in instantiation of member function 'LL<char>::It::operator=' requested here
this->operator=(other);
^
helpme.cpp:245:21: note: in instantiation of member function 'LL<char>::It::It' requested here
for (; first != last; ++first) this->InsertBack(*first);
^
helpme.cpp:321:12: note: in instantiation of function template specialization 'LL<char>::LL<LL<char>::It>' requested here
LL<char> temp (list.begin(),list.end());
^
1 error generated.
also testing lines 264-267.
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
#include <cstddef>
#include <initializer_list>
#include <iostream>
#include <algorithm>
template <typename T>
class LL {
struct node;
class It;
public :
LL() {
}
LL(std::initializer_list<T> content) {
this ->operator =(content);
}
LL(const LL<T>& diff) {
this ->operator =(diff);
}
~LL() {
while (this ->start) {
node* oldStart = this ->start;
this ->start = oldStart->next;
delete oldStart;
}
}
std::size_t Size() const {
return size;
}
bool Empty() const {
if (Size() == 0) { return true ; }
else { return false ; }
}
T& First() const {
if (Empty() == true ) {
throw std::domain_error("Error 404" );
}
return start->data;
}
T& Last() const {
if (Empty() == true ) {
throw std::domain_error("Error 404" );
}
return last->data;
}
void InsertBack(T val) {
node* new_node = new node{val};
if (this ->size == 0) {
this ->start = this ->last = new_node;
} else {
this ->last->next = new_node;
new_node->prev = this ->last;
this ->last = new_node;
}
++this ->size;
}
void InsertFront(T val) {
node* new_node = new node{val};
if (this ->size == 0) {
this ->start = this ->last = new_node;
} else {
new_node->next = this ->start;
this ->start->prev = new_node;
this ->start = new_node;
}
++this ->size;
}
void RemoveFront() {
if (Empty() == true ) {
return ;
}
node* cur = start->next;
delete start;
start = cur;
--size;
}
void RemoveBack() {
if (Empty() == true ) {
return ;
}
if (start == NULL) {
return ;
}
if (start->next == NULL) {
delete start;
start = NULL;
return ;
}
node* cur = start;
while (cur->next && cur->next->next != NULL) {
cur = cur->next;
}
delete cur->next;
cur->next = NULL;
last = last->prev;
--size;
}
void Resize(std::size_t n, const T &fill_value) {
int count = 0;
node* cur = start;
while (cur != NULL) {
count++;
cur = cur->next;
}
if (count > n) {
n = count - n;
for (int i = 0; i < n; i++) {
RemoveBack();
}
} else {
n = n - count;
for (int i = 0; i < n; i++) {
InsertBack(fill_value);
}
}
}
void Clear() {
while (this ->start) {
node* old_head = this ->start;
this ->start = old_head->next;
delete old_head;
}
}
void RemoveAll(const T& val) {
if (Empty() == true ) {
return ;
}
node* cur = this ->start;
node* temp = this ->start;
while (cur != last) {
if (cur->data == val) {
if (cur == start) {
start = cur->next;
delete cur;
cur = start;
} else {
temp->next = cur->next;
delete cur;
cur = temp->next;
}
} else {
temp = cur;
cur = cur->next;
}
}
}
void RemoveDuplicate() {
node* cur = this ->start;
node* temp = this ->start;
while (cur != last) {
if (cur->data == cur->next->data) {
if (cur == start) {
start = cur->next;
delete cur;
cur = start;
} else {
temp->next = cur->next;
delete cur;
cur = temp->next;
}
} else {
temp = cur;
cur = cur->next;
}
}
}
void Reversing() {
if (start == NULL) {
return ;
} else if (Size() == 1) {
return ;
}
node* cur = start;
node* temp = start->next;
while (cur->next != last) {
temp = cur->next;
cur->next = cur->next->next;
temp->next = start;
start = temp;
}
}
LL& operator =(std::initializer_list<T> content) {
Clear();
this ->size = 0;
for (auto && val : content) this ->InsertBack(val);
return *this ;
}
LL& operator =(const LL& diff) {
Clear();
node* cur = diff.start;
while (cur != NULL) {
this ->InsertBack(cur->data);
cur = cur->next;
}
return *this ;
}
bool operator ==(const LL&diff) {
node* cur = diff.start;
if (diff.Size() != Size()) {
return false ;
}
while (start != NULL) {
if (cur->data != start->data) {
return false ;
}
cur = cur->next;
start = start->next;
}
return true ;
}
bool operator !=(const LL&diff) {
node* cur = diff.start;
if (diff.Size() == Size()) {
return false ;
}
while (start != NULL) {
if (cur->data == start->data) {
return false ;
}
cur = cur->next;
start = start->next;
}
return true ;
}
friend std::ostream& operator <<(std::ostream& out, const LL& c) {
out << '[' ;
for (auto cur = c.start; cur; cur = cur->next) {
out << cur->data;
if (cur->next) out << ", " ;
}
out << ']' ;
return out;
}
template <class It>
LL(It first, It last) {
for (; first != last; ++first) this ->InsertBack(*first);
}
It begin() { return It(start); }
It end() { return It(NULL); }
private :
struct node {
T data;
node* next = nullptr ;
node* prev = nullptr ;
};
class It {
friend class LL;
explicit It(LL::node* val) : cur(val) {}
public :
using It_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = int ;
using pointer = T*;
It() {}
It(const It& other) {
this ->operator =(other);
}
~It() {
while (this ->cur) {
typename LL<T>::node* temp;
temp = cur;
cur = ++temp;
delete temp;
}
}
It& operator =(const It& that) {
LL<char > first(that.begin(), that.end());
}
It& operator ++() {
this ->cur = cur->next;
return *this ;
}
It operator ++(int ) {
It temp(*this );
this ->operator ++();
return temp;
}
It& operator --() {
this ->cur = cur->prev;
return *this ;
}
It operator --(int ) {
It temp(*this );
this ->operator --();
return temp;
}
bool operator !=(It that) const { return !(this ->operator ==(that));}
bool operator ==(It that) const { return this ->cur == that.cur;}
T& operator *() const {return this ->cur->data;}
It* operator ->() const {return this ;}
private :
typename LL<T>::node* cur;
};
node* start = nullptr ;
node* last = nullptr ;
std::size_t size = 0;
};
int main() {
LL<char > list;
std::cout << list << std::endl;
for (auto c : std::string("Hello World!" )) list.InsertBack(c);
std::cout << list << std::endl;
LL<char > temp (list.begin(),list.end());
temp.RemoveBack();
std::cout << temp << " = It test" << std::endl;
if (list.begin() == temp.begin()) {
std::cout << "True they are equal" << std::endl;
}
LL<char > first (list);
LL<char > copy = list;
copy.Resize(10, '!' );
list.RemoveAll('o' );
copy.RemoveDuplicate();
std::cout << list << std::endl << copy << std::endl;
}
Dec 2, 2018 at 2:46am UTC
1 2 3
It& operator =(const It& that) {
LL<char > first(that.begin(), that.end());
}
on your iterator class
`that' is an iterator, ¿does an iterator have `.begin()' and `.end()' member functions?
¿why is an assignment operator for iterators trying to create a list?
you should simply copy the `cur' member variable, which is what the default assignment operator does.
Dec 2, 2018 at 2:46am UTC
The list itself has the begin() and end() methods, not the iterator. So this doesn't make sense because "that" is an iterator, not a list.
1 2 3
It& operator =(const It& that) {
LL<char > first(that.begin(), that.end()); // and <char> should presumably be <T>
}
There's too much code for me to look through in detail but I noticed a couple of things. For instance
operator!= can be simplified to this:
1 2 3
bool operator !=(const LL&diff) {
return !operator ==(diff);
}
Don't say
if (Empty() == true)
Just say
if (Empty())
That's how booleans work.
Also, don't say:
1 2 3 4
bool Empty() const {
if (Size() == 0) { return true ; }
else { return false ; }
}
Just say
1 2 3
bool Empty() const {
return (Size() == 0);
}
Last edited on Dec 2, 2018 at 2:49am UTC
Topic archived. No new replies allowed.