3
struct node
{ 
    int data;
    struct node *next;
}*start;

I don't understand what struct node *next does, I know it points to the address of the next variable; but how as in implementation? Is node *next similar to struct node *next?

And what about the *start is it similar to struct node *start?

I am finding very difficulty understanding linked lists implementation.

DavidG
  • 24,279
  • 14
  • 89
  • 82
AdiL IsmaiL
  • 158
  • 1
  • 10

4 Answers4

3

I don't understand what struct node *next does, I know it points to the address of the next variable; ...

This is correct.

... but how as in implementation ?

The implementation is pretty much exactly how you described the behaviour: A pointer is implemented as an object whose value is the memory address of the pointed object.

Is node *next similar to struct node *next?

Not only similar, but pretty much identical.


And what about the *start is it similar to struct node *start?

It is struct node *start. See the complete declaration:

struct node /* structure definition */ *start;

It is a combined declaration of a variable, and definition of a structure.


† The struct keyword is just used to disambiguate that the following identifier is in the name space†† of structure tags. In your case it is redundant, because there are no non-structure identifier Node to disambiguate, and in C++ structure tags are also type names.

What you're probably looking at is C code, where the name space†† specifier is mandatory for structure tags.

†† In this context, I refer to C name spaces, not C++ name spaces.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

And what about the *start is it similar to struct node *start?

If you define a struct and put a name after it you declare a variable of that struct. Here you declare your start variable which is of the type node* (pointer to node). So the variable named start is your first node.

I don't understand what struct node *next does, I know it points to the address of the next variable; but how as in implementation?

Your list holds the data as int and the address of the next node as you said before.

In an implementation of that list you would allocate memory for each new node and put the address of that node into the next member of the struct. For more understanding of linked lists I would look here.

Therefore I would recommend to write functions for inserting, deleting, etc.

If you work with lists object orientation might be helpful. But take your time to understand more and more complicated code.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
1

A node needs to be pointing to next node. So it has a pointer variable named next. Since next is also a node struct you declare it as struct node *next;. start is your first node.

Jonas
  • 6,915
  • 8
  • 35
  • 53
sithereal
  • 1,656
  • 9
  • 16
1

Your code really looks like a piece of C, not C++ code. In C++ you would presumably never write your own list implementation, but use std::list or std::forward_list. If you do, it would 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 keyword in struct node*next; is not necessary and the start pointer can be declared on a separate line for clarity.

Walter
  • 44,150
  • 20
  • 113
  • 196
  • Could you kindly tell me the difference between NULL and nullptr ? – AdiL IsmaiL May 24 '17 at 10:24
  • [`NULL`](https://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0) is a C macro and should be avoided in C++. C++ has the keyword [`nullptr`](http://en.cppreference.com/w/cpp/language/nullptr) that should be used instead. – Walter May 24 '17 at 10:27