2012-06-05 66 views
0

我试图在C++中实现链接列表类,我有问题。我有添加新节点的+ =运算符。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?

非常感谢!

+0

如果你尝试实现堆栈,它会简单得多。 –

+0

链接列表并不比向量好。它们实际上比较慢,并且不支持随机访问。至少实现这种具有恒定时间插入的LL,而不是O(n)。 –

回答

2

while(p) p = p->next; p是NULL

和明年你做p = new LinkedList<Type>;但你不与p链接到头部。

+0

谢谢,它更好,它节省了它的新节点 - 但它似乎像列表末尾没有NULL ..你知道为什么吗? – nimrod

+0

p-> data = data; p-> next = NULL;我认为列表的最后一个节点应该指向这个新节点以获得您的预期结果(NULL结束)。 – Rahul

0

相反的:

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

您需要:

head->next = p; 
0

至于其他的答案说,你超越的列表,当您尝试添加。试试这样的:

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); 
} 
+0

嗯..好吧,我试着自己解决'删除'问题。非常感谢你! – nimrod

+0

@nimrod确保构造函数将'head'成员变量清零。最好还有其他变量,那么您不必在我发布的函数中将它们归零。 –

0

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

这是示例代码。你需要照顾一些情况,如添加第一个节点等

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

try 
{    
    p = new LinkedList<Type>;   
    temp->next = p; 
}