Hello everyone....again.

I have a program that is using a queue. I have a class member function that is to traverse the queue, and output the smallest number in the queue.

Pseudo for this function is:
Compare the front and front->next. Whichever is smaller, keep it. Traverse the queue until each node had been evaluated (yes, I know this is not an efficient way to do things, but is quick and dirty for what I need.)

Here is the code snippet. If you think you need more, I'll be happy to post the rest.

void dynintqueueEX::smallest()
{
	queuenode *temp;

	// CHECK FOR EMPTY QUEUE
	if(front == NULL)
	{
		cout<< "\nQUEUE IS EMPTY! \n";
		
	}

	//step through qeueu - starting with front
	//to find the smallest number in the queue
	for(int a = 0; a <= NULL; a--)
	{
		temp = front;
		if(front >= front->next)
		{
			temp = front->next;
			cout << "The low value this step is: " << front->value << endl;
		}
		
	}
	//cout << "\nTHE SMALLEST NUMBER IN QUEUE IS: " << top->value;

}

I commented out the last cout for debugging purposes. I put the cout inside the loop to let me know what the program was doing through each pass. Currently, as shown above, it results in an infinite loop. If I change a-- to a++ it only seems to evaluate the queue once.

This is probably something simple, but I've been staring at it too long. Any hints or help to prod me along is much appreciated.

line 14: >> for(int a = 0; a <= NULL; a--)

Do you know the definition of NULL? Answer: 0. That loop will execute as many times as it takes for a to wrap back around to a positive number, which could be a whole lot of iterations. for(int a = 0; a <= 0; a--) line 17: >>if(front >= front->next)
Why are you comparing the address of two pointers? What good does that do? Aren't you supposed to compare the data object occupied by the nodes?

My guess is what you want is this:

void dynintqueueEX::smallest()
{
    int lowval;
    //CHECK FOR EMPTY QUEUE
    if(front == NULL)
    {
        cout<< "\nQUEUE IS EMPTY! \n";
        return;
    }

    //step through qeueu - starting with front
    //to find the smallest number in the queue
    queuenode *temp = front;
    lowval = temp->value;
    while(temp)
    {
          if(lowval > temp->value)
              lowval = temp->value;
         temp = temp->next;
    }    
    cout << "Low value is " << lowval << "\n";

Ancient Dragon,

Thanks for your help. I need to go back and visit how queues work. I don't think I truly understand them.

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.