I am a new learner and trying to build linked list. I am able to do so but I am trying to keep the pointer of root or first node so after building linked list I can read the list or execute the pattern matching but I am not able to do it successfully. Can you please help here?
#include <stdio.h>
#include <stdlib.h>
struct node {
int x;
struct node *next;
};
int main(){
int d;
struct node *linked;
struct node *head;
linked = malloc (sizeof(struct node));
head = linked; <<<< As this is pointer, in while loop whenever malloc executes it changes the value of head as well.
printf ("Location of head %p \n", head->next);
d = 1;
while (d>0){
printf ("Enter the value of X: ");
scanf ("%d", &linked->x);
linked->next = malloc (sizeof(struct node));
printf ("Location of linked %p \n", linked->next);
printf ("Location of head %p \n", head->next);
printf ("To enter further value enter non zero: ");
scanf ("%d", &d);
if (d==0)
linked->next = NULL;
}
//linked->next = NULL;
printf("Value of Next is %p\n", linked->next);
printf ("Location of head %p \n", head->next);
}
Output:
MacBook-Air:cprog jimdev$ ./a.out
Location of head 0x7fff90ab952c <<<< this value should not be change
but here in sub sequent output it is.Enter the value of X: 0
Location of linked 0x7ff0624039c0
Location of head 0x7ff0624039c0 <<<< different value then previous
To enter further value enter non zero: 3
Enter the value of X: 3
Location of linked 0x7ff0624039d0
Location of head 0x7ff0624039d0 <<<< different value then previous
To enter further value enter non zero: 0
Value of Next is 0x0
Location of head 0x0
I just tried this new one it does the printf of linked list elements as well, let me know if you guys think of any improvement. I know recursion is quick and neat way to achieve it but I wanted to try out something with while loop.
include
include
struct node {
int x;
struct node *next;
};
int main () {
int d, hold, i;
struct node *list;
struct node *head;
struct node *current;
list = (node *)malloc(sizeof(struct node));
head = list;
printf ("Location of list is %p \n", head);
d = 1;
while (d>0){
printf ("Enter the value of X: ");
scanf ("%d", &list->x);
printf ("Location of list is %p\n", list);
current = (node *)malloc (sizeof (struct node));
list->next = current;
list = current;
printf ("Location of head is %p\n", head);
printf ("Enter zero to terminate the loop: ");
scanf ("%d", &d);
}
list->next = NULL;
printf ("Value of last next is %d\n", list->next);
current = head;
i = 1;
while (current->next != 0){
printf ("Location of list is %p \n", current);
printf ("Value of linked list %d elements %d \n", i, current->x);
current = current->next;
i++;
}
scanf ("%d", &hold);
}
2 things:
you are outputting
head->nextinstead of justheadyou are not updating
linkedtolinked->nextThe head node of a linked list is the first element of a list. head->next, when the linked list is well-formed, is the second element. Either way it shouldn’t be changing as the list is made after it is created.
If you are not updating
linkedto belinked->nextyou are just throwing away the memory you just allocated inlinked->nextand not creating a list.This works:
Your code, since it wasn’t updating
linked, keptheadandlinkedequal. Your list looked like this:Node {x:next}
Since you don’t have any pointers to node2 or node3 you lose these nodes (a memory leak).
You had it almost right. This is what a list generated with this code looks like:
update:
I changed the diagram for your code to better reflect what is happening.
The
ptr->varnameoperator looks in location pointed to byptrfor the value ofvarname. So if the pointers are the same value then you are looking in the same space for the variables. In your case,head=linkedmeans that if you setlinked->next = ptrthenhead->next = ptr, and sincelinkedis never updated to belinked->next(and thereforehead->next) then you are just assigning and reassigninglinked->next