Hello,

I'm sorry if this has been asked before, but I didn't seem to find any answers. I'm trying to write a function that would delete a specified number from a singly linked list.

This is what i've got so far, but it's not working:

void remove(list*& start, int s)
{
      list* pom=NULL;
      list* pom2=NULL;
      while(pom!=NULL)
      {
                pom=start;
                if(pom->next->value==s)
                {
                        pom2=pom->next;
                        pom->next=pom->next->next;
                        delete pom2;
                 }
                start=start->next;
       }
}

Recommended Answers

All 3 Replies

Deleting a node is not difficult.
let me tell you the algorithm.

Algorithm.
Delete (value)
1. Check if head is null return
2. if the value matches head
set temp = head
set head = head->next
free head.

3. Link previous = head;
4. Link temp = head->next;

5. while (temp->data != value)
do
temp = temp->next
previous = previous->next

6. if temp is not null
set previous->next = temp->next
free temp.

Hope this helps

Thanks, works like a charm.

any time, But I have left one small bug in the above algorithm for the sharp eyes, please check that also. nothing is a free cake :). small efforts required.

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.