簡體   English   中英

加入分類袋 C++

[英]Adding in a sorted bag C++

我想在 C++ 中實現排序包(集合)數據結構(帶有單鏈表),當我想測試添加 function 時遇到問題。 這是測試:

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。

這是添加 function:

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 的頻率。有人可以幫我嗎? (結構節點有值、頻率和指向下一個節點的指針)

對於初學者來說,這些陳述

Node* CheckSLL = new Node;

Node* goThrough = new Node;

導致 memory 泄漏。

還有這個 output

And it will print 1 2 3 3 4 5.

不對應輸入數據的順序,因為 function size計算頻率的總值

Size += goThrough->freq;

因此,在列表中插入了6元素,那么 output 應該是

1 2 3 4 5 6

應該像e1 < e2那樣指定關系,而不是像e1 <= e2

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

您應該決定 function 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;
}

程序 output 是

0:1 -> 5:2 -> 6:1 -> 8:1 -> 10:1 -> null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM