C++不会通过其他方法保存更改
C++ doesn't save the changes by other method
我正在尝试在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 吗?
多谢!!
while(p) p = p->next;
p 后为 NULL
接下来你做p = new LinkedList<Type>;
但你没有将P链接到头部。
而不是:
// go to the end
while(p) p = p->next;
你需要:
head->next = p;
正如其他答案所说,当您尝试添加时,您会超出列表。 尝试这样的事情:
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);
}
您还可以使用临时变量来存储最后一个节点,然后最后一个节点将指向新节点。
这是示例代码。您需要处理一些情况,例如添加第一个节点等。
LinkedList<Type>* temp = NULL;
while(p)
{
temp = p;
p = p->next;
}
try
{
p = new LinkedList<Type>;
temp->next = p;
}
相关文章:
- 还有其他方法可以在数组中写入多维数组吗?
- 如何使用 C/C++ 和 system() 系统调用以外的其他方法在 Linux 中获取文件功能?
- Unity3d 中还有其他方法可以访问设备相机吗?
- Sizeof返回的是指针大小,而不是数组大小.有其他方法可以找到尺寸吗
- 还有其他方法可以为乘法表编写循环以获取运行时值吗?
- 是否有其他方法将.dll文件从一个项目复制到我的启动项目中的可执行文件旁边
- 当从其他方法返回 vector 时,C++无法访问矢量元素
- 我不允许更改变量的声明,我可以编辑哪些其他方法
- 有没有其他方法可以将地址分配给指针变量
- 如何将策略模式与派生类中的其他方法一起使用
- C++基础知识(使用其他方法(Unicode?)的相同代码)
- 成员函数模板推演指南或其他方法让编译器知道如何调用函数
- 如何在类中的其他方法中访问私有数据成员 2D 数组
- 如何确保类的每个方法都首先调用其他方法
- 如果派生类包含其他方法和成员,则可以static_cast从基类到派生类
- 我们还能使用任何其他方法来访问Android中的C 代码,而不是使用JNI访问C 代码
- Qt QNetworkAccessManager 或其他方法获取 html 状态代码而不获取页面 contenet
- extern可以解决这个问题吗,或者我可以通过其他方法解决这个问题吗?
- 使用来自其他方法和 #define 的函数定义常量
- 在其他方法之前运行的C 类方法