Lolly
Lolly

Reputation: 36382

structure implementation for linked list

When I was reading about Linked list, I came to know that the structure for linked list as

Struct node{           
   Struct node *next;  
   int value;  
}

Why is the Struct node *next? Why cant it just be an integer pointer? Like below

Struct node{   
   int *next;  
   int value;  
}

why can't this hold the next node's address? Can anyone please give me explanation?

Upvotes: 0

Views: 259

Answers (2)

Steven Jeuris
Steven Jeuris

Reputation: 19100

Why is the Struct node *next? Why cant it just be an integer pointer?

Because then you would be pointing to the address of the next integer. This could be used to retrieve the value of the next int, but nothing more, so from there on onward, you would be stuck.

By linking nodes together, which hold integer values, you can traverse the nodes and retrieve the int values.

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816302

That's actually what a pointer (*next) does. It holds the address of something else. The type definition describes what this something else is (in this case struct node). Otherwise the application would not know how many bytes to read and how to interpret the data.

Read more about pointers.


Btw. int *next would hold the address of an integer.

Upvotes: 2

Related Questions