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.
typedef patient SeqItemType;
class ListNode
{
public:
SeqItemType item;
ListNode *next;
};
class Sequence
{
public:
Sequence();
bool isEmpty();
int size();
void insert(int index, SeqItemType newItem);
void remove(int index);
void alter_status(int index, int change);
int retrieve_status(int index);
void display(int index, int a,int b, int c, int d,
int e, int f,int g);
void move(int from, int to);
void alter_priority(int index);
int retrieve_ward(int index);
void alter_ward(int index, int change);
void alter_doctors_name(int index, int y);
private:
ListNode *find(int index);
int length;
ListNode *head;
};
void Sequence::remove(int index)
{
ListNode *cur;
// remove the first node in the list
if (index == 1)
{
cur = head;
head = head->next;
}
else{
ListNode *prev = find(index-1);
cur = prev->next;
prev->next = cur->next;
}
// clean things up!
length--;
cur->next = NULL;
delete cur;
}
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.