#include "queue.h"
#include <cstdlib>
using namespace std;


void queue::sort()
{
	Node *nodeLeft, *nodeRight;
	double temp=0;
	int nofswaps=1;
	
	
	if(size>1)//check that at least two nodes exists in the queue
	{
		while(nofswaps>=1)
		{		
			nofswaps=0;			
			do
			{
				nodeLeft=pFront;//nodeLeft first time points to the first node of the queue and then points to next node each time the loop executes
				nodeRight=nodeLeft->next; //nodeRight points to the next node of nodeLeft
	
				if((nodeLeft->input)<(nodeRight->input))//if left node's value is smaller than right node's
				{
					//swap the values of left and right node
					temp=nodeLeft->input; //contents of left node stored in variable temp
					*nodeLeft=*nodeRight; //left node now contains the value of right node
					*nodeRight=temp; //right node now contains the value of left node
					nofswaps++; //increase nofswaps which counts how many swaps we had
				}
				pFront=pFront->next; //pFront will now point to the next node of queue
			}while(nodeRight!=NULL);
		}
	}//end of if statement
}//end of compareNodes()

Edited 7 Years Ago by miag: n/a

Comments
Sorry, I don't have enough time to care. Hopefully you'll fail and learn to manage your time better instead of looking for a savior.

i think the problem is in line 31 bcause after the first time the pointer points at null

i think the problem is in line 31 bcause after the first time the pointer points at null

you could use an intregrer comparation instead... while(intr <= size)

Edited 7 Years Ago by gastonci: n/a

This article has been dead for over six months. Start a new discussion instead.