Skip to content

Commit

Permalink
Fixed bugs in double_node and dl_list classes
Browse files Browse the repository at this point in the history
  • Loading branch information
SleepiCaffeine committed Jun 20, 2023
1 parent fcd8e9d commit 1644e40
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
38 changes: 32 additions & 6 deletions Data Structures/dl_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "double_node.hpp"
#include <cstddef>
#include <stdexcept>
#include <iostream>

/*!
* @class dl_list
Expand Down Expand Up @@ -260,14 +261,18 @@ class dl_list {
* @return Length of LL
* @see size()
*/
unsigned int length() const {return len;}
unsigned int length() {
update_len();
return len;}

/**
* Returns the length of the sl list
* @return Length of LL
* @see length()
*/
unsigned int size() const {return len;}
unsigned int size() {
update_len();
return len;}

/**
* Updates the length of the sl_list (Mainly kept to avoid bugs, will later implement other features to avoid using this)
Expand Down Expand Up @@ -332,6 +337,15 @@ double_node<T>* dl_list<T>::push_front(T dt) {
++len;
return head;
}

if (len == 1) {
tail = head;
head = new double_node<T>(dt);
head->set_next(tail);
tail->set_prev(head);
++len;
return head;
}
// Create a new head object with the provided data and pointing to the head itself
head = new double_node<T>(head, dt);
++len;
Expand All @@ -342,16 +356,26 @@ template <typename T>
double_node<T>* dl_list<T>::push_front(double_node<T>* const nd) {
// If the list has a length of 0, create a node for the head, and set the tail to nullptr
if (!len) {
head = new double_node<int>(*nd);
head = new double_node<T>(*nd);
tail = nullptr;
++len;
return head;
}

if (len == 1) {
tail = head;
head = new double_node<T>(*nd);
head->set_next(tail);
tail->set_prev(head);
++len;
return head;
}

// Swap head and provided node
auto oldHead = new double_node<T>(head->get_next(), nd, head->get_data());
auto oldHead = new double_node<T>(*head);
oldHead->set_prev(nd);
nd->set_next(oldHead);
head = nd;
head = new double_node<T>(*nd);

++len;
return head;
Expand Down Expand Up @@ -411,7 +435,9 @@ double_node<T>* dl_list<T>::push_back(double_node<T>* const nd) {
}

double_node<T>* currNode = head;
double_node<T>* oldTail = new double_node<T>(nd, tail->get_prev(), tail->get_data());
double_node<T>* oldTail = new double_node<T>(*tail);
oldTail->set_next(nd);

nd->set_prev(oldTail);
tail = nd;

Expand Down
10 changes: 2 additions & 8 deletions Data Structures/double_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,8 @@ class double_node {
};

template <typename T>
double_node<T>::double_node(const double_node<T>& nd) {
next = new double_node<T>();
prev = new double_node<T>();

next = nd.get_next();
prev = nd.get_prev();
data = nd.get_data();
}
double_node<T>::double_node(const double_node<T>& nd)
: next{nd.get_next()}, prev{nd.get_prev()}, data{nd.get_data()} { }

template <typename T>
void double_node<T>::set_prev(double_node<T>* const nd) {
Expand Down

0 comments on commit 1644e40

Please sign in to comment.