我试图在没有链接的情况下重载+运算符,这样我就可以组合两个链表。根据我目前的代码,它似乎根本没有合并它们。
这是我目前尝试重载运算符以组合列表。
void WORD::operator+ (const WORD & B)
{
character *p = new character;
while (p == 0)
{
Insert(p->symbol);
}
}
这是我给出的帮助编写代码的程序头。
#include <iostream>
#include <string>
using namespace std;
#ifndef WORD_H
#define WORD_H
class character
{
public:
char symbol;
character *next;
};
class WORD
{
public:
bool IsEmpty() { return front == 0; };
int Length(); //Length: Determines the length of the word A; remember A is the current object;
friend ostream & operator<<(ostream & out, const WORD & org); //Overload the insertion operator as a friend function with chaining to print a word A;
void operator=(const string & s);// Overload the assignment operator as a member function to take a
//string (C-style or C++ string, just be consistent in your implementation) as an argument and
//assigns its value to A, the current object;
WORD & operator=(const WORD & w); // Overload the assignment operator as a member function with chaining to take a word
//object as an argument and assigns its value to A, the current object;
void operator+(const WORD & B); //Overload the ‘+” operator as a member function without chaining to add word B
//(adds the set of symbols that makep B's linked list to the back of A's linked list) to the back of word A;
//remember A is the current object;
void WORD::Insert(char key);
WORD();//The default constructor will initialize your state variables.
//The front of the linked list is initially set to NULL or 0; this implies a non-header node
//implementation of the link list.
WORD(const string & s); //Explicit-value constructor: This constructor will have one argument;
//a C-style string or a C++ string representing the word to be created;
WORD(const WORD & org); // Copy Constructor: Used during a call by value, return, or initialization/declaration of a word object;
~WORD(); //Destructor: The destructor will de-allocate all memory allocated for the word. Put the message
//"destructor called\n" inside the body of the destructor.
bool IsEqual(const WORD & B);// Returns true if two word objects are equal; otherwise false; remember A is the current
private:
character *front, *back;
};
#endif
这也是&#34;插入&#34;在重载运算符中使用的函数。
void WORD::Insert(char key)
{
character *p = new character;
p->next = 0;
p->symbol = key;
if (front == 0)
{
front = back = p;
}
else
{
back->next = p;
back = p;
}
}
用于测试重载运算符功能的驱动程序。
cout << "************TEST#8*******************************" << endl;
cout << "Adding 2 words by adding us to the back of their. Their is the current object" << endl;
WORD their("AAAA0000AAA0000AAA");
WORD us("XXXX");
their + us;
cout<<"their followed by us is \n" <<their<< " = AAAA0000AAA0000AAAXXXX"<<endl;
cout << "*************************************************" << endl;
cout<<endl<<endl;
cout << "************TEST#9*******************************" << endl;;
cout << "Adding 2 words, their2 is empty, by adding us to the back of their.Their is the current object" << endl;;
WORD their2("");
their2 + us;
cout<<"their followed by us is \n" <<their<< " = XXXX"<<endl;
cout << "*************************************************" << endl;
cout << endl << endl;
cout << "************TEST#10*******************************" << endl;;
cout << "Adding 2 words, their3 is empty, by adding us to the back of their.Their is the current object" << endl;
WORD their3("");
us + their3;
cout << "us followed by empty their3 is \n" << us << " = XXXX" << endl;
cout << "*************************************************" << endl;
cout<<endl<<endl;
值得一提的是,我无法真正改变&#34; Insert&#34;函数或重载运算符的函数头。话虽如此,我将如何组合这两个链表?谢谢您的帮助。
答案 0 :(得分:0)
根据您提问中的有限信息,我相信这会解决它:
void operator+ (const Word & B)
{
character *p = B.front;
while (p != 0)
{
Insert(p->symbol);
p = p->next;
}
}
如果你发布了cpp实现文件,可能还有你的主文件,那么它将有助于评估这个解决方案的正确性。