I seem to be stuck in an endless loop here and was wondering if someone could help? I am writing this method to a linked list class. There are strings of words in a file that is read into the linked list. It needs to read the last string in the file (eg: happy) then run back through the linked list, and anything less than happy (any word starting with a-g) it will delete from the list and create a new list with the deleted nodes. I have a deleteNode method written also. Any help is appreciated!

void list::outputResult(ostream& outstream)
{

  node *nodeptr;
  string data;
  
  nodeptr = head;
  while(nodeptr->link != NULL)
  {
  nodeptr = nodeptr->link;
  }
  
  nodeptr->data;
  string temps = nodeptr->data;

  nodeptr = head;
  while(nodeptr->link != NULL)
  {
  
  if(nodeptr->data < temps)
  {
  deleteNode(data);
  nodeptr = head;
  }
  else
  nodeptr = nodeptr->link;
  }
}

Recommended Answers

All 3 Replies

Where do you remove elements from the list? I see that you call deleteNode(data), but you haven't shown us what deleteNode does.

Here is deleteNode

void list::deleteNode(string item)
{
  
  if(head != NULL)
    {
      node *previous, *current;
      previous = NULL;
      current = head;
      while((current != NULL) && (current->data != item))
	{
	  previous = current;
	  current = current->link;
	}
      if(previous == NULL)
	{
	  deleteFront();
	}
      if((previous != NULL) && (current != NULL))
	{
	  node *temp;
	  temp = current;
	  previous->link = current->link;
	  delete temp;
	}
     
    }

}

Are you sure it's an endless loop? Unforturnately, despite the name outputResult() I don't see where any output is coded in the function so it might look like it is just grinding away when in reality it isn't written to do anything we humans can appreciate. Renaming outputResult() something like adjustList() may be better. You could then use a display() function (or the << operator if you have that overloaded) to output the list after adjusting the list based on the value of the string in the last node of the list.

If you truly have an endless loop, then throw in some output statements so you can follow the code and see where the endless loop actually starts (this is one way to debug runtime errors. If you're comfortable using a debugging software, you could use that, too). Of course this also means you need to remove the debugging code before releasing the project.

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.