我无法完成一项家庭作业,将已经插入字符串的对象作为字符链接列表(如H-E-L-L-O)传递,然后将该列表传递给另一个对象。
我可以将第一个单词作为列表单独输入(参见函数InsertWord),但是如果我想在其中添加第二个单词(Insert),我不知道如何。这是我的代码。
我觉得这很简单,我一直都很遗憾。
struct Character {
string symbol;
Character *next;
};
class Word {
public:
Word();
Word::~Word();
void InsertWord(const string&);
void Insert(const Word&, int slot);
void Remove(const string&);
private:
Character *front;
Character *back;
int count;
};
void Word::InsertWord(const string& key) {
int ln = key.length();
for (int n = 0; n < ln; n++) {
if (front == 0) {
front = new Character;
front->symbol = key[n];
front->next = 0;
back = front;
} else {
Character *p = new Character;
p->symbol = key[n];
p->next = front;
front = p;
}
}
}
void Word::InsertInto(const Word &key, int slot) {
if (slot = 0 || 1) {
if (front == 0) {
front->symbol = key.p; //this is where I lose it
front->next = 0;
back = front;
} else {
p->symbol = key[n];
p->next = front;
front = p;
}
}
}
int main() {
Word A;
Word B;
A.InsertWord("45352");
B.InsertWord("YZTHRH");
A.InsertInto(B,1);
return 0;
}