添加到已分类的袋子C++中



我想在C++中实现已排序的包(集合(数据结构(带有单链表(,但在测试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的频率。有人能帮帮我吗?(结构节点具有值、freq和指向下一个节点的指针(

对于初学者来说,这些语句

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

相关内容

  • 没有找到相关文章

最新更新