链接列表赋值运算符

时间:2014-02-08 03:26:50

标签: c++ list

我为链接列表的overloaded =运算符编写了一些代码,但由于某些原因它没有做任何事情,我无法弄清楚原因。

包含链表的类称为String,结构ListNode是节点本身。

ListNode:

struct ListNode
{
      char info;
      ListNode * next;
      ListNode(char newInfo, ListNode * newNext)
        : info( newInfo ), next( newNext )
      {
      }
};

字符串:

class String {

    private:
        ListNode* head;

    public:
    String( const char * s = "");
    String( const String & s );
    String operator = ( const String & s );
    ~String();
}
ostream & operator << ( ostream & out, String& str );
istream & operator >> ( istream & in, String & str );

String.cpp:

String::String( const char * s) {
    if (s == "") {
        head = NULL;
        return;
    }

    ListNode* newNode = new ListNode(s[0], NULL);
    head = newNode;
    ListNode* current = head;
    for (int i = 1; s[i] != 0; current = current->next) {
        current->next = new ListNode(s[i], NULL);
        ++i;
    }
}

String::String(const String& s ) {

    ListNode* current = new ListNode((s.head)->info, NULL); //make all next's null just in case
    head = current;
    for(ListNode* sstart = s.head->next; sstart != NULL; sstart = sstart->next) {
            current->next = new ListNode(sstart->info, NULL);
            current = current->next;
    }

}

//RETURN STRING BY REFERENCE OR COPY CONSTRUCTOR IS CALLED
String& String::operator = ( const String & s ) {
    ListNode* start = head;
    ListNode* tmp;
    while(start != NULL) {
        tmp = start->next;
        delete start;
        start = tmp;
    }

    head = NULL;

    if (s.head == NULL)
        return *this;    
    ListNode* current = new ListNode((s.head)->info, NULL); //make all next's null just in case
    head = current;
    for(ListNode* sstart = s.head->next; sstart != NULL; sstart = sstart->next) {
            current->next = new ListNode(sstart->info, NULL);
            current = current->next;
    }

    return *this;

}

String::~String() {
    ListNode* nextNode = head;
    ListNode* tmp;
    while(nextNode) {
        tmp = nextNode->next;
        delete nextNode;
        nextNode = tmp;
    }
}

ostream & operator << ( ostream & out, String& str) {

    for (int i = 0; i < str.length(); ++i) {
        out << str[i];
    }
    return out;

}

istream & operator >> ( istream & in, String & str ) {
    int len = in.gcount();
    char* buf = new char[len];
    char inChar;
    for(int i = 0; in >> inChar; ++i) {
        buf[i] = inChar;        
    }
    String tmp(buf);
    str = tmp;
}

在第一个循环中,我正在删除head指向的链接列表。在那之后,我将头部设置为NULL,其中s根本不包含任何内容。如果不是,那么,我将当前设置为s中第一个ListNode的副本并将当前存储在head中(如果我使用head遍历,那么我将丢失指向列表开头的指针)。最后,我的第二个循环将“附加”其余的s到当前。

当我运行我的代码时,没有任何反应。我的终端会打印出一个空白行,然后什么都没有,向我建议我可能会在某个地方变得无限。我的代码出了什么问题?

编辑:更改了此链接列表的删除,问题仍然存在。

2 个答案:

答案 0 :(得分:4)

通过访问此代码段中已删除的对象(因为tmp == start),您将导致未定义的行为:

tmp = start;
delete start;
start = tmp->next;

可能还有其他问题,但首先要解决这个问题。例如,您可以在删除之前将下一个指针存储在临时变量中:

tmp = start->next;
delete start;
start = tmp;

答案 1 :(得分:1)

我测试你在问题中输入的代码,似乎是重载的问题&lt;&lt; 希望它会有所帮助

ostream & operator << ( ostream & out, String& str) {

    ListNode *p = str.head; // change head to public or define a public function get_head;
    while(p)
    {
        out << p->info;
        p = p->next;
    }

    return out;

}