0

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;
       }
}
2
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Laiq Ahmed
0

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

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.