Sunday, August 26, 2018

Flow of values of pointers in a Singly Linked-list as new nodes get added from front

#include<stdio.h>
#include<stdlib.h>
struct Node{
int value;
struct Node * next;
};
int insertNode(struct Node** head,int value){
    printf("Insert Node %d\n",value);
    struct Node* newNodePtr = (struct Node*)malloc(sizeof(struct Node));
    newNodePtr->value=value;
    newNodePtr->next=*head; // Now the 'next' of the NEW NODE holds what head held originally = NULL
    printf(" address in newNodePtr = %d = address OF the NEW NODE\n",newNodePtr);
    printf(" address in newNodePtr->next = %d \n",newNodePtr->next);
    printf(" address in *head = %d \n",*head);
    printf(" address in head = %d \n",head);
    *head=newNodePtr; // I am changing where head is POINTING TO, it now POINTS TO 'newNodePtr'
    // i.e. to the NEW NODDE (now holds the address of a POINTER pointing to the NEW NODE
    printf("Finally address OF head = %d \n",head);
    printf("Finally address in head = *head = %d = address OF the NEW NODE\n",*head);
    return 1;
}
void PrintList(struct Node* head){
        while(head){
        printf("value in nodes is %d \n",head->value);
        head = head->next;
        }
}
int main(){
    int a =10;
    int * p = &a; // 'p' points to 'a'
  struct Node * head = NULL;
   printf("In main, address IN 'head' = %d i.e. head POINTS TO NULL\n",head);
   printf("In main, But 'head' has ITS OWN ADDRESS (address of head = '&head') = %d \n",&head);
   printf("This address OF head '&head' = %d UNIQUELY IDENTIFIES this LIST and remains",&head);
   printf("UNCHANGED throughout\n");
   printf("In main, value at address of head '*(&head) = %d = address in 'head'\n",*(&head));
   printf("This means address IN 'head' == '*(&head)'\n");
   printf("NOTE: 'Node ** head == '&head', POINTER-2-POINTER i.e. ADRRESS of a POINTER\n");
   printf("Therefore, '*heaad' == Address held by 'head',i.e. where the head is POINTING TO");
   printf(" p = %d\n",p);
   printf(" &a = %d\n",&a);
   printf(" &p = %d\n",&p);
   printf(" *(&p) = %d\n",*(&p));
   printf(" *p = %d\n",*p);
   printf("(1) This means p == *(&p), where 'p' is any POINTER\n");
   printf("(2) This means if you print 'p' you will get '&a', because p=&a\n");
   printf("\nSee how the values of newNodePtr, newNodePtr->next\n");
   printf("addresses head holds(points to) changes, BUT  the address OF HEAD\n");
   printf("remains the SAME with each call to insertNode\n\n");
  int arr[5]={1,2,3,4,5};
    int i;
    for(i=0;i<5;i++){
     insertNode(&head,arr[i]);
    }
     PrintList(head);
    return 0;
}


Output:
$gcc -o main *.c
$main
In main, address IN 'head' = 0 i.e. head POINTS TO NULL
In main, But 'head' has ITS OWN ADDRESS (address of head = '&head') = -1625641784 
This address OF head '&head' = -1625641784 UNIQUELY IDENTIFIES this LIST and remainsUNCHANGED throughout
In main, value at address of head '*(&head) = 0 = address in 'head'
This means address IN 'head' == '*(&head)'
NOTE: 'Node ** head == '&head', POINTER-2-POINTER i.e. ADRRESS of a POINTER
Therefore, '*heaad' == Address held by 'head',i.e. where the head is POINTING TO p = -1625641768
 &a = -1625641768
 &p = -1625641776
 *(&p) = -1625641768
 *p = 10
(1) This means p == *(&p), where 'p' is any POINTER
(2) This means if you print 'p' you will get '&a', because p=&a

See how the values of newNodePtr, newNodePtr->next
addresses head holds(points to) changes, BUT  the address OF HEAD
remains the SAME with each call to insertNode

Insert Node 1
 address in newNodePtr = 10371104 = address OF the NEW NODE
 address in newNodePtr->next = 0 
 address in *head = 0 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371104 = address OF the NEW NODE
Insert Node 2
 address in newNodePtr = 10371136 = address OF the NEW NODE
 address in newNodePtr->next = 10371104 
 address in *head = 10371104 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371136 = address OF the NEW NODE
Insert Node 3
 address in newNodePtr = 10371168 = address OF the NEW NODE
 address in newNodePtr->next = 10371136 
 address in *head = 10371136 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371168 = address OF the NEW NODE
Insert Node 4
 address in newNodePtr = 10371200 = address OF the NEW NODE
 address in newNodePtr->next = 10371168 
 address in *head = 10371168 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371200 = address OF the NEW NODE
Insert Node 5
 address in newNodePtr = 10371232 = address OF the NEW NODE
 address in newNodePtr->next = 10371200 
 address in *head = 10371200 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371232 = address OF the NEW NODE
value in nodes is 5 
value in nodes is 4 
value in nodes is 3 
value in nodes is 2 
value in nodes is 1 

No comments:

Post a Comment

इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया - सुदर्शन फ़ाकिर

 इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया वरना क्या बात थी किस बात ने रोने ना दिया आप कहते थे कि रोने से ना बदलेंगे नसीब उमर भर आप की इस बात...