To preface this is from my first project in a data structures and algo course. I used C++ in first year but with exponentially "prettier" code and since then I've interacted heavily with Swift and Javascript. This code is legit jarring to look at for me and I'm getting frustrated because I can't figure out how to do the simplest of things.
The project is about Doubly Linked lists, the linked list class and nested Node class is
template <typename Type>
class Double_sentinel_list {
public:
class Double_node {
public:
//imutable reference to Type
Double_node( Type const & = Type(), Double_node * = nullptr, Double_node * = nullptr );
//Functions to return variables
Type value() const;
Double_node *previous() const;
Double_node *next() const;
//Variables
Type node_value;
Double_node *previous_node;
Double_node *next_node;
};
Double_sentinel_list();
Double_sentinel_list( Double_sentinel_list const & );
Double_sentinel_list( Double_sentinel_list && );
~Double_sentinel_list();
// Accessors
int size() const;
bool empty() const;
Type front() const;
Type back() const;
Double_node *begin() const;
Double_node *end() const;
Double_node *rbegin() const;
Double_node *rend() const;
Double_node *find( Type const & ) const;
int count( Type const & ) const;
// Mutators
void swap( Double_sentinel_list & );
Double_sentinel_list &operator=( Double_sentinel_list );
Double_sentinel_list &operator=( Double_sentinel_list && );
void push_front( Type const & );
void push_back( Type const & );
void pop_front();
void pop_back();
int erase( Type const & );
private:
Double_node *list_head; //looks like head/tail pointers are nodes as well..
Double_node *list_tail;
int list_size;
// List any additional private member functions you author here
// Friends
template <typename T>
friend std::ostream &operator<<( std::ostream &, Double_sentinel_list<T> const & );
};
The linked list constructor is written like follows:
//Constructor
template <typename Type>
Double_sentinel_list<Type>::Double_sentinel_list():
// Updated the initialization list here
list_head( nullptr ),
list_tail( nullptr ),
list_size( 0 )
{
list_size = 0;
//Creating Sentinal Nodes
//Node* used so double node type points to the address that holds the new double_node()
Double_node* headSentinal = new Double_node(); //new calls constructor for double node
Double_node* tailSentinal = new Double_node();
//Interconnecting Sentinals
headSentinal->next_node = tailSentinal;
tailSentinal->previous_node = headSentinal;
//Assigning head/tail pointers to sentinal nodes
//arrow operator used to access the object that the pointer of the object is pointing to
list_head->next_node = headSentinal;
list_tail->next_node = tailSentinal;
}
I believe I have a general understanding of what's going on but for some specific function calls I have no idea how to return the value back.
For example this class should just return the next node in the list
template <typename Type>
typename Double_sentinel_list<Type>::Double_node *Double_sentinel_list<Type>::Double_node::next() const {
// Enter your implementation here
return Double_node->next_node; ????
}
I cannot for the life of me, in this function declaration figure out what to call ->next_node on for the return value. The whole thing looks like mess of garbage if I look at it too long. Hell, how do I even call this function on a node?