简体   繁体   中英

Singly Linked List c++

I'm trying to understand Singly Linked List by reading some codes and making sense out of it. This code, found on this site http://www.sanfoundry.com/cpp-program-implement-single-linked-list/ , has

struct node
{
    int info;
    struct node *next;

} *start;

I understand what int info and struct node *next does, but what does the extra *start mean after closing the brackets?

Thanks!

start is a variable of type struct node* - my guess is it is the head of the list.

If you find it easier to split the type and the variable:

struct node
{
    int info;
    struct node *next;

};

struct node* start;

Since this is C++ you can also simplify to node* start;

    struct node
{
    int info;
    struct node *next;

};

Think of the above as just a template for something you will be using in future.

So when you want to structure your data using the above struct you will need to define a variable or instantiate the struct in memory as shown below

Something like

struct node mynode;
or 
struct node* mynodeptr = new node;

To answer your subsequent question the above can be done wheresoever you need a node variable instantiated. So yes it doesn't have to be always done the way it was done in your original question.

As for typedef ing a struct there's a good discussion on why you would use it. Take a look here

It is a variable of type struct node. It is used as the head of the linked list.Many times reference to traverse in the linked list. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM