In the code
struct link *node
{
int data;
struct link *next;
};
Is next element pointer to pointer?
And what does node=node->next do?
The following isn't valid C (it won't compile):
struct link *node
{
int data;
struct link *next;
};
You probably want:
struct link
{
int data;
struct link *next;
} * node;
Is next element pointer to pointer[?]
No, (if instantiated) next is a pointer to struct link.
what does node=node->next do?
It assigns to node where node->next would point to.
In your struct, the instance field next stores a pointer to the next link.
This line
currNode = currNode->next;
...makes the pointer currNode point to the node that follows the node that currNode previously pointed to.
The operator -> is the same as dot syntax but for pointers.
However, in the code you provided node->next wouldn't do anything because you do not have a variable named "node".
Also, the definition of you struct won't compile. It should be struct link, not struct link *node.
Related
I was making a linked list in C, then a query raised in my mind that (read the title above)
struct node
{
int data;
char age;
} temp;
// versus
struct node
{
int data;
struct node* next;
} *temp;
A struct like node cannot contain itself. If this were allowed, each struct node variable would be infinitely sized.
struct node* next; is a pointer to a struct node value. A pointer has a known, finite size and thus a struct can contain a pointer to another value of the same type. By having a pointer to a next struct node you are creating a linked list. This pointer can also be NULL which allows your list to have an end.
Why the nodes in a linked list are declared as pointers? Nodes contains the pointer part in it to link to another node. Then why the nodes are itself a pointer?
struct node
{
int data;
struct node *link;
} *start;
Now we introduce nodes as
struct node *tmp;
Now this is a node which is a pointer to data type struct node..but for linking we use the link pointer to link the other node
Why dindnt we coded node as
struct node tmp;
only...is this because of allocating dynamic memory..or something more?
Yes, this is because the nodes are allocated dynamically.
struct node tmp could be using tmp as a dummy or sentinel node, where it's data is not used, only it's next pointer to the actual first node of a list.
struct node *tmp would be using tmp as a pointer to the first node of a list or as a working pointer to some node in a list when scanning a list.
In some cases a list structure is also used:
struct list{
struct node *head // pointer to first node
struct node *tail // optional: pointer to last node
size_t size; // optional: number of items in a list
}
A circular list could be implemented using just a tail pointer to the last node, which in turn would have a next pointer to the head/first node.
I am new to pointers and there is this code for merge sort of linked lists. And here it has declared a dummy node as struct node dummy; and the next node for dummy node is NULL so to set it we use dummy.next = NULL;.
/* Link list node */
struct node
{
int data;
struct node* next;
};
struct node* SortedMerge(struct node* a, struct node* b)
{
/* a dummy first node to hang the result on */
struct node dummy;
/* tail points to the last result node */
struct node* tail = &dummy;
/* so tail->next is the place to add new nodes
to the result. */
dummy.next = NULL;
//Code continues...
}
I understand that i can use it if it was struct node *dummy;
but we cant use it here as it is not a pointer node.
So my question is why doesn't dummy->next = NULL work here?
and what is the difference between struct node and struct node* ??
a -> b is shorthand for (*a).b.
If a is not a pointer, *a is not valid, and neither is a -> b.
dummy is not a pointer to a structure. It is the structure variable itself.
You can derefer attributes of a structure with the operator -> only if it is a pointer to the structure.
If you are using the struct variable, then . is the way to go about, which is very much the case with dummy.
I understand that i can use it if it was struct node *dummy;
If by "it" you mean struct node dummy; then the answer is no. You cannot use a pointer to node in the same way as a pointer to node.
So my question is why doesn't dummy->next = NULL work here?
Because dummy is a node, not a pointer, and operator -> if for pointers. The expression dummy->next has the same semantics as (*dummy).next.
. So my question is why doesn't dummy->next = NULL work here? and what is the difference between struct node and struct node* ?
Declared as this struct node dummy;
dummy->next=NULL doesn't work because dummy is not a pointer to struct .
If you write so -
struct node A; // then A is a struct variable which can access struct members using '.' operator
and this -
struct node* B; // then B is a pointer to struct node which can access struct member using '->` or like this (*B).data=something.
SO, I'm trying to write a generic singly linked list implementation that works with all types, and keep coming across the error of: Assignment from incompatible pointer type for the line of code that is as follows:
node->next = newNode;
This is in the context of the following declarations and structs:
typedef struct{
void* data; // The generic pointer to the data in the node.
Node* next; // A pointer to the next Node in the list.
} Node;
void insertAfter(Node* node, Node* newNode){
// We first want to reassign the target of node to newNode
newNode->next = node->next;
// Then assign the target of node to point to newNode
node->next = newNode;
}
I have tried to use both this: node->next = *newNode; and this: node->next = &newNode; But as you can imagine, they do not work, what am I doing wrong here, what and why is this error being caused and how do I fix it?
Change the definition of your struct from
typedef struct{
void* data; // The generic pointer to the data in the node.
Node* next; // A pointer to the next Node in the list.
} Node;
to
typedef struct Node Node;
struct Node {
void* data; // The generic pointer to the data in the node.
Node* next; // A pointer to the next Node in the list.
};
Reason being that you can not reference the typedef inside your struct until the typedef is complete.
Not sure why you thought the other line was the issue. It was not.
I get this weird side effect while using operator '->' in code I wrote in C. The pointer which I used -> on , is changed to have some garbage.
More specifically:
I have the following structs:
typedef void* ListElement ;
typedef struct List_t* List ;
typedef struct Node_t* Node;
Struct Node_t {
ListElement data ;
Node next;
}
Struct List_t {
Node* head;
Node* current
}
when I use the following ListGetFirst(), I get wired behavior :
ListElement ListGetFirst(List list)
{
if( list == NULL || list->head==NULL)
{
return NULL;
}
list->current=list->head;
Node* head =list->head; // here is the problem
ListElement data = (*head)->data;
return data;
}
when I used debugger I figured out that the pointer list->head is changed on the marked aformentioned line .
I realy have no idea why, and I didn't knew that '->' can have side effect
thanks in advance
Are you sure this is exactly what you want to do?
typedef struct Node_t* Node;
Node* head =list->head;
Since you defined Node as a pointer to Node_t, shouldn't you be doing:
Node head =list->head;
EDIT:
To summarize the whole thing, I think this typedef is misleading you:
typedef struct Node_t* Node;
It would made more sense if it were simply:
typedef struct Node_t Node;
Gah, pointers hidden behind typedefs; unless the type's meant to be totally opaque, that's almost always bad juju. For my benefit, I'm going to take out the typedefs so I have an easier time seeing what you're really playing with.
struct Node_t {
void *data ;
struct Node_t *next;
};
struct List_t {
struct Node_t **head;
struct Node_t **current;
};
void *ListGetFirst(struct List_t *list)
{
if( list == NULL || list->head==NULL
{
return NULL;
}
list->current=list->head;
struct Node_t **head =list->head; // here is the problem
void *data = (*head)->data;
return data;
}
I got nuthin'. Types all appear to match up. The -> operator most emphatically does not have any side effects; all it does is dereference a pointer. The extra level of indirection for head and current in struct List_t is a head-scratcher, and it makes me wonder if they're being allocated or assigned correctly. All I can figure is that list->head isn't pointing to memory that you actually own, and is getting overwritten somehow when you reach that point (IOW, you've invoked undefined behavior somewhere else in your code).
In short, the problem isn't in the code that you've posted. It's probably where you allocate and assign list elements.
You are using pointers to pointers, where most likely you want pointers.
In List_t you define head as Node*, where Node is already a Node_t* .
hth
Mario