Difficulty understanding the linked list implementation (structural part)?
struct node
{
int data;
struct node *next;
}*start;
I don't understand what it does struct node *next
, I know it points to the address of the next variable; but how about implementation? node *next
similar to struct node *next
?
What about *start
, does it look like struct node *start
?
I have a hard time understanding the implementation of linked lists.
source to share
I don't understand what it does
struct node *next
, I know it points to the address of the next variable; ...
It is right.
... but how about implementation?
The implementation is very different from how you described the behavior: a pointer is implemented as an object whose value is the memory address of the pointed object.
Is it
node *next
similar tostruct node *next
?
Not only similar, but almost identical. †
What about
*start
, does it look likestruct node *start
?
is he struct node *start
... See full announcement:
struct node /* structure definition */ *start;
This is a combined variable declaration and structure definition.
† The keyword is struct
simply used to disambiguate that the next identifier is in the namespace †† structure tags. In your case, this is redundant because there is no unstructured identifier Node
to disambiguate, and there are type names in C ++ struct tags as well.
What you are probably looking at is C code where the †† namespace specifier is required for structure tags.
†† In this context, I mean the C namespaces, not the C ++ namespaces.
source to share
How about
*start
he looks likestruct node *start
?
If you define a structure and put a name after it, you are declaring a variable for that structure. This is where you declare your variable start
, which is of type node*
(pointer to node). So the variable named start
is your first node.
I don't understand what it does
struct node *next
, I know it points to the address of the next variable; but how about implementation?
In your list contains data as int
and address of the next
node, as you said earlier.
In the implementation of this list, you will allocate memory for each new node and put the address of that node in the next
structure element . For a better understanding of linked lists, I would take a look here .
Therefore, I would recommend writing functions for insert, delete, etc.
If you are working with lists, object orientation can be helpful. But don't rush to dig into more complex code.
source to share
Your code really looks like a piece of C code, not C ++. In C ++, you probably never write your own list implementation, but use std::list
or std::forward_list
. If you do this it will look like
class list
{
struct node
{
type data; // type could be a template parameter
node*next=nullptr; // set next to null by default
};
node*head=nullptr; // list is empty by default
public:
/* ... */
};
In particular, the struct
c keyword is struct node*next;
not required, and the pointer start
can be declared on a separate line for clarity.
source to share