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.

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir for the answer.Helped me alot
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.

2 Comments

Thanks brother.Helped me a lot.
Would be nice if you accept one of the given answers or ask for more informations.
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.

1 Comment

Can i write node *next instead of struct node *next?
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.

2 Comments

Could you kindly tell me the difference between NULL and nullptr ?
NULL is a C macro and should be avoided in C++. C++ has the keyword nullptr that should be used instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.