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

The if is the proper construct. You couldn't replace them with while or do/while without significant restructuring of the methods.

I will note however that the remove method could be improved. It presently will 'leak' the newitem that it created. (The creation of which was entirely un-necessary.) The method will also fault in ListItem * next = head->getNext(); // Initialize next pointer if the list is empty (head == null).

Keep it simpler ;)

void OrderedList::insert(int val)
{
    ListItem* prev = 0;
    ListItem* curr = head;

    while (curr && curr->getValue() < val) {
        prev = curr;
        curr = curr->getNext();
    }
    ListItem* pnew = new ListItem(val,curr);
    if (prev == 0)
        head = pnew;
    else
        prev->setNext(pnew);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.