944,116 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 10542
  • C RSS
Dec 30th, 2005
0

remove method linked list

Expand Post »
I am having a small problem removing ALL items from a linked list. I have written a function that will remove all discharged patients from the linked list.


 for(int a=1;a<(S.size()+1);a++)
               {
               int x=S.retrieve_status(a);
                        if (x==5)
                        {
                        S.remove(a);
                        }
               }

in this function I check to see if the status of the patient is =5 and if it is I remove it from the linked list.

  1. typedef patient SeqItemType;
  2.  
  3. class ListNode
  4. {
  5. public:
  6. SeqItemType item;
  7. ListNode *next;
  8.  
  9. };
  10.  
  11.  
  12.  
  13. class Sequence
  14. {
  15.  
  16. public:
  17. Sequence();
  18.  
  19.  
  20. bool isEmpty();
  21. int size();
  22. void insert(int index, SeqItemType newItem);
  23. void remove(int index);
  24. void alter_status(int index, int change);
  25. int retrieve_status(int index);
  26. void display(int index, int a,int b, int c, int d,
  27. int e, int f,int g);
  28. void move(int from, int to);
  29. void alter_priority(int index);
  30. int retrieve_ward(int index);
  31. void alter_ward(int index, int change);
  32. void alter_doctors_name(int index, int y);
  33.  
  34. private:
  35. ListNode *find(int index);
  36. int length;
  37. ListNode *head;
  38.  
  39. };
  40.  
  41.  
  42.  
  43. void Sequence::remove(int index)
  44. {
  45.  
  46. ListNode *cur;
  47.  
  48.  
  49. // remove the first node in the list
  50. if (index == 1)
  51. {
  52. cur = head;
  53. head = head->next;
  54.  
  55. }
  56. else{
  57. ListNode *prev = find(index-1);
  58.  
  59. cur = prev->next;
  60. prev->next = cur->next;
  61. }
  62.  
  63. // clean things up!
  64. length--;
  65. cur->next = NULL;
  66. delete cur;
  67.  
  68.  
  69. }


HOWEVER when I check my linked list after it will always contain 1 patient. even though the status of the patient is ==5. It delelts all but 1.

When I run the function again it will remove the last element.

Why do I have to run the function twice to remove all the elements from the linked list? is there a problem with my remove method in my linked list. or am I specifing the wrong parameters in my for loop function at the top of screen?

any suggestions greatly appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
TimC is offline Offline
42 posts
since May 2005
Dec 31st, 2005
0

Re: remove method linked list

This is what I am thinking...

Your loop condition is based on a=1 and will increment while a < size()+1.
  1. for(int a=1;a<(S.size()+1);a++)

But my guess is.. your linked list is "zero based".. much like how an array is.. which would account for why all other elements would be deleted.. except for your very first one.. which is, "node zero" or the head node..

try this and see what happens:
 for(int a=0; a<S.size()+1; a++)
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Jan 1st, 2006
0

Re: remove method linked list

try this and see what happens:


 for(int a=0; a<S.size()+1; a++)

Clinton thanks for the response. I tryed that but unfortunately it did not work. HOWEVER you got me thinking about the for loop and I came up with a solution.

What I forgot is.... when it comes to removing a node from a linked list the size of that linked list will automatically chage size. So on the question I asked above

  1.  
  2. for(int a=1;a<(S.size()+1);a++)
  3. {
  4. int x=S.retrieve_status(a);
  5. if (x==5)
  6. {
  7. S.remove(a);
  8. }
  9. }

If X==5, S.remove(a) would remove the node AND the size of the list S.size() would automatically change. This then caused complications with my for loop as size started to decrease and "a" was still being incremented.

this is the soloution I came up with and works fine.

  1.  
  2. static int wx=0;
  3.  
  4. for(int a=1;a<(S.size()+1+wx);a++)
  5. {
  6. int x=S.retrieve_status(a-(wx));
  7. if (x==5)
  8. {
  9.  
  10. S.remove(a-(wx));
  11. wx++;
  12. }
  13. }
  14.  
  15. wx=0;
Reputation Points: 10
Solved Threads: 0
Light Poster
TimC is offline Offline
42 posts
since May 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Parsing a String into Tokens Using strcspn, Part 3
Next Thread in C Forum Timeline: C preprocessor





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


Follow us on Twitter


© 2011 DaniWeb® LLC