【问题标题】:C++ doesn't save the changes by other methodC++ 不通过其他方法保存更改
【发布时间】:2012-06-09 03:31:04
【问题描述】:

我正在尝试在 C++ 中实现一个链表类,但遇到了问题。我有添加新节点的 += 运算符。

链表类接口:

template <typename Type>

class LinkedList {
public:
    LinkedList<Type>* head;
//  linked list stracture
    Type data;
    LinkedList<Type>* next;
//  others ....
    size_t length;
public:
    LinkedList();
    ~LinkedList();
    void initializeHead(LinkedList<Type>* headPtr);
    size_t size() const;
    LinkedList& operator+=(const Type& add);
    void operator-=(const Type& remove);
    LinkedList<Type>& operator[] (const size_t index) const;
    bool operator== (const LinkedList<Type> &versus) const;
    friend ostream& operator<< (ostream& out,LinkedList& obj);
};

这里我有 += 重载工具:

template <typename Type> LinkedList<Type>& LinkedList<Type>::operator +=(const Type& add) {
    // head ptr - :)
    LinkedList<Type>* p = head->next;
    // go to the end
    while(p) p = p->next;
    // now on end - create new..!!!
    try {
        p = new LinkedList<Type>;
    } catch (bad_alloc& e) {
        cout << "There\'s an allocation error....";
    } catch (...) {
        cout << "An unknown error.." << endl;
    }// fill and done
    p->data = add;
    p->next = NULL;
    // increment length .........
    ++head->length;
    // done ............
    return *p;
}

另外,我还有“数组”访问重载方法:

template <typename Type> LinkedList<Type>& LinkedList<Type>::operator [](const size_t index) const {
    if(index < 0 || index >= length) // invaild argument
        throw  exception();
    // continue
    LinkedList<Type>* p = head;
    for(size_t i = 0; i < index; ++i) p = p->next; // we are at what we want
    return *p;
}

一切正常 - 我检查了调试器,

问题是 - += 没有将新节点保存在“head->next”中,由于某种原因,在完成 += 方法之后,head->next 等于 null。

有人知道为什么新分配没有链接到 head->next 吗?

非常感谢!!

【问题讨论】:

  • 如果你尝试实现为堆栈,它会简单得多。
  • 链表并不比向量好。它们实际上速度较慢,并且不支持随机访问。至少实现这样的 LL,它具有恒定的时间插入,而不是 O(n)。

标签: c++ linked-list operator-overloading


【解决方案1】:

while(p) p = p-&gt;next;p 之后为 NULL

接下来你做p = new LinkedList&lt;Type&gt;;,但你没有将p链接到头部。

【讨论】:

  • 谢谢,它更好,它保存了新节点 - 但是列表末尾似乎没有 NULL.. 你知道为什么吗?
  • p->数据=数据; p->下一个 = NULL;我认为列表的最后一个节点应该指向这个新节点以获得您的预期结果(最后为 NULL)。
【解决方案2】:

代替:

// go to the end
while(p) p = p->next;

你需要:

head->next = p;

【讨论】:

    【解决方案3】:

    正如其他答案所说,当您尝试添加时,您会超出列表。试试这样的:

    template <typename Type> LinkedList<Type>& LinkedList<Type>::operator +=(const Type& add)
    {
        LinkedList<Type> *last;
    
        // Find the last node in the list
        for (last = head; last != 0 && last->next != 0; last = last->next)
        {
        }
    
        // `last` now points to the last node in the list, or is zero
        // If zero (i.e. NULL) then list is empty
    
        if (last == 0)
        {
            head = new LinkedList<Type>;
            head->next = 0;
            head->data = add;
            head->length = 0;
        }
        else
        {
            last->next = new LinkedList<Type>;
            last->next->next = 0;
            last->next->data = add;
        }
    
        // We can safely use `head` as we are sure it won't be zero
        head->length++;
    
        // Return the added node
        return (last != 0 ? *last->next : *head);
    }
    

    【讨论】:

    • Mm.. 好的,我尝试自己解决delete 问题。非常感谢大家!
    • @nimrod 确保构造函数将head 成员变量置零。最好还有其他变量,那么您不必在我发布的函数中将它们归零。
    【解决方案4】:

    您也可以使用临时变量来存储最后一个节点,然后最后一个节点将指向新节点。

    这是示例代码。您需要处理一些情况,例如添加第一个节点等。

    LinkedList<Type>* temp = NULL;
    while(p) 
    {
      temp = p;
      p = p->next;   
    }  
    
    try 
    {             
      p = new LinkedList<Type>;         
      temp->next = p;
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-05
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多