添加到已排序的C ++中

问题描述 投票:2回答:1

我想在C ++中实现经过排序的bag(collection)数据结构(带有一个单链列表),当我要测试add函数时遇到问题。这是测试:

SortedBag sb(relation1);  (relation1 is e1<=e2) 
    sb.add(5);
    std::cout << sb.size()<<" ";
    sb.add(6);
    std::cout << sb.size() << " ";
    sb.add(0);
    std::cout << sb.size() << " ";
    sb.add(5);
    std::cout << sb.size() << " ";
    sb.add(10);
    std::cout << sb.size() << " ";
    sb.add(8);
    std::cout << sb.size() << " ";

它将打印1 2 3 3 4 5而不是1 2 3 4 5 6。

这是添加功能:

void SortedBag::add(TComp e) {
    Node* auxiliarElement = new Node;
    Node* CheckSLL = new Node;
    int flagStop = 1;

    if (this->head == nullptr)
    {
        auxiliarElement->value = e;
        auxiliarElement->freq = 1;
        auxiliarElement->next = nullptr;
        this->head = auxiliarElement;
    }
    else {
        CheckSLL = this->head;
        while (CheckSLL->next != nullptr && rel(CheckSLL->value, e)) 
        {
            if (CheckSLL->value == e) {
                CheckSLL->freq += 1;
                flagStop = 0;
                break;
            }
            CheckSLL = CheckSLL->next;
        }
        if (CheckSLL == this->head && flagStop)
        {
            auxiliarElement->value = e;
            auxiliarElement->freq = 1;
            auxiliarElement->next = this->head;
            this->head = auxiliarElement;
            flagStop = 0;
        }
        if (CheckSLL->value == e && flagStop)
        {
            CheckSLL->freq += 1;
            flagStop = 0;
        }
        if (flagStop) {
            auxiliarElement->value = e;
            auxiliarElement->freq = 1;
            auxiliarElement->next = nullptr;
            CheckSLL->next = auxiliarElement;
        }
    }
}

size()函数工作正常,我也将发布它:

int SortedBag::size() const {
    int Size = 0;
    Node* goThrough = new Node;
    goThrough = this->head;
    while (goThrough != nullptr) {
        Size += goThrough->freq;
        goThrough = goThrough->next;
    }
    return Size;
}

而且我不知道为什么它不从第二个5起增加频率。请有人帮我吗? (结构节点具有value,freq和指向下一个节点的指针)

c++ data-structures linked-list singly-linked-list sortedlist
1个回答
1
投票

对于初学者,这些陈述

Node* CheckSLL = new Node;

Node* goThrough = new Node;

导致内存泄漏。

也是此输出

And it will print 1 2 3 3 4 5.

由于功能size计算频率的总值,因此不符合输入数据的顺序

Size += goThrough->freq;

因此,在列表中插入了6元素后,输出应为

1 2 3 4 5 6

关系应指定为e1 < e2,而不是e1 <= e2

功能add可以非常简单地定义。我假设该关系对应于运算符<.>

void SortedBag::add( TComp e ) 
{
    Node **current = &this->head;

    while ( *current != nullptr && rel( ( *current )->value, e ) )
    {
        current = &( *current )->next;
    }

    if ( *current == nullptr || rel( e, ( *current )->value ) )
    {
        Node *new_node = new Node;

        new_node->value = e;
        new_node->freq  = 1;
        new_node->next = *current;

        *current = new_node;
    }
    else
    {
        ++( *current )->freq;
    }
}

并且您应该确定函数size是返回频率还是列表中的节点数。

这里是演示程序。

#include <iostream>
#include <functional>

template <typename T, typename Comparison = std::less<T>>
class List
{
private:
    struct Node
    {
        T value;
        size_t freq;
        Node *next;
    } *head = nullptr;

    Comparison cmp;

public:
    explicit List() : cmp( Comparison() )
    {
    }

    explicit List( Comparison cmp ) : cmp( cmp )
    {
    }

    ~List()
    {
        while ( this->head != nullptr )
        {
            Node *current = this->head;
            this->head = this->head->next;
            delete current;
        }
    }
    List( const List & ) = delete;
    List & operator =( const List & ) = delete;

    void add( const T &value );

    friend std::ostream & operator <<( std::ostream &os, const List &list )
    {
        for ( Node *current = list.head; current != nullptr; current = current->next )
        {
            os << current->value << ':' << current->freq << " -> ";
        }

        return os << "null";
    }
};


template <typename T, typename Comparison>
void List<T, Comparison>::add( const T &value ) 
{
    Node **current = &this->head;

    while ( *current != nullptr && cmp( ( *current )->value, value ) )
    {
        current = &( *current )->next;
    }

    if ( *current == nullptr || cmp( value, ( *current )->value ) )
    {
        Node *new_node = new Node { value, 1, *current };

        *current = new_node;
    }
    else
    {
        ++( *current )->freq;
    }
}

int main() 
{
    List<int> list;

    list.add( 5 );
    list.add( 6 );
    list.add( 0 );
    list.add( 5 );
    list.add( 10 );
    list.add( 8 );

    std::cout << list << '\n';

    return 0;
}

程序输出为

0:1 -> 5:2 -> 6:1 -> 8:1 -> 10:1 -> null
© www.soinside.com 2019 - 2024. All rights reserved.