hey guys. ive created this function that will delete the first element it finds in a linked list that has the value n in it. it works except when the number is the FIRST element in the list. it wont delete it, it will replace it with a 0 when i try and print the list for some reason.

the linked list is set out like this.

struct node
{
int number;
node *next;
}

void deleteNumber(node *t, int n)
{

node *current;
node *previous;

previous = NULL;

for (current = t; current!=NULL; previous = current, current = current->next)
{
 if (current->number == n)
 {
     if(previous == NULL)
     {
         t = current->next;
     }
     else
     {
      previous->next = current->next;
     }
     delete current;
     return;
 }




}

}

Dani AI

Generated

is correct: the function parameter t is a local copy of the head pointer, so changing t inside the function does not change the caller’s head. That is why deleting the first node appears to turn it into a 0 when you print—the caller still holds a pointer to freed memory (undefined behavior). The fix is to make the caller’s head actually change, or return the new head to the caller.

Three common, safe options (using a different struct/name than your original post):

Return the new head (call-site updates head):

struct Node { int data; Node* next; };

Node* removeValue(Node* head, int value) {
    Node* prev = nullptr;
    Node* cur = head;
    while (cur) {
        if (cur->data == value) {
            Node* next = cur->next;
            if (prev) prev->next = next;
            else head = next;   // update head to be returned
            delete cur;
            return head;
        }
        prev = cur;
        cur = cur->next;
    }
    return head;
}

Use it like: head = removeValue(head, n);

Pass the head by pointer-to-pointer or by reference so the function can update the caller’s pointer without returning:

// pointer-to-pointer
void removeValue(Node** headRef, int value) {
    Node* cur = *headRef;
    Node* prev = nullptr;
    while (cur) {
        if (cur->data == value) {
            if (prev) prev->next = cur->next;
            else *headRef = cur->next; // directly modify caller head
            delete cur;
            return;
        }
        prev = cur;
        cur = cur->next;
    }
}

// C++ reference-to-pointer
void removeValue(Node*& head, int value) { /* same body as removeValue above using head */ }

Notes: always update the caller’s head when deleting the first node, handle single-node lists, and avoid using a node after delete. A simple dummy/sentinel head can also simplify edge cases. These approaches address the issue you saw and avoid the undefined behaviour that produces the mysterious zero value.

Recommended Answers

All 3 Replies

t = current->next;

This is where your problem lies. Since the memory address 't' is purely local (it's just been pushed onto the stack), when the function returns, the linked list pointer in the parent function will keep pointing to the address of the memory that you deleted.

so how'd i go about fixing it :(

but i thought that creating a pointer to a node called *t would reference the SAME memory location as my start_ptr in my main application. therefore if i change what 't' points to then it changes what start_ptr points to?

>so how'd i go about fixing it
Use either a pointer of a pointer (node **t in your function parameters), or set up your function to return the address of the first node in the list.

>therefore if i change what 't' points to then it changes what start_ptr points to?
You can change the memory that the memory address references, but you can't actually change the memory address itself (which is what you've been attempting to do here). A pointer of a pointer solves this problem because you're indirectly passing the memory address of the node to the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.