0

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?

0

2 Answers 2

2

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.

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

2 Comments

why cant it be like nodeNext=(node *) malloc(sizeof(node *)); next=&nodeNext;
@Senthilnathan: Not really sure what you are trying to do, but seems much more complicated. Why do you want to make it more complicated? As I said, next already contains the address of the next node.
1

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.

2 Comments

why cant it be some thing like nodeNext=(node *) malloc(sizeof(node *)); next=&nodeNext; *next (will give next node's address) and *(next+1) (will give "value")
@Senthilnathan: do you fully understand the concept of pointers? I presume the programming language you are using is C. If yes, please refer to Chapter 5 (about Pointers and Arrays) of K&R book. And then refer to Chapter 6 (its about structures).

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.