Use of struct keyword inside of struct - c

typedef struct node
{
int n;
struct node* next;
}
node;
In the above code there is struct node* next inside the struct node. I don't understand the meaning of it.

In the code there is struct node* next inside the struct node. I
don't understand the meaning of it.
Explanation :
A node is a general term used to refer a self referential structure.(click on link to know more)
and going by definition,
A self referential structure is a typical structure whose definition has one or
more of its member as a pointer to its own type.
Here in your code next is such a member of the structure struct node which is a pointer to it's own type therefore you see it as a pointer to itself in this manner inside of struct :
struct node* next;
That's the reason why struct keyword is used inside of struct
Purpose of using self referential structures :
self referential structures are very much helpful in building data structures such a linked lists... and in fact the variable next is the very member which is useful in making links between nodes in a linked list :)

struct node* (note the *) is a pointer (*) to a struct node. That means that one node can connect to another, via the pointer, without including the other.
This is referred to as a "linked list" structure. I found a Stanford University tutorial that looks like it covers the subject fairly thoroughly.

In simple words, it means that next would also be of the same structure as that of node. So next will also have a field int n and another next.. Also this is dynamic so you don't have to allocate any memory before hand.. you can keep on adding as many node as you want ( you still have to assign memory to each node though -- search for how to use malloc to allocate memory to struct node )
And as it is of pointer type ( struct node* next ) it means you can store the address of your next node in to it and link nodes..
See the linked list implementation for better understanding..
I hope it helps :)

Related

What is the differences in these declarations?

I am learning about uses of data structures and came across some doubts:
struct node
{
int data;
struct node *next;
}*head;
Question1: In the above structure, what does declaring a struct node *next within the structure mean?. And how is it different from simply declaring a pointer variable within structure as int *next
Question2: We can see that, at the end of the structure definition, a pointer *head is declared of type node and is used to access the structure members if I am right. Is this same as declaring it like this:
struct node *head;
Any help with this would be great. Thank you all in advance.
"struct node *next" declares a variable called next which points to a "struct node". In other words, a singly linked list.
You are correct in that the statement does two things:
Declares a struct called node (with an int called data and a
pointer to the next struct node)
Declares a variable called head pointing to the struct node.
These could have been done separately as follows:
struct node
{
int data;
struct node *next;
};
struct node *head;
This is very commonly how Linked Lists are declared in C. Recall — a linked list is a series of nodes which contain data as well as a link (in this case, a pointer) to the next node in the series.
So, each object of type struct node should contain a pointer to another struct node. This is characterized by declaring a field next in the struct with type struct node *.
As for your second question: the code you provided both defines struct node (allowing you to instantiate objects of type struct node), and declares a pointer to such a struct called head. You could certainly define the variable head the way you asked, but you would still need to define the struct somewhere earlier in the code. This is usually done in a separate header file, but could be done in the same .c source code file for brevity.
Now, note that both these methods will not actually create a struct node object. All you're doing is declaring a pointer to one. The object doesn't yet exist anywhere since you never allocated space for it. To do that, you'd need to use malloc:
struct node {
int data;
struct node * next;
} * head;
head = malloc(sizeof(struct node));
You could also declare a runtime stack instance of the struct, which just means you won't need to explicitly free the memory you allocated with malloc(). The memory allocated for the struct is deallocated when you return from the function, or when your program terminates if declared outside of any functions.
Declaring next as struct node *next means that it points to a struct node, presumably the next node in the list. If it were instead declared as int *next then it would point to an int, which it doesn't seem there's any need for.
The answer to question 2 is yes. They are simply combining the definition of struct node with the declaration of head. They could be split apart as you indicated.

What is the difference between these two pointers

In linked list I came across the following structures
struct node
{
int data;
struct node *next;
};
struct node *list;
What is the difference between list and next?
They are both pointers to the struct node.
But the difference is that next is a member of the struct while list is an object.
That means that you can use list as
list->data;
But to use next, you need to use
list->next->data;
or
struct node a;
a.next->data;
That is, it behaves like needs an object just like any other members of the struct.
You can refer this code
> There is no difference between these Two Pointers both are refrences
and store the adress of the object.
but the *next behaves as the variable of the struct and the other pointer behaves as an object of the Struct.
1st of all in the snippet shown there only "exists" one pointer, that is list.
However:
list is an instance of a pointer pointing to struct node. It uses memory.
next is a member of, part of the definition of the type struct node and of type pointer to struct node. next is no instance of a pointer. It does not use memory.
The Both pointers list and next are pointing to node structure.
The Main Difference is:
list is not part of the structure node, where as next is part of the structure node.

Structures and typedefs and pointers in C

I am trying create a doubly linked list in C and I have a question regarding the code below:
typedef struct Node{
double coeff;
int exp;
struct Node* next;
struct Node* prev;
}Node;
My question:
In line(5) above; Is it correct to say that I am creating a pointer variable called next? And this variable is of type Node? This variable will point to the address of the next Node stored in memory?
Is the Node* the same as Node *pointer?
Thanks for the help; I want to get my foundation solid before I progress further.
In line(5) above; Is it correct to say that I am creating a pointer variable called next?
You have defined a type struct Node with a member named next( and others ).
And this variable is of type Node?,
next has a type struct Node*
This variable will point to the address of the next Node stored in memory?
If you assign it a correct value then it will.

Linked-list is confusing

I am stuck, as I don't understand what is this code doing:
struct node
{
int info; /* This is the value or the data
in the node, as I understand */
struct node *next; /* This looks like a pointer. But
what is it doing in real life? */
} *last; /* What is this and why is it
outside the body? What is this
thing doing? */
I know that when a node is created, it has a value, and it is pointing to some other node
but I don't understand the syntax.
Is this a better way of writing the code above?
Is there a simpler way of writing the same struct for better understanding?
In my lectures they presume that the student has understanding of what they teach.
Well, we can explain this to you, but we can't understand it for you.
Code snippet you've provided is definition of variable last, being pointer to newly defined structure type node. It can be written other way as:
typedef struct _node_t {
int info;
node_t *next;
} node_t;
node_t *last;
This way we define typedef, which is, say, alias of type definition to some short name — in this particular case, it aliases structure of two fields as the name node_t. Wherever you define something as being of type node_t, you tell compiler that you mean 'this should be aforementioned structure of two fields', and node_t *last means 'variable last should be pointer to node_t type'.
So, back to syntax:
struct foo {
int a;
float b;
void *c;
} bar, *baz;
means 'Define structure type foo, and make it contain three fields — integer a, float-point b and untyped pointer c, then make variable bar to be of this structure type, and make variable baz to point to this structure type'.
Now to pointer. What you see is called 'recursive definition', e.g. type mentions itself in it's own definition. They are okay, if language supports them (C does), but one could avoid recursive definitions in linked list node structure by specifying next node pointer to be just untyped:
struct node_t {
int info;
void *next;
};
This way you no longer reference node_t type from node_t type, but that adds you some inconveniences when using this type (you have to explicitly cast next to node_t type, like ((*node_t)(last->next))->info instead of just last->next->info).
If you feel you need additional reference, consider taking a look at interactive online tutorials, like http://www.learn-c.org/ (I'm not affiliated).
that is the simplest way to write a linked list node , but why name it last ? name it node instead , this makes it more understandable , but here's how it works.
when a linked list is first created it contains only the root node (the first node in a linked list) , when you add a node , you fill the info field with the data that node will hold (note that info may be any kind of data , character , string , int ...) then set next to NULL , since that node is the last node in the list.
when you add another node , you change the value of next to point to the node you just added and you set the value of next to NULL in the node you just created because now that is the last node in the list .
you can repeat this to add as many nodes as your memory allow you to.
this link may help you to better understand structures
typedef struct marks {
int m;
struct marks *next;
} marks_t;
This way we define a structure so that a Linked List can be formed. Now we have defined the last variable next as a "structure pointer" which gives us the address of the next element in the list (i.e. as structure only)!
The last element does not point to any node (here marks structure) and hence the pointer variable has NULL value.
Now to define the first element:
marks_t *list;
if (list == NULL) {
list = (marks_t *) malloc(sizeof(marks_t));
}
list->m = 15;
list->next = NULL;
Now if we want to add an element next to this (i.e. second element):
marks_t *next1;
next1 = (marks_t *) malloc(sizeof(marks_t));
next1->m = 27;
next1->next = NULL;
list->next = next1; // Storing address of next1 structure in list

typedef confusion in C

I'm having a hard time understanding the typedefs in this C structure.
typedef struct node {
int value;
list rest;
} node;
typedef struct node *list;
What's the difference between the "node" typedef declaration and the "list" declaration? Why is list prefaced as a pointer? Isn't "node" a pointer as well? Why can't I simply say "typedef struct node list" and omit the asterisk? I've been searching everywhere and I can't really find a satisfactory answer.
The first typedef defines node as an alias for struct node, to allow you to refer to it simply as node without writing struct node every time (in C "regular" type names and struct names live in two different namespaces). It's equivalent to:
struct node
{
int value;
struct node* rest;
};
typedef struct node node;
The second typedef, instead, defines list as an alias for node *, i.e. defines the type list as a pointer to a node structure.
(by the way, personally I find that this is very bad style: hiding pointers inside typedefs is almost always a bad idea; one can argue that a pointer to the first element in a list could be identified as the list, but the usage of list even for the rest pointer is IMHO not very nice)
node is not a pointer; it is a struct. list is a typedef for a pointer to node.
You typedef a struct in C to avoid typing struct node ... everywhere.
Whether or not this is good practice is questionable (most will agree that hiding a pointer type behind a typedef, unless it is truly opaque, is a bad idea), but that's the gist of it.
The first declaration says that a node is the structure. The second says that a list is a pointer to a node.
So, this would be proper code using those declarations:
list x;
node n;
x = &n;
Declaring an item using typedef struct node list is not correct. The typedef statement declares a new type. The keyword typedef is not part of the type's name.
node is a struct, which is (confusingly) named the same thing as struct node. list is a pointer to struct node. So the following are equivalent:
struct node *a;
node *a;
list a;
node is just a synonym for struct node
list is a pointer to a struct node (or node)
Each node instance contains a list pointer which enables you to build data structures such as (singly) linked lists, etc.

Resources