C++ 我需要做一个包使用双链接列表(元素,频率)对

C++ 我需要做一个包使用双链接列表(元素,频率)对,c++,C++,就像在标题中一样,我需要使用带有(元素、频率)对的双链接列表制作一个包。通过分析我做的其他项目,到目前为止,我做了以下几点: //bag.h #pragma once #include <utility> using namespace std; //DO NOT INCLUDE BAGITERATOR //DO NOT CHANGE THIS PART #define NULL_TELEM -111111; typedef int TElem; class BagIterator;

就像在标题中一样,我需要使用带有(元素、频率)对的双链接列表制作一个包。通过分析我做的其他项目,到目前为止,我做了以下几点:

//bag.h
#pragma once
#include <utility>
using namespace std;
//DO NOT INCLUDE BAGITERATOR
//DO NOT CHANGE THIS PART
#define NULL_TELEM -111111;
typedef int TElem;
class BagIterator; 
class Bag {

private:
    struct node
    {
        pair<TElem, int> info;
        node* prev;
        node* next;
    };
    node* head;
    node* tail;

    friend class BagIterator;
public:
    //constructor
    Bag();
    //adds an element to the bag
    void add(TElem e);
    //removes one occurence of an element from a bag
    //returns true if an element was removed, false otherwise (if e was not part of the bag)
    bool remove(TElem e);
    //checks if an element appearch is the bag
    bool search(TElem e) const;
    //returns the number of occurrences for an element in the bag
    int nrOccurrences(TElem e) const;
    //returns the number of elements from the bag
    int size() const;
    //returns an iterator for this bag
    BagIterator iterator() const;
    //checks if the bag is empty
    bool isEmpty() const;
    //destructor
    ~Bag();
//bag.h
#布拉格语一次
#包括
使用名称空间std;
//不包括BAGITERATOR
//不要改变这部分
#定义NULL_TELEM-111111;
typedef-int-TElem;
类迭代器;
书包{
私人:
结构节点
{
配对信息;
节点*prev;
节点*下一步;
};
节点*头;
节点*尾部;
友元类迭代器;
公众:
//建造师
袋子();
//将元素添加到包中
无效添加(电话e);
//从包中删除元素的一次出现
//如果元素被移除,则返回true,否则返回false(如果e不是包的一部分)
bool-remove(TElem-e);
//检查元素appearch是否在包中
布尔搜索(远程)常量;
//返回包中元素的出现次数
内部(远程)常数;
//返回包中的元素数
int size()常量;
//返回此包的迭代器
BagIterator iterator()常量;
//检查袋子是否是空的
bool isEmpty()常量;
//析构函数
~Bag();

//bag.cpp
#包括“Bag.h”
#包括“BagIterator.h”
#包括
#包括
使用名称空间std;
Bag::Bag(){
//待办事项-实施
此->头部=空PTR;
这->尾=空PTR;
}
空包::添加(远程元素){
//节点*n;
//n=新节点;
//n->pair.first=elem;
//不知道怎么做/继续做。
}
因此,基本上我不知道如何实现add()函数。我想如果我使用add()作为模型,我可以创建其他函数,如remove()和search(),但因为我还需要计算频率,所以尝试和错误并没有给我带来任何好处。

试试这个

void add(TElem elem) {
        node* n = nullptr;
        n = new node;
        n->info.first = elem;
        if (this->tail == nullptr) { //if we don't have an end
            if (this->head == nullptr) { //if we dont have a start
                this->head = n; //set the start to n
            }
            this->tail = n; //set the end to n
        }
        else {
            this->tail->next = n; //our bag's end's next node equals n;
            n->prev = this->tail; //n's previous equals our current tail
            this->tail = this->tail->next; //our tail equals n
        }
    }
它在包的末尾添加了一个元素。我还做了一个删除功能:

bool remove(TElem e) {
        if (this->head == nullptr||this->tail==nullptr) return false; //if we have no elements
        node* traverser = this->head; //our starting point is our bag's starting point
        while (true) {
            if (traverser->info.first == e) { //if we found our element
                node* last = traverser->prev; //the element before ours
                node* succ = traverser->next; //the element after ours
                last->next = succ; //put the element before our's 's next element to the element after our's
                succ->prev = last; //set the element after our's 's previous element to the element before our's
                delete traverser; //delete our element
                return true; 
            }
            else
                if (traverser->next == nullptr) return false; //If we reached the end
                else traverser = traverser->next; //move forward
        }
    }
另外,我建议您将此构造函数添加到
节点
类中:

node() : prev(nullptr), next(nullptr) {} 
这样就不会出现错误

此外,如果您感兴趣,也许迭代器的此模型适合您:
“为什么不使用一个代码> STD::vector < /代码>?这个包似乎没有添加任何一个标准的C++库向量。<代码> TeleMe/Cuff>没有定义任何地方,“ETC”不是结束类定义的有效方式。发布真实代码。@ El JayAy闻起来像一个赋值……不允许使用任何真正的C++。编辑:确认了:他/她/他们得到了模板…@艾尔杰,甚至有一个向量,不知道如何处理频率部分。Pete我修正了。JHBonarius你是对的,但是我可以尝试把真正的C++代码翻译成“数据结构”。是的,不管怎样,我只需要一个基础来分析和理解复制一些在线例子,但是总是混淆它,因为教授也有谷歌,不喜欢你复制的例子。或者做你的家庭作业,读你的书,思考它,并且学习。那个构造函数应该是 NODEE():PREV(NulLPTR),下一个(NulLPTR){}。。在这段代码中没有太大区别,但是编译器总是会生成代码来初始化初始化列表中没有初始化的数据成员。这可能会很昂贵。养成总是在初始化列表中初始化成员的习惯。@PeteBecker我会改变这一点嘿,谢谢Eduardo。完全忘记我可以使用info.first/info.second..啊…谢谢你的帮助。我想我现在知道了如何添加/遍历列表。我不太清楚为什么我们需要“我们的尾巴等于n”但我会尝试进行更多的反向工程,在画图上可视化,并注意频率。再次感谢您的帮助@HyperOni,随时!另外,请参阅我的编辑,我为您的迭代器添加了一个可能的模型
node() : prev(nullptr), next(nullptr) {}