双链表插入排序错误

发布于 2024-08-30 12:06:30 字数 2746 浏览 5 评论 0原文

我已经在 10,000 个整数的文件的双链接列表(从最高到最低)中实现了插入排序,并以相反的顺序输出到文件。

据我所知,我已经实现了这样一个程序,但是我注意到在输出文件中,有一个数字不合适。所有其他数字均按正确顺序排列。

不在位置的数字是重复的数字,但该数字的其他重复顺序都是正确的。奇怪的是这个数字是如何错误放置的。另外,未排序的数字只有 6 个位置不同步。

我已经检查了我的程序好几天了,不知道问题出在哪里,所以我向你寻求帮助。

下面是有问题的代码,

(旁注:我的问题可以自己删除吗?相反,我的大学不会窃取我的代码,如果不是,如何删除它?)

    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);
    }
}

谢谢您的时间。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花心好男孩 2024-09-06 12:06:30

我不喜欢这部分

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);
}

想象一下,如果 inValue 大于除 this->back->value 之外的所有值,会发生什么。它被插入到最后,而不是在此->后面之前。顺便说一句,您正在以相反的顺序插入相等的整数,它们会被读取。对于整数来说,这并不重要,但如果您插入其他对象,则可能会如此。我会将插入方法的代码更改为:

node* temp;
temp = this->front;
while(temp!=NULL)
{
    if(temp->value > inValue)
    {
        insertBefore(inValue, temp);
        return;
    }
    temp = temp->next;
}
insertBack(inValue);

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);
终遇你 2024-09-06 12:06:30

只是一些评论。

while(!in.eof())

这不会阻止循环内部出现 EOF 错误。你想要

while ( in >> readInt )

也,

if(this->front == NULL)

不要

void DLLIntStorage::insertion(int inValue)
{
    node* temp;
    temp = this->front;

    if(temp->value >= inValue)

混合。前面要么可以为 NULL,要么不能。同样,您需要决定是使用 temp->next!=NULL 还是 temp!=this->back 作为循环终止条件,但不能同时使用两者。


我的猜测是,多个链接约定之间的一些不一致导致错误的值被推到列表的中间。

Just some remarks.

while(!in.eof())

This will not stop the inside of the loop from seeing an EOF error. You want

while ( in >> readInt )

Also,

if(this->front == NULL)

and

void DLLIntStorage::insertion(int inValue)
{
    node* temp;
    temp = this->front;

    if(temp->value >= inValue)

do not mix. Either the front can be NULL, or it cannot. Likewise, you need to decide whether to use temp->next!=NULL or temp!=this->back, but not both, as a loop termination condition.


My guess would be that some inconsistency between multiple linking conventions is causing the errant value to get pushed into the middle of the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文