rose February 2016

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!

Answers


John3136 February 2016

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;


Prab February 2016

    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

Post Status

Asked in February 2016
Viewed 2,025 times
Voted 6
Answered 2 times

Search




Leave an answer