kapil sharma
kapil sharma

Reputation: 15

next pointer element in linked list structure

In the code

struct link *node
{
 int data;
 struct link *next;
};

Is next element pointer to pointer? And what does node=node->next do?

Upvotes: 0

Views: 2578

Answers (2)

alk
alk

Reputation: 70901

The following isn't valid C (it won't compile):

struct link *node
{
  int data;
  struct link *next;
};

You probably want:

struct link
{
  int data;
  struct link *next;
} * node;

Is next element pointer to pointer[?]

No, (if instantiated) next is a pointer to struct link.


what does node=node->next do?

It assigns to node where node->next would point to.

Upvotes: 1

Sildoreth
Sildoreth

Reputation: 1915

In your struct, the instance field next stores a pointer to the next link.

This line

currNode = currNode->next;

...makes the pointer currNode point to the node that follows the node that currNode previously pointed to.

The operator -> is the same as dot syntax but for pointers.

However, in the code you provided node->next wouldn't do anything because you do not have a variable named "node".

Also, the definition of you struct won't compile. It should be struct link, not struct link *node.

Upvotes: 0

Related Questions