What does NODE first = first-> link mean in linked list? - c

I want to know how a node* variable NODE can be assigned to the data inside the structure?
struct node
{
int info;
struct node *link;
};
typedef struct node* NODE;
//IN SOME OTHER FUNCTION
NODE temp,first;
temp = first;
first = first->link; // I WANT HELP WITH THIS LINE.

In a linked list you have a node with some information. In every node you have the address of the next node.
struct node
{
int info; //Information or data which is stored at every node
struct node *link; // Address of next node
};
first = first->link; // I WANT HELP WITH THIS LINE.
If you're done with current node then you may require to access the next node.
In this line you have accessed the next node (first->link) of the linked list
and you are making your next node as first (top node).

first is of type struct node *
link is also a struct node *
So you can assign one to the other and vice versa.
In this example the original value of first is stored in another variable temp and then first is replaced with what was the value of link in the original first. You could equivalently write
first = temp->link
instead of the last line, hopefully that will make it less confusing.

Related

How do I delete a doubly linked list in C?

I've created a doubly linked list, filled it with values and now I want to delete it and remove all the values to avoid memory leaks. Here's what I wrote as well as the structs that were used when creating the doubly linked list. Both those functions will be called towards the end of the main function.
struct node
{
struct node *next;
struct node *prev;
char *value;
};
// The type for a list.
typedef struct list
{
struct node head;
} List;
// The type for a list position.
typedef struct list_pos
{
struct node *node;
} ListPos;
void list_destroy(List *lst)
{
List p,q;
p = *lst;
while (p)
{
q = p.head->next;
free(p);
p = q;
}
*lst = NULL;
}
// Remove the value at the position and return the position of the next element.
ListPos list_remove(ListPos pos)
{
}
You appear to have the right general idea: you walk the list and free each node, making sure to grab any needed data from each node (in particular, the pointer to the next node) while the node holding it still exists. Your case differs from some that you might have seen, however, because instead of handling the overall list via a bare pointer to the head node, you have a separate object, of a separate type (List / struct list), to represent the list itself. This approach has much to recommend it, including, especially, the use of (apparently) a dummy head node, which provides for a variety of algorithmic simplifications. This is usually how I write a linked list.
But because struct list is not struct node, you cannot set a list pointer equal to a node pointer. Instead, create a struct node * to track your position. The first node to free would be the one referenced by struct node *to_free = lst->head.next, and the one after that would be the one referenced by to_free->next.
Note that you might need to free the struct list, too.

when implementing a list with C, I'm trying to remove the first element and preserves the remainder of the list

so I have it like this
struct NODE{
int val;
struct NODE *next;
} *list_head;
3 nodes have been correctly inserted into the list. And I want to remove the first
I tried
list_head->next = list_head;
and
list_head->next = list_head->next->next;
am I doing it wrong? or they are right and that's not the problem?
That would be:
// NODE previous_head = list_head;
list_head = list_head->next;
// free(previous_head);
Basically, you replace the head with the second node. De-allocate resources if needed.

Nodes as a pointer

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.

next pointer element in linked list structure

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.

How 'memory efficient doubly linked list' works?

In Data Structures and Algorithms Made Easy, struct of memory efficient memory list given as follows,
struct LinkNode
{
int data;
struct LinkNode* ptrdiff;
}
In ptrdiff, there will be xoring of previous and next node is done. For example, previous node have address 100 and next node address is 500.
So, in ptrdiff address will be 400. Now how it is possible to move to next or previous node (as we do in doubly link list), just by knowing xoring of their addresses?
Am I missing any step here?
The first node has no previous node, and the last node has no next node ... so think of the address of the node before the first and of the node after the last as 0. That's enough to start a traversal, and as you traverse you always have the address of the preceding node in hand so you can determine the address of the succeeding node. Here's an example of traversing such a list and printing the data ... pass the address of either the first or last node to printxorlist and it will print it either forwards or backwards:
void printxorlist(struct LinkNode* node)
{
struct LinkNode* prev = NULL;
while (node)
{
printf("%d\n", node->data);
struct LinkNode* next = (struct LinkNode*)((intptr_t)node->ptrdiff ^ (intptr_t)prev);
prev = node;
node = next;
}
}
Note that we have to cast node->ptrdiff because it doesn't really have the right type. Better would be to declare it correctly:
struct LinkNode
{
int data;
intptr_t ptrdiff;
}
then
void printxorlist(struct LinkNode* node)
{
struct LinkNode* prev = NULL;
while (node)
{
printf("%d\n", node->data);
struct LinkNode* next = (struct LinkNode*)(node->ptrdiff ^ (intptr_t)prev);
prev = node;
node = next;
}
}
In this type of linked list, you can not traverse from the address of any arbitrary node. Because you need some extra information either about predecessor address or successor address. But traversing from the first node(predecessor address =0 , since not any predecessor) or last node(successor address=0, since not any successor) is possible.

Resources