Linked-list is confusing - c

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

Related

typedef struct declaration gives back an error

I do not understand what is wrong with the following piece of code. I am trying to create a linked list in C. I am creating a typedef struct which I call a person, then I declare a pointer that points to that structure and I am trying to allocate some memory for it to be able to store all its components. The compiler gives back an error saying that 'head' does not name a type.
typedef struct node {
int num;
struct node *next;
} person;
person *head = NULL;
head = (person*)malloc(sizeof(node));
Assuming the assignment to head is in a function, it is still incorrect as node is not a valid type or variable. It's either struct node but as you typedef'd that you should use person
head = malloc(sizeof(person));
But as the variable head is already of type person* you can also do
head = malloc(sizeof(*head));
which has the advantage you no longer need to know the exact type name (should you ever change it)
Also note that casting the result of malloc is not needed and unwanted.
You will have to check for the result being NULL though.

Can you typedef a previously typedef'd statement?

I was assigned to work on some code, but I was given part of it beforehand. It includes this part of code:
typedef int ElementType;
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node {
ElementType Element;
Position Next;
Position Previous;
};
The way I understand it is,
Instead of writing int we now can write ElementType to define a value as an integer. (Confusion #1)
We have defined a structure called Node. (I am unsure why it wasn't defined here).
We typedef PtrToNode as a pointer of the structure called Node.
We typedef List as PtrToNote. Which should mean that List is also a pointer to the structure called Node. This pointer is called List. (Confusion #2)
Similar to number 4, but this one is called Position.
We define how the structure is going to be set up. In this case Element is supposed to be an int, as per our previous definition. Next and Previous are supposed to be pointers of Position? However, if I were to change Position with List, how would this affect the code?
Can someone please explain to me points #1, 2, 4, and 6? Am I understanding the code correctly?
Instead of writing int we now can write ElementType to define a value as an integer. (Confusion #1)
This is correct - for example:
int a = 50;
Is now equivalent to
ElementType a = 50;
We have defined a structure called Node. (I am unsure why it wasn't defined here).
This is (forward) declaring a structure called Node, not defining a structure
We typedef PtrToNode as a pointer of the structure called Node.
Your understanding here is also correct. Thus the following are equivalent:
struct Node *x;
PtrToNode x;
We typedef List as PtrToNote. Which should mean that List is also a pointer to the structure called Node. This pointer is called List. (Confusion #2)
List and PtrToNode represent the same type - the following examples all do the same thing:
struct Node *x;
PtrToNode x;
List x;
Similar to number 4, but this one is called Position.
Correct.
We define how the structure is going to be set up. In this case Element is supposed to be an int, as per our previous definition. Next and Previous are supposed to be pointers of Position? However, if I were to change Position with List, how would this affect the code?
This is defining the structure Node. Replacing Position with List would have no effect on the code. To two types are compatible.

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.

Define a struct with a member pointing to another member

I'm trying to program a network in C. I have nodes which are linked to each other and I 'd like to do that by making the struct member point to another member (not to another node, because I want to preserve the identity of the links).
The code I made to do that is something like:
struct node{
int k; //number of links
struct node.link **link; //<- wrong
};
but this is not right as node is not a variable but a type of variable (this is already discussed as an error in another QA: first you have to define a variable of node type and then apply the .link, but this doesn't help here). There's also a QA called "Struct member point at another struct member" but they don't do it from definition and it is not so clear how to generalize it (at least for me).
Is it a correct way to do this?
The problem is that the C language doesn't let you create the type you want. You need a type T with the property *T has the same type as T. You can't do that. (Well, function pointers have that property, but that's an irrelevant technicality.)
You have to introduce a new name. C only lets you do this with structs or similar constructions.
struct link {
struct link *ptr;
};
struct node {
int k;
struct link *link;
};
This will get you what you want. Now, in order to go from a struct link * to a struct node *, you'll have to do some pointer math:
struct node *node_from_link(struct link *link) {
return (struct node *) ((char *) link - offsetof(struct node, link));
}
This is also provided by the container_of macro, which is not part of the C standard, but you can find a definition for it online.
Or, you could just go the traditional route.
// Usually easier to do it this way...
struct node {
int k;
struct node *link;
};
Is this what you are after?
struct Node
{
int k; //number of links
void* link;
};
struct Node* create()
{
struct Node* node = malloc(sizeof(struct Node));
node->k = 0;
node->link = 0;
return node;
}
void link(struct Node* from, struct Node* to)
{
from->link = &(to->link);
}
int main()
{
struct Node* child = create();
struct Node* parent = create();
link(parent, child);
return 0;
}
I've used void* for the link for the reason expressed by Dietrich: you want a pointer to the link to be the same type as the link. This effectively means a cast, so why not just use a generic pointer?
Membership in a structure, generalized or specific, is not an attribute of C data types. There is therefore no way to declare a pointer that can only point to a structure member, and not to any other variable of compatible type.
On the other hand, you don't need to do anything special to declare a pointer that can point to a member of another structure. You just need a pointer to that member's data type, and structure membership is irrelevant to that data type.
For example, you can have
struct node {
int k; /* number of links */
struct node **links; /* points to a dynamic array of node pointers */
struct node **one_link; /* points to a node pointer from another node */
};
In that case, it might make sense to do something like this:
struct node *n1 = /* ... */;
struct node *n2 = /* ... */;
n2->one_link = &(n1->links[3]);
Overall, though, I think this is kind of convoluted. There is probably a better way to structure your data.
Update:
Based on your description of what you're after:
[...] links are bidirectional, if I destroy one link (say the one that links node 1 to node 3) I'll need to destroy the node 1 link AND the corresponding link from node 3. Then I need to know more than just who is link to who. I need to know which link they are using.
there are at least two possible solutions, depending on details of how your nodes are structured. If they are structured like I show above, with an array (dynamic or not) of pointers to other nodes, then your general idea simply won't work. That's because the position of each link within an array of links will change as you delete other links (supposing that you close the gaps). Instead, you can just scan:
struct node {
int k; /* number of links */
struct node **links; /* points to a dynamic array of node pointers */
struct node *parent; /* points to a node that links to this one */
};
void delete_node(struct node *n) {
if (n->parent) {
int i;
for (i = 0; i < n->parent->k; i += 1) {
if (n->parent->links[i] == n) {
/* ... delete the ith element of n->parent->links ... */
break;
}
}
}
/* ... clean up node n ... */
}
If one node's links to others are stored in separate members, on the other hand, then you could indeed provide a double-pointer by which to remove links from the parent, but the presence of member k in your original structure tells me that's not your situation.
Ok, this is how I finally solved it in my program:
typedef struct node{
int k; //connectivity
struct link **enlace; //vector of LINKs
}NODE;
typedef struct link{
NODE *node1;
NODE *node2;
}LINK;
Basicly, I defined two structures: one is the NODE type, which contains the information of how connected is the node and a vector of LINKs, and the other is the structure LINK which contains the information of the link itself, I mean which nodes the link connects.
With these two I'm able to create the network of nodes with a connectivity following a Poisson distribution, and then destroy each link one by one, choosing one link at random from a list and then redirecting the pointers of each node to NULL.

Declaring structs in C

struct node
{
int data;
struct node *next;
}*head;
Why do we have *head? And is this different (better?) than doing
typedef struct
{
int data;
struct node *next;
}head;
The first section defines a variable head which is a pointer to a struct node type. It can be NULL, indicating that your linked list has zero elements.
The second block just declares a type called head. It's not a variable at all. And it does not compile as the type of its next field, struct node, does not exist.
You probably wanted
typedef struct node {
int data;
struct node *next;
} node;
node *head;
This form declares 2 types, struct node and node (the same), and defines a variable head. I'd go for the 1st form without the typedef, as it's more simple, and you cannot refer to the typedef inside the struct's next field anyway.
The first version declares a type (struct node), and a variable head which is a pointer to struct node. All lists need a head, so that's helpful.
The second declares head as the type name for an (otherwise unnamed) struct. It won't actually compile, since the inner struct node *next refers to a type which doesn't exist.
In your first example, you define the struct but also declare the head variable to be a pointer to such struct.
In your second example (typedef) you just declare some type synonym (but
no variables!)
struct node
{
int data;
struct node *next;
}*head;
in the code above you have declare object head as a pointer so in order to access its element data you need to use -> operator that is for example head->data=4; and so on
you can create nodes to append the head pointer
typedef struct
{
int data;
struct node *next;
}head;
where as in this code every node that you create will be of head type it will create a problem when you delete the head node if you write linked list code for deleting head you are unable redefine the new head
where as in earlier code head being a pointer can be change by updating its next element in struct after updating the head->next in previous code we can then easily delete the previous head without losing the new head
the gdb debugger (and perhaps other debuggers)
can only see the contents/format of a struct that contains a tag name.
The use of a typedef hides the tag name and defines a new type.
however, gdb will not properly display the contents of any instance of the struct.
The correct way is:
struct tagname { ... };
Notice no struct name to clutter the symbol table and create confusion
Of course, then each defining instance of this struct needs to be written as:
struct tagname myName;
but that should not be a problem and adds the visible documentation
that myName is a struct
Along with the increased visibility of the struct, both to gdb and the reader
of the resulting code, any pointer to that struct should never be defined
as part of the struct definition. Such a formatting hides the
fact that such name is a pointer. rather declare an instance of a pointer as:
struct tagname * pMyStruct;
And, never use this syntax:
struct { ... } structName;
as that is a depreciated syntax

Resources