943,553 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1704
  • C++ RSS
Mar 31st, 2008
0

deleting a random node from a singly linked list

Expand Post »
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:

c++ Syntax (Toggle Plain Text)
  1. void remove(list*& start, int s)
  2. {
  3. list* pom=NULL;
  4. list* pom2=NULL;
  5. while(pom!=NULL)
  6. {
  7. pom=start;
  8. if(pom->next->value==s)
  9. {
  10. pom2=pom->next;
  11. pom->next=pom->next->next;
  12. delete pom2;
  13. }
  14. start=start->next;
  15. }
  16. }
Reputation Points: 10
Solved Threads: 0
Light Poster
flash121 is offline Offline
49 posts
since Jan 2008
Mar 31st, 2008
0

Re: deleting a random node from a singly linked list

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
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Mar 31st, 2008
0

Re: deleting a random node from a singly linked list

Thanks, works like a charm.
Reputation Points: 10
Solved Threads: 0
Light Poster
flash121 is offline Offline
49 posts
since Jan 2008
Mar 31st, 2008
0

Re: deleting a random node from a singly linked list

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.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: total array
Next Thread in C++ Forum Timeline: class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC