Skip to content

Commit f59bdce

Browse files
committed
Implemented iterator for DSList
1 parent fcc3981 commit f59bdce

4 files changed

Lines changed: 88 additions & 3 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- main
7+
- DSAdjList
78

89
jobs:
910
build:

CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ set(CMAKE_CXX_STANDARD 17)
2828
set(CMAKE_CXX_STANDARD_REQUIRED ON)
2929

3030
add_executable(${EXE_NAME} ./main.cpp
31-
DSString/dsstring.h DSString/dsstring.cpp
3231
./catch.hpp
3332
./test.cpp
33+
34+
DSString/dsstring.h DSString/dsstring.cpp
35+
3436
DSVector/dsvector.h
35-
DSStack/dsstack.h
37+
3638
DSDoublyLL/dsnode.h
3739
DSDoublyLL/dsdoublyll.h
3840

39-
DSAdjList/dsadjlist.h )
41+
DSStack/dsstack.h
42+
43+
DSAdjList/dsadjlist.h
44+
)

DSDoublyLL/dsdoublyll.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,67 @@
55

66
template <class T>
77
class DSDoublyLL{
8+
private:
9+
/**
10+
* @brief The iterator struct - iterator for the vector class
11+
*/
12+
struct iterator{
13+
public:
14+
/**
15+
* @brief iterator - constructor
16+
* @param ptr - pointer to start iteration from
17+
*/
18+
iterator(DSNode<T>* ptr): ptr(ptr){}
19+
20+
/**
21+
* @brief operator * - dereferences pointer
22+
* @return - reference of the class T at the pointer's location
23+
*/
24+
T& operator*(){
25+
return ptr->data;
26+
}
27+
28+
/**
29+
* @brief operator ++ - iterates to next pointer
30+
* @return this iterator after iteration
31+
*/
32+
iterator operator++(){
33+
ptr = ptr->next;
34+
return *this;
35+
}
36+
37+
/**
38+
* @brief operator ++
39+
* @return a reference to an iterator before iteration
40+
*/
41+
iterator operator++(int){
42+
iterator tmp = *this;
43+
ptr = ptr->next;
44+
return tmp;
45+
}
46+
47+
/**
48+
* @brief operator == compares if two iterators are equal
49+
* @param a - first iterator to compare
50+
* @param b - second iterator to compare
51+
* @return true if the two iterators are equal
52+
*/
53+
friend bool operator==(const iterator& a, const iterator& b){
54+
return a.ptr == b.ptr;
55+
}
56+
57+
/**
58+
* @brief operator != compares if two iterators are equal
59+
* @param a - first iterator to compare
60+
* @param b - second iterator to compare
61+
* @return true if the two iterators are not equal
62+
*/
63+
friend bool operator!=(const iterator& a, const iterator& b){
64+
return a.ptr != b.ptr;
65+
}
66+
private:
67+
DSNode<T>* ptr;
68+
};
869
public:
970
/**
1071
* @brief DSDoublyLL - default constructor
@@ -119,6 +180,14 @@ class DSDoublyLL{
119180
*/
120181
~DSDoublyLL();
121182

183+
inline iterator begin(){
184+
return iterator(head);
185+
};
186+
187+
inline iterator end(){
188+
return iterator(nullptr);
189+
}
190+
122191
private:
123192
DSNode<T>* head = nullptr;
124193
DSNode<T>* tail = nullptr;

test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,16 @@ TEST_CASE("Data_Structures_Doubly_Linked_List", "[Doubly_Linked_List][Data_Struc
388388

389389
numList3 = numList1 + numList2;
390390
}
391+
392+
SECTION("Iteration"){
393+
for(auto& it : numList1){
394+
it += 3;
395+
}
396+
397+
REQUIRE(numList1 == numList2);
398+
399+
numList1 = numList1Copy;
400+
}
391401
}
392402

393403
/*

0 commit comments

Comments
 (0)