2017-03-01 19 views
-1

我想重載+操作符而無需鏈接,這樣我可以組合兩個鏈表。用我目前的代碼,它似乎沒有把它們結合起來。如何使用重載+運算符組合兩個鏈表?

這是我當前嘗試重載操作符來組合列表。

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 

此外,這是「插入」功能,在重載操作符內使用。

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; 

值得一提的是,我不能真正改變「插入」函數或重載操作符的函數頭。話雖如此,我將如何去結合這兩個鏈表?感謝您的幫助。

+0

你有'void WORD :: Insert(char key);'在你的'class WORD'定義中聲明...當你已經在'class WORD'類中時,你不需要'WORD ::' (並且實際上它可能不會編譯) – smead

+0

另外,在'operator +'超載中,'while(p == 0)'不會運行,因爲你已經給一個新字符賦了'p'。事實上,它甚至不像你在任何地方使用過輸入'B',所以我不太確定你如何期待它的工作。 – smead

+0

大概你需要從'B.front'開始,複製那個角色,然後得到那個角色的'next',複製它,等等,直到你到達'back' – smead

回答

0

從你的問題的信息有限,我相信這將解決這個問題:

void operator+ (const Word & B) 
{ 
    character *p = B.front; 
    while (p != 0) 
    { 
     Insert(p->symbol); 
     p = p->next; 
    } 
} 

如果您發佈的CPP實現文件,並可能你的主,它會在評估這正確性幫助解。

+0

@ProjectCBL請考慮標記一個正確的答案。謝謝。 – alhadhrami