0

I am trying to delete Nodes from a given point but I am stuck on how to delete the last node. Nodes other than tail can be deleted by swapping the values and changing the links. My Code:

    #include <iostream>
    using namespace std;
    
    struct Node
    {
        int data;
        Node *next;
        Node(int x)
        {
            data = x;
            next = NULL;
        }
    };
    
    void appointNULL(Node *point)
    {
        point = NULL;   
    }
    int main()
    {
        Node *head = new Node(10);
        head->next = new Node(20);
        appointNULL(head->next);
        cout<<head->next->data;
    }

Output:

20

when I tried to print it came out same without any change?

what am I doing wrong?

  • 2
    Arguments are by default passed *by value*, which means the value is *copied*. The `appointNULL` function only have a copy of the pointer in its local `point` variable. Modifying this local copy will not change the original. Perhaps you need to [get a decent book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or two and learn about *references*? – Some programmer dude Feb 01 '21 at 08:23
  • @Someprogrammerdude oh thanx didn't know that.. – Sumit Jaiswal Feb 01 '21 at 08:33

0 Answers0