我有以下两个函数,它们根本不会更改我的链表。我说明了如果给定节点为空,我然后检查给定节点是头还是尾,最后插入需要的位置并更正节点指向的位置。我一定错过了一小部分,但我不知道是什么。有任何想法吗?谢谢!在节点之前和之后插入
// insert the new object after the node p
void DoublyLinkedList::insertAfter(DListNode &p, int newobj) {
if(isEmpty()) {
throw EmptyDLinkedListException("Empty Doubly Linked List");
}
DListNode *newNode = new DListNode();
newNode->obj = newobj;
newNode->prev = &p;
newNode->next = p.next;
if (p.next != NULL) {
p.next->prev = newNode;
}
p.next = newNode;
if(&trailer == &p){
trailer = *newNode;
}
}
// insert the new object before the node p
void DoublyLinkedList::insertBefore(DListNode &p, int newobj){
if(isEmpty()) {
throw EmptyDLinkedListException("Empty Doubly Linked List");
}
DListNode *newNode = new DListNode();
newNode->obj = newobj;
newNode->prev = p.prev;
newNode->next = &p;
if(&header == &p){
header = *newNode;
}
if (p.prev != NULL) {
p.prev->next = newNode;
}
}
我有一个列表,最初是:
100 90 80 70 60 50 40 30 20 10 10 20 30 40 50 60 70 80 90 100
我然后实现insertAfter功能的嵌入式15后80:
cout << "On list2 insert 15 after 80. " << endl;
DListNode location = *dll2.getFirst()->next->next;
dll2.insertAfter(location, 15);
cout << "list2: " << dll2 << endl << endl;
根据调试器,所有东西都指向正确的位置,但是上述函数ca的结果LL:
On list2 insert 15 after 80.
list2: 100 90 80 70 60 50 40 30 20 10 10 20 30 40 50 60 70 80 90 100
奇怪的是,当我实现insertAfter然后的insertBefore,如:
// add tests for insertAfter
cout << "On list2 insert 15 after 80. " << endl;
DListNode location = *dll2.getFirst()->next->next;
dll2.insertAfter(location, 15);
cout << "list2: " << dll2 << endl << endl;
//insertBefore
cout << "On list2 insert 9 before 80. " << endl;
dll2.insertBefore(location, 9);
cout << "list2: " << dll2 << endl << endl;
的输出中是这样的:
On list2 insert 15 after 80.
list2: 100 90 80 70 60 50 40 30 20 10 10 20 30 40 50 60 70 80 90 100
On list2 insert 9 before 80.
list2: 100 90 9 80 15 70 60 50 40 30 20 10 10 20 30 40 50 60 70 80 90 100
这说明9之前插入并在后面插入了15,但是当函数结束时,析构函数中存在分段错误,表示正在删除的节点未分配。
“if(&p == NULL)” - 指针可以为null,但对象的地址永远不能为null,这里的&p是现有对象的地址。 –
啊好吧我会改变我抛出异常的措辞。谢谢你, – K22
凯特,一般来说,我们在代码中发现错误的方式并不是通过真正想到它。它甚至没有向其他可能更难以思考的人展示代码。我们在代码中发现错误的方式是使用调试器。我猜想有人可能最终会出现谁能够指出你的代码中的错误,但是如果你只是使用了你的调试器,那么你将节省很多时间(并且学到了非常有价值的技能)。 –