View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12920 Members
Join this group to post and comment.

Linked Lists Questions

This is the basic declaration we do while working on linked lists.

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

why is the next declared as a pointer? and not as a just

struct node next; ??


I will start with basic and then go for advanced questions.
aarthivg
aarthivg • Mar 14, 2012
English-Scared
This is the basic declaration we do while working on linked lists.

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

why is the next declared as a pointer? and not as a just

struct node next; ??


I will start with basic and then go for advanced questions.
linked list consist of two parts data and reference to the next node
True, but the reference can also be accessed by normal variables using logic!

and think in this perspective, is it valid if I declare

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

If valid why dont we use, If invalid why is it invalid?
aarthivg
aarthivg • Mar 14, 2012
The field of each node that contains the address of the next node is usually called the next link or next pointer.

So reference cant be used as a normal variable.
OK the answer I was expecting is,

if we donot use a pointer, and declare a variable of struct node inside struct node, then compiler donot know how much memory to allocate to that variable as the parent structure declaration is not complete yet.

if we use a pointer, then compiler knows that , its a pointer and it stores address of something . so it allocates memory.
greatcoder
greatcoder • Mar 16, 2012
Here Because next is not a structure itself , it is a pointer to the next node in the linked list.
great Coder : Please elaborate!!

Share this content on your social channels -