Skip to content

Commit

Permalink
added iterators to lists
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Feb 3, 2024
1 parent 9135888 commit 03f39d7
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 91 deletions.
3 changes: 2 additions & 1 deletion src/classes/graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ template <typename T> class graph {
return;
}
}
~graph() {}

~graph() { adj.clear(); }

/**
* @brief add_edge function
Expand Down
266 changes: 180 additions & 86 deletions src/classes/list/doubly_linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,122 +36,58 @@ template <typename T> class doubly_linked_list {
*/
size_t size() { return __size; }

class Iterator;

/**
* @brief pointer that points to begin
*
* @return Iterator
*/
Iterator begin() { return Iterator(root); }

/**
* @brief pointer that points to end
*
* @return Iterator
*/
Iterator end() { return Iterator(nullptr); }

/**
*@brief search function.
*@param key: the key to be searched.
*@returns true if key exists in the list.
*/
bool search(T key) {
if (this->empty()) {
return false;
} else {
std::shared_ptr<node> t = root;
while (t != tail && t->val != key) {
t = t->next;
}
if (t == tail || t == nullptr) {
return false;
}
return true;
}
return false;
}
bool search(T key);

/**
*@brief push_back function.
*@param key: the key to be pushed back.
*/
void push_back(T key) {
std::shared_ptr<node> p = std::make_shared<node>(key);
if (root == nullptr) {
root = p;
}
if (tail != nullptr) {
tail->next = p;
}
p->next = nullptr;
p->prev = tail;
tail = p;
__size++;
}
void push_back(T key);

/**
*@brief push_front function.
*@param key: the key to be pushed in front.
*/
void push_front(T key) {
std::shared_ptr<node> p = std::make_shared<node>(key);
p->next = root;
p->prev = nullptr;
if (root != nullptr) {
root->prev = p;
}
root = p;
__size++;
}
void push_front(T key);

/**
*@brief erase function.
*@param key: the key to be erased from the list.
*/
void erase(T key) {
if (root == nullptr) {
return;
}
if (root->val == key) {
root = root->next;
}
std::shared_ptr<node> head = root;
while (head && head->val != key) {
head = head->next;
}
if (head == nullptr) {
return;
}
if (head->next != nullptr) {
head->next->prev = head->prev;
}
if (head->prev != nullptr) {
head->prev->next = head->next;
}
}
void erase(T key);

/**
*@brief elements function.
*@returns vector<T>: the elements of the list.
*/
std::vector<T> elements() {
std::vector<T> __elements;
if (this->empty()) {
return __elements;
}
std::shared_ptr<node> head = root;
while (head) {
__elements.push_back(head->val);
head = head->next;
}
return __elements;
}
std::vector<T> elements();

/**
* @brief reverse function.
* reverses the linked list.
*/
void reverse() {
std::shared_ptr<node> current = root;
std::shared_ptr<node> temp{nullptr};

while (current != nullptr) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}

if (temp) {
root = temp->prev;
}
}
void reverse();

/**
*@brief << operator for the doubly_linked_list class.
Expand Down Expand Up @@ -185,4 +121,162 @@ template <typename T> class doubly_linked_list {
size_t __size;
};

template <typename T> bool doubly_linked_list<T>::search(T key) {
if (this->empty()) {
return false;
} else {
std::shared_ptr<node> t = root;
while (t != tail && t->val != key) {
t = t->next;
}
if (t == tail || t == nullptr) {
return false;
}
return true;
}
return false;
}

template <typename T> void doubly_linked_list<T>::push_back(T key) {
std::shared_ptr<node> p = std::make_shared<node>(key);
if (root == nullptr) {
root = p;
}
if (tail != nullptr) {
tail->next = p;
}
p->next = nullptr;
p->prev = tail;
tail = p;
__size++;
}

template <typename T> void doubly_linked_list<T>::push_front(T key) {
std::shared_ptr<node> p = std::make_shared<node>(key);
p->next = root;
p->prev = nullptr;
if (root != nullptr) {
root->prev = p;
}
root = p;
__size++;
}

template <typename T> void doubly_linked_list<T>::erase(T key) {
if (root == nullptr) {
return;
}
if (root->val == key) {
root = root->next;
}
std::shared_ptr<node> head = root;
while (head && head->val != key) {
head = head->next;
}
if (head == nullptr) {
return;
}
if (head->next != nullptr) {
head->next->prev = head->prev;
}
if (head->prev != nullptr) {
head->prev->next = head->next;
}
}

template <typename T> std::vector<T> doubly_linked_list<T>::elements() {
std::vector<T> __elements;
if (this->empty()) {
return __elements;
}
std::shared_ptr<node> head = root;
while (head) {
__elements.push_back(head->val);
head = head->next;
}
return __elements;
}

template <typename T> void doubly_linked_list<T>::reverse() {
std::shared_ptr<node> current = root;
std::shared_ptr<node> temp{nullptr};

while (current != nullptr) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}

if (temp) {
root = temp->prev;
}
}

/**
* @brief Iterator class
*/
template <typename T> class doubly_linked_list<T>::Iterator {
private:
std::shared_ptr<node> curr_root;

public:
/**
* @brief Construct a new Iterator object
*
* @param l doubly linked list type
*/
explicit Iterator(const std::shared_ptr<node> &l) noexcept : curr_root(l) {}

/**
* @brief = operator for Iterator type
*
* @param current smart pointer of type node
* @return Iterator&
*/
Iterator &operator=(std::shared_ptr<node> current) {
this->curr_root = current;
return *(this);
}

/**
* @brief operator ++ for type Iterator
*
* @return Iterator&
*/
Iterator &operator++() {
if (curr_root) {
curr_root = curr_root->next;
}
return *(this);
}

/**
* @brief operator ++ for type Iterator
*
* @return Iterator
*/
Iterator operator++(int) {
Iterator it = *this;
++*(this);
return it;
}

/**
* @brief operator != for type Iterator
*
* @param it const Iterator
* @return true if curr_root == it.curr_root
* @return false otherwise
*/
bool operator!=(const Iterator &it) { return curr_root != it.curr_root; }

/**
* @brief operator * for type Iterator
*
* @return T the value of the node
*/
T operator*() { return curr_root->val; }
};

#endif
Loading

0 comments on commit 03f39d7

Please sign in to comment.