-3

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?

Alex Barbulescu
  • 331
  • 4
  • 14
  • How about `return next_node;` ? – M.M Sep 15 '18 at 00:25
  • 2
    If you're relatively new to C++ (which, given your question, I would judge you to be) and your instructor expects you to learn by yourself, I would urge you to start with a beginner-level [C++ Book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Take it from me personally, learning C++ by trial and error is a bad way to go. There are very many pitfalls and easy subtle mistakes that a good book, good convention, and reading documentation will help you avoid. – alter_igel Sep 15 '18 at 00:34
  • If you don't have a fair understanding of the syntax and semantics of pointers, references, classes, and templates, then to answer your question would be to do your homework for you and rob you of your learning experience. I would encourage you to go to your instructors for help if you feel lost. – alter_igel Sep 15 '18 at 00:38
  • 2
    The current object is in `this`, so `return this->next_node;`. C++ even lets you omit `this->` if the code is unambiguous. – melpomene Sep 15 '18 at 00:38

1 Answers1

1

C++ beginner here (still learning myself), but I can give you some advice and hopefully someone with more experience can help you as well.

Double_node* tailSentinal = new Double_node(); is creating a pointer to a Double_node.

Templates

Now onto the templates, so template <typename Type> Double_sentinel_list<Type>::Double_sentinel_list(): is a template that allows you to create a new instance of Double_sentinel_list with a type eg int, float, so you can use it like Double_sentinel_list<int> myList; or so.

As for why the head and tail are Double_node's is because this template is a template for a doubly linked list. I've included a small explanation of a doubly linked list below.

From Wikipedia, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes.

Doubly linked list on Wikipedia: https://en.wikipedia.org/wiki/Doubly_linked_list

dhilln
  • 56
  • 3