Hi, I'v written this code for an Assignment however I was wondering if anybody had any suggestions on how to change the loops from IF/ELSE statements to Do/While, if that would be possible just the rest of the guys in my class are doing it this way with IF and I'd like to be able to go about it another way.
void OrderedList::insert(int val)
{
ListItem * newItem = new ListItem(val,NULL); // Create a new list entry
ListItem * previous = NULL; // Pointer for holding previous item
ListItem * current = head; // Point current to head value
while(current != NULL) // While there are items available for evaluation
{
if(newItem->getValue() < current->getValue())
{
newItem->setNext(current);
if(previous == NULL) // If there is no previous value we are at list start
{
head = newItem;
return;
}
else // Otherwise value is being inserted into middle of list
{
previous->setNext(newItem);
return;
}
}
previous = current;
current = current->getNext();
}
if(previous == NULL)
{
head = newItem;
return;
}
else
{
previous->setNext(newItem);
return;
}
}
bool OrderedList::remove(int val)
{
ListItem * newItem = new ListItem(val,NULL); // Create a new list entry
ListItem * previous = NULL; // Pointer for holding previous item
ListItem * current = head; // Point current to head value
ListItem * next = head->getNext(); // Initialize next pointer
// Walk the list, deallocating the item that matches the search val
while (current->getNext() != NULL)
{
if (newItem->getValue() == current->getValue()) // if we find the value
{
// If head value is deleted make next value in the list the head value
if (previous == NULL)
{
delete current;
head = next;
}
else // Otherwise delete the value
{
delete current;
previous->setNext(next);
}
return true;
}
previous = current;
current = current->getNext();
next = current->getNext();
}
if (newItem->getValue() == current->getValue())
{
delete current;
previous->setNext(NULL);
}
else
{
return false;
}
}
Thanks