deleting a random node from a singly linked list

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 49
Reputation: flash121 is an unknown quantity at this point 
Solved Threads: 0
flash121 flash121 is offline Offline
Light Poster

deleting a random node from a singly linked list

 
0
  #1
Mar 31st, 2008
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:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: deleting a random node from a singly linked list

 
0
  #2
Mar 31st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: flash121 is an unknown quantity at this point 
Solved Threads: 0
flash121 flash121 is offline Offline
Light Poster

Re: deleting a random node from a singly linked list

 
0
  #3
Mar 31st, 2008
Thanks, works like a charm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: deleting a random node from a singly linked list

 
0
  #4
Mar 31st, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC