lundi 6 avril 2020

Linked list has different behaviour for similar pointer assignment

I am writing a code to delete a node from a linked list when only the pointer to the node is given and the head node is not given

/*
struct Node {
  int data;
  struct Node *next;
  Node(int x) {
    data = x;
    next = NULL;
  }
}*head;
*/

// This function should delete node from linked list. The function
// may assume that node exists in linked list and is not last node
// node: reference to the node which is to be deleted
void deleteNode(Node *node)
{
        node=(node->next);
}

does not delete the present pointer in the list, but,

/*
struct Node {
  int data;
  struct Node *next;
  Node(int x) {
    data = x;
    next = NULL;
  }
}*head;
*/

// This function should delete node from linked list. The function
// may assume that node exists in linked list and is not last node
// node: reference to the node which is to be deleted
void deleteNode(Node *node)
{
        *node=*(node->next);
}

Deletes the node from the linked list

Why? what is the difference between the approach?

Aucun commentaire:

Enregistrer un commentaire