lundi 26 août 2019

Declaring structure with itself?

I've begun studying data structures, and the linked list popped up. The idea behind a linked list is simple, but the implementation (I use c++) is a bit confusing, especially regarding the nodes used in linked lists. The way a node is defined for a singly linked list in C++ is as follows

// A linked list node 
struct Node { 
    int data; 
    struct Node* next; 
}; 

or if we were using classes, then it is defined like this.

class Node { 
public: 
    int data; 
    Node* next; 
}; 

My confusion arises here. How can one define another struct inside struct Node with the same name? I see that the new struct is a pointer to a node, but how does this implementation actually work? It's really confusing :(

Aucun commentaire:

Enregistrer un commentaire