Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 757693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:23:35+00:00 2026-05-14T15:23:35+00:00

I have implemented an insertion sort in a double link list (highest to lowest)

  • 0

I have implemented an insertion sort in a double link list (highest to lowest) from a file of 10,000 ints, and output to file in reverse order.

To my knowledge I have implemented such a program, however I noticed in the ouput file, a single number is out of place. Every other number is in correct order.

The number out of place is a repeated number, but the other repeats of this number are in correct order. Its just strange how this number is incorrectly placed. Also the unsorted number is only 6 places out of sync.

I have looked through my program for days now with no idea where the problem lies, so I turn to you for help.

Below is the code in question,

(side note: can my question be deleted by myself? rather my colleges dont thieve my code, if not how can it be deleted?)

    void DLLIntStorage::insertBefore(int inValue, node *nodeB)
{
    node *newNode;
    newNode = new node();
    newNode->prev = nodeB->prev;
    newNode->next = nodeB;
    newNode->value = inValue;

    if(nodeB->prev==NULL)
    {
        this->front = newNode;
    }
    else
    {
        nodeB->prev->next = newNode;
    }
    nodeB->prev = newNode;
}
void DLLIntStorage::insertAfter(int inValue, node *nodeB)
{
    node *newNode;
    newNode = new node();
    newNode->next = nodeB->next;
    newNode->prev = nodeB;
    newNode->value = inValue;

    if(nodeB->next == NULL)
    {
        this->back = newNode;
    }
    else
    {
        nodeB->next->prev = newNode;
    }   
    nodeB->next = newNode;
}
void DLLIntStorage::insertFront(int inValue)
{   
    node *newNode;
    if(this->front == NULL)
    {
        newNode = new node();
        this->front = newNode;
        this->back = newNode;
        newNode->prev = NULL;
        newNode->next = NULL;
        newNode->value = inValue;
    }
    else
    {
        insertBefore(inValue, this->front);
    }

}   
void DLLIntStorage::insertBack(int inValue)
{   
    if(this->back == NULL)
    {
        insertFront(inValue);
    }
    else
    {
        insertAfter(inValue, this->back);
    }
}

ifstream& operator>> (ifstream &in, DLLIntStorage &obj)
{   
    int readInt, counter = 0;               

    while(!in.eof())
    {
        if(counter==dataLength) //stops at 10,000
        {
            break;
        }   

        in >> readInt;

        if(obj.front != NULL )
        {   
            obj.insertion(readInt);         
        }
        else
        {
            obj.insertBack(readInt);
        }
        counter++;
    }       
    return in;
}
void DLLIntStorage::insertion(int inValue)
{
    node* temp;
    temp = this->front;

    if(temp->value >= inValue)
    {
        insertFront(inValue);
        return;
    }
    else
    {       
        while(temp->next!=NULL && temp!=this->back)
        {
            if(temp->value >= inValue)
            {
                insertBefore(inValue, temp);
                return;
            }
            temp = temp->next;
        }
    }

    if(temp == this->back)
    {
        insertBack(inValue);
    }
}

Thankyou for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:23:36+00:00Added an answer on May 14, 2026 at 3:23 pm

    I don’t like this part

    else
    {       
        while(temp->next!=NULL && temp!=this->back)
        {
            if(temp->value >= inValue)
            {
                insertBefore(inValue, temp);
                return;
            }
            temp = temp->next;
        }
    }
    
    if(temp == this->back)
    {
        insertBack(inValue);
    }
    

    Imagine what happens if inValue is greater than all values except this->back->value. It gets inserted at the end instead before this->back. By the way, You are inserting equal integers in the reversed order, they are read. For integers it doesn’t matter that much, but it could if You inserted other objects. I would change the code of the insertion method to this:

    node* temp;
    temp = this->front;
    while(temp!=NULL)
    {
        if(temp->value > inValue)
        {
            insertBefore(inValue, temp);
            return;
        }
        temp = temp->next;
    }
    insertBack(inValue);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.