rose
rose

Reputation: 667

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!

Upvotes: 2

Views: 733

Answers (3)

Abhishesk sharma
Abhishesk sharma

Reputation: 11

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.

Upvotes: 0

pcodex
pcodex

Reputation: 1940

    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 typedefing a struct there's a good discussion on why you would use it. Take a look here

Upvotes: 1

John3136
John3136

Reputation: 29266

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;

Upvotes: 6

Related Questions