向已排序的文件写入并打印一个额外的 0,并将节点(单链表)从末尾 C++ 中删除。

发布于 2024-11-04 13:10:31 字数 2103 浏览 0 评论 0原文

这可能看起来有点模糊,所以真的很抱歉。我正在写入一个文件并将此单链表中的排序节点打印到控制台。不幸的是,在排序列表中,它会在前面打印并写入一个额外的 0,并从末尾删除一个值。这是代码:

void SLLIntStorage::Read(istream& r)
{
    char c[13];
    r >> c;
    r >> numberOfInts;

    head = new Node;
    head->next = NULL;
    tail = head;
    r >> head->data;

    for (int i = 0; i < numberOfInts; i++)
    {
        Node* newNode = new Node;
        r >> newNode->data;

        if(_sortRead)
        {
            if(newNode->data > tail->data)
            {
                tail->next = newNode;
                tail = newNode;
            }
            else if(head->data > newNode->data)
            {
                newNode->next = head;
                head = newNode;
            }
            else
            {
                current = head;

                while(current->next != NULL)
                {
                    if(current->next->data > newNode->data)
                    {
                        newNode->next = current->next;
                        current->next = newNode;
                        break;
                    }
                    else
                    {
                        current = current->next;
                    }
                }
            }
        }
        else
        {
            tail->next = newNode;
            tail = newNode;
        }
    }
    print();
}

void SLLIntStorage::Write(ostream& w)
{
    current = head;

    for(int i = 0; i < numberOfInts; i++)
    {
        w << current->data << endl;

        if (current->next != NULL)
            current = current->next;
    }
}
void SLLIntStorage::print()
{
    current = head;

    for(int i = 0; i < numberOfInts; i++)
    {
        cout << current->data << endl;
        //system("pause");
        if(current->next != NULL)
        {
            current = current->next;
        }
    }
}

文件示例: 0 0 1 2 2 3 ………… 9995 9996 9996 9998 //这里应该是另一个 9998

That may seem kind of vague so really sorry. I am writing to a file and printing to a console the sorted nodes in this singly linked list. Unfortunately, in the sort list, it both prints and writes an extra 0 at the front and cuts a value off the end. Here is the code:

void SLLIntStorage::Read(istream& r)
{
    char c[13];
    r >> c;
    r >> numberOfInts;

    head = new Node;
    head->next = NULL;
    tail = head;
    r >> head->data;

    for (int i = 0; i < numberOfInts; i++)
    {
        Node* newNode = new Node;
        r >> newNode->data;

        if(_sortRead)
        {
            if(newNode->data > tail->data)
            {
                tail->next = newNode;
                tail = newNode;
            }
            else if(head->data > newNode->data)
            {
                newNode->next = head;
                head = newNode;
            }
            else
            {
                current = head;

                while(current->next != NULL)
                {
                    if(current->next->data > newNode->data)
                    {
                        newNode->next = current->next;
                        current->next = newNode;
                        break;
                    }
                    else
                    {
                        current = current->next;
                    }
                }
            }
        }
        else
        {
            tail->next = newNode;
            tail = newNode;
        }
    }
    print();
}

void SLLIntStorage::Write(ostream& w)
{
    current = head;

    for(int i = 0; i < numberOfInts; i++)
    {
        w << current->data << endl;

        if (current->next != NULL)
            current = current->next;
    }
}
void SLLIntStorage::print()
{
    current = head;

    for(int i = 0; i < numberOfInts; i++)
    {
        cout << current->data << endl;
        //system("pause");
        if(current->next != NULL)
        {
            current = current->next;
        }
    }
}

File sample:
0
0
1
2
2
3
........
9995
9996
9996
9998
//supposed to be another 9998 here

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

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

发布评论

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

评论(1

哀由 2024-11-11 13:10:31

看来你一篇文章读太多了。您首先阅读 r >>> 行中的条目头->数据;
就在 for 循环之前。然后,您在 for 循环中读取额外的 numberOfInts 条目,总计 numberOfInts+1 条目。

It seems you read one entry too much. You first read an entry in the line r >> head->data;
just before the for-loop. Then you read an additional numberOfInts entries in the for-loop for a total of numberOfInts+1 entries.

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