2

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!

rose
  • 657
  • 2
  • 10
  • 20

3 Answers3

6

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;

John3136
  • 28,809
  • 4
  • 51
  • 69
  • ah ok thanks. I'm assuming we can put that somewhere else correct? Where else can we insert that instead of there? It seems a bit out of place to me and makes it a bit confusing... – rose Feb 09 '16 at 00:41
  • 1
    Since this is tagged C++ the typedef is not required. Nor is the second struct. – Martin York Feb 09 '16 at 00:50
1
    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

Community
  • 1
  • 1
pcodex
  • 1,812
  • 15
  • 16
  • oh I see thanks! Im having a bit of a hard time determining where to put what? Would it be appropriate to set that up in the public section of single_llist? – rose Feb 09 '16 at 03:19
  • @rose I'm not entirely sure what you mean by public section of the `single_llist`. Structures are usually declared completely in header files if you plan on using them across several source files of a project. However you could both declare and instantiate within a single source file too, keeping the struct localized to just that one source file. This depends on the requirements of your implementation. – pcodex Feb 09 '16 at 04:17
0

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.