【发布时间】:2021-07-02 17:03:50
【问题描述】:
我有这个 Char.h 文件:
struct Node {
char value;
Node* next;
};
class CharList {
private:
Node* head_; //point to the first Node of the list
Node* tail_; //point to the last Node of the list
unsigned size_; //the number of Nodes in the list
public:
CharList();
friend std::ostream& operator<<(std::ostream&, const CharList&);
CharList& operator+=(char c);
};
而且我需要实现+= 运算符,但我真的很挣扎,我不知道该怎么做。
这是我目前所拥有的:
CharList& CharList::operator+=(char c) {
Node* ptr = new Node;
ptr->value = c;
ptr->next = nullptr;
this->head_ +=c;
return *this;
}
在main() 函数上,我希望它能够运行,以便结果看起来像这样:
+='M' M
+='C' M->C
+='S' M->C->S
【问题讨论】:
-
this->head_ +=c;为什么要向指针添加char?
标签: c++ linked-list nodes