Question about Insertion Sort

Queue::QueueNode* Queue::getPointer(int nodeNo)
{
QueueNode *cur = frontPtr;
for(int i = 1; i < nodeNo; i++)
cur=cur->next;
return cur;

}

void Queue::insertionsort()
{
int counter = 2;
for(QueueNode* cur = frontPtr->next; cur != NULL; cur=cur->next)
{
int p = cur->priority;
QueueNode *sort = cur;
for(int i=counter;i>1;i--)
{
QueueNode *prev=getPointer(counter-1);
if(prev->priority < p)
{
QueueNode *slot = sort;
slot->next=prev->next;
slot = prev;
sort->next = slot;

}

}

counter++;
}
}


i was trying to do about insertion sort in queue.
i can succesful run the program but it stuck when i call insertion
fuction. It was not runtime error, it juz stuck at the program, when i press enter it juz wont continue run.
Last edited on
Just noticing, why do you manually do things as 'QueueNode* nxt = frontPtr->next;' when you have a 'getPointer' function to do this?
ya. it juz same without it.
but the problem still exist.
Last edited on
I suspect an invalid pointer is reached.

What happens when you put everything between 'for(QueueNode* cur = frontPtr->next; cur != NULL; cur=cur->next)' and the closing '}' in comments, does it still stop functioning? If yes your for loop is incorrect, if it doesn't and seems to work, go to the next for loop.
i tink the problem is exist inside the
if(prev->priority < p)
{
QueueNode *slot = sort;
slot->next=prev->next;
slot = prev;
sort->next = slot;

}


because when i comment like this

if(prev->priority < p)
{
/*QueueNode *slot = sort;
slot->next=prev->next;
slot = prev;
sort->next = slot;
*/
}



then the program can run, but i duno which 1 wrong about this code.
these codes should shift the node to the right if the priority is higher, then insert the higher priority back to the front.
but i duno how to code it.
Last edited on
Topic archived. No new replies allowed.