![]() |
| ||
| remove method linked list 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++)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. typedef patient SeqItemType; 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. |
| ||
| 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. 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++) |
| ||
| Re: remove method linked list
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
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.
|
| All times are GMT -4. The time now is 2:11 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC