Click here to Skip to main content
15,905,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone help me to change this insertion sort to work properly? Queue implementation is in the header file.
#include <iostream>
#include "queue.h"
using namespace std;

void queue::Enqueue(int m_data)
{

        node * temp=new node;
        temp->value=m_data;
        temp->next=NULL;
        if(front==NULL && tail==NULL)
        {
            front=tail=temp;
            return;
        }
        else
        {
            tail->next=temp;
            tail=temp;
        }

}

void queue::dequeue(){
    node *temp=front;
    if(front==NULL)
    {
        cout<<"queue is empty"<<endl;
        return;
    }
    if(front==tail)
    {
        front=tail=NULL;
    }
    else
    {
        front=front->next;

    }
    free(temp);

}

int queue::frontt()
{
    if(front==NULL)
    {
        cout<<"queue is empty"<<endl;
    }
    return front->value;
}
void queue::print() {

    node *temp = front;
    while (temp != NULL) {
        cout << temp->value;
        temp = temp->next;
    }
    cout << endl;
}

void queue::insertion_sort()
{

    int temp=front->value;

    while(front->next!=NULL)
    {
        while(front->value>front->next->value && front!=NULL)
        {
            swap(front->value, front->next->value);
        }

    }
}
int main()
{
    queue q;
    q.Enqueue(7);
    q.Enqueue(1);
    q.Enqueue(4);
    q.Enqueue(5);
    q.Enqueue(2);
    q.Enqueue(0);
    q.Enqueue(8);

    q.print();
    cout<<endl;
    q.insertion_sort();
    cout<<endl;
    q.print();

}


What I have tried:

I have tried to use array based implementation as a helper but it is a lot different.
Posted
Updated 28-Oct-17 5:37am

1 solution

Either the insertion_sort is sorting the first 2 elements in the queue and loop infinitely, either your sort algorithm is too smart for me.
I think a complete rewrite of insertion_sort is in order.
I guess it is time to learn how to debug your code.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900