1

I have a question on which I seek clarity with pointers. I am coming from javascript so I am having a hard time with pointers. I have written singly linked list code mostly by myself and the code is fully working. I have created a function to delete the particular item in the linked list. The function in the main is this:

insertAtMiddle(&head, 3, 500);
insertAtMiddle(&head, 100, 500);

There is one thing I can't understand. First I would like to show the code of my delete function.

void insertAtMiddle(node_t **head, int location, int newData){
    node_t *temp = *head;

    while(temp->next != NULL){
        if (temp->data == location)
        {
            break;
        }
        //Shouldn't that also change the original head or move the head to the left as it is passed by reference
        temp=temp->next;
    }

    if (temp->data != location)
    {
        printf("No location found for replacement!");
    }

    //Create new node 
    node_t *newNode = malloc(sizeof(node_t));
    newNode->data = newData;

    newNode->next = temp->next;
    temp->next = newNode;
}

My question is shouldn't the temp=temp->next; inside the while loop should also affect or modify the original head? Head has been passed as a reference to this function. My confusion arises because *temp = *head, temp has been assigned head.

My full code:

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int data;
    struct node *next;
} node_t;

void viewAllNodes(node_t *head){
    node_t *tmp = head;

    printf("\n");

    while (tmp != NULL)
    {
        printf("%d--->", tmp->data);
        tmp=tmp->next;
    }

    printf("\n");
}

void insertAtBegining(node_t **head, int data){
    node_t *tmp = malloc(sizeof(node_t));
    tmp->data = data;
    tmp->next = *head;
    *head = tmp;
}

void deleteNode(node_t **head,int data){
    
    node_t *tmp = *head, *nodeToDelete = NULL;

    //see two nodes in advance
    while(tmp->next != NULL){

        if (tmp->next->data == data)
        {
            nodeToDelete = tmp->next;
            break;
        }
        tmp = tmp->next;
    }

    if (nodeToDelete == NULL)
    {
        printf("No node found to delete");
        return;
    }
    
    tmp->next = nodeToDelete->next;
    free(nodeToDelete);
}

void insertAtEnd(node_t **head, int data){
    node_t *newNode = malloc(sizeof(node_t));
    newNode->data = data;
    newNode->next = NULL;

    node_t *tmp = *head;
    
    while (tmp->next != NULL)
    {
        tmp = tmp->next;
    }

    tmp->next = newNode;
}

node_t *searchNode(node_t *head, int value){
    node_t *tmp = head;

    while (tmp->next != NULL)
    {
        if(tmp->data == value){
            return tmp;
        }
        tmp=tmp->next;
    }
    
    return NULL;
}

void insertAtMiddle(node_t **head, int location, int newData){
    node_t *temp = *head;

    while(temp->next != NULL){
        if (temp->data == location)
        {
            break;
        }
        //Shouldn't that also change the original head as it is passed by
        temp=temp->next;
    }

    if (temp->data != location)
    {
        printf("No location found for replacement!");
    }

    //Create new node 
    node_t *newNode = malloc(sizeof(node_t));
    newNode->data = newData;

    newNode->next = temp->next;
    temp->next = newNode;
}

int main(){

    node_t *head = NULL;

    insertAtBegining(&head, 1);
    insertAtBegining(&head, 2);
    insertAtBegining(&head, 3);
    insertAtBegining(&head, 4);
    insertAtBegining(&head, 5);
    insertAtBegining(&head, 6);

    insertAtEnd(&head, 8);
    insertAtEnd(&head, 9);

    insertAtBegining(&head, 100);
    viewAllNodes(head);
    
    deleteNode(&head, 1);
    deleteNode(&head, 8);

    insertAtMiddle(&head, 3, 500);
    insertAtMiddle(&head, 100, 500);

    viewAllNodes(head);

    return 0;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
killerprince182
  • 455
  • 2
  • 12
  • You never modify `*head`. `temp` is just a local copy of `*head`. – Jabberwocky Jan 05 '22 at 10:08
  • `head` still points to where it was pointing before. `temp` initially points to where `head` is pointing and later updates when `temp = temp->next`. – kiner_shah Jan 05 '22 at 10:09
  • @kiner_shah shouldn't change head because temp has been assigned as *temp = *head; Any modifications made to temp should also affect head? – killerprince182 Jan 05 '22 at 10:10
  • What is the type of `head` on this line: `insertAtMiddle(&head, 3, 500);`? – kiner_shah Jan 05 '22 at 10:13
  • Pointers also occupy some memory. `node ** head` in `insertAtMiddle` is a pointer to a location where pointer to `head` is stored. – kiner_shah Jan 05 '22 at 10:17
  • 1
    One important thing about pointers is that they are also values, the memory location where the point to, in C arguments are passed by value, so if you pass a pointer as argument of a function, it is still a copy of the original value, changing it does not change the original. This is not really the main problem here, but it is something that helps to understand the dynamic. – anastaciu Jan 05 '22 at 10:23

1 Answers1

2

You are basically doing this:

#include <stdio.h>

void foo(int* head) {
  int temp = *head;
  temp = 1234;
}

int main()
{
  int bar = 0;
  foo(&bar);
  printf("bar = %d\n", bar);
}

You expect 1234 as output, but the actual output is 0.

temp is just a local copy, modifying temp will just modify temp and nothing else.

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Can you help me understand what is still causing this? https://onlinegdb.com/MX1oFcg83 – killerprince182 Jan 05 '22 at 10:25
  • @killerprince182 you should ask a new question – Jabberwocky Jan 05 '22 at 10:26
  • @killerprince182 quick answer: you never modify `a` and there is no pointer to `a` either so how should `a` be modified? – Jabberwocky Jan 05 '22 at 10:30
  • 1
    @killerprince182 [here you have some pointers](https://stackoverflow.com/q/897366/6865932) (pun intended) on how pointers work, note that this does not at all replace the learning process you should take on, I'm an old fashioned guy so I think you should start by getting yourself a [good book](https://stackoverflow.com/q/562303/6865932). – anastaciu Jan 05 '22 at 10:50