shan said:
I used the following code in linked list in C.
typedef struct node
{
int data;
struct node *next; //more explanation needed for this line
}sll;
Is *next a variable to struct node,If yes then why it is not given
outside.
Each `struct node' instance contains two elements. The
element named `data' is an int, and the one named `next' is
a pointer. "A pointer" is not a complete description, because
it omits the type of the thing the pointer points to: You can
have pointers to char, pointers to double, and so on. In this
case, the `next' element points to a `struct node'.
Some people find this confusing, because it looks like a
`struct node' contains a `struct node', which itself would
contain a `struct node', and so on in infinite regression.
But that is not the case: `next' is not a `struct node', but
a pointer to a `struct node', which is quite a different thing.
Your house cannot contain another house which contains yet
another house, but your house *can* contain a slip of paper
with another house's address written on it.
Can anybody explain the need of typedef.I think it's not necessary .
It is not necessary. The typedef introduces `sll' as an
alias for `struct node', so you can write `struct node' or `sll'
interchangeably. Some people like the convenience of using a
meaningful alias, other people think it is a silly attempt to
hide the "struct-ness."