这是我制作的一些代码,应该正确复制链接数据类型中的所有节点,但它无法正常工作。我已经检查了逻辑并在纸上写了很多次,但它仍然无法使用。我在代码的这一部分上做错了什么?我使用指针复制节点的准确性是否准确?我的构造函数测试的一部分是干草,是开始打印出队列中的内容的一部分。
void LinkedQueue<ItemType>::CopyNodesFrom(const LinkedQueue& a_queue)
{
Node<ItemType>* orig_chain_ptr = a_queue.front_ptr_; // Points to nodes in original chain
if (orig_chain_ptr == nullptr) {
front_ptr_ = nullptr; // Original queue is empty
back_ptr_ = nullptr;
return;
}
// Copy first node
front_ptr_ = new Node<ItemType>();
front_ptr_->SetItem(orig_chain_ptr->GetItem());
// Advance original-chain pointer
orig_chain_ptr = orig_chain_ptr->GetNext();
// Copy remaining nodes
Node<ItemType>* new_chain_ptr = front_ptr_; // Points to last node in new chain
Node<ItemType>* temp_ptr;
while (orig_chain_ptr != nullptr) {
temp_ptr = new Node<ItemType>(orig_chain_ptr->GetItem() );
new_chain_ptr->SetNext(temp_ptr);
orig_chain_ptr = orig_chain_ptr->GetNext(); //Advance Our original pointer
new_chain_ptr = new_chain_ptr->GetNext(); //Advance our new chain pointer
} // end while
new_chain_ptr->SetNext(nullptr);
back_ptr_ = new_chain_ptr;
} // end copy constructor
#include <iostream>
#include <string>
#include "LinkedQueue.h" // ADT Queue operations
using namespace std;
void CopyConstructorAndAssignmentTester() {
LinkedQueue<string> queue;
string items[] = {"zero", "one", "two", "three", "four", "five"};
for (int i = 0; i < 6; i++) {
cout << "Adding " << items[i] << endl;
bool success = queue.Enqueue(items[i]);
if (!success)
cout << "Failed to add " << items[i] << " to the queue." << endl;
}
cout << "Queue contains, from front to back, zero one two three four five." << endl;
cout << "Checking Copy Constructor tester " << endl;
LinkedQueue<string> copy_of_queue(queue);
cout << "Copy of queue contains, from front to back, ";
for (int i = 0; i < 6; i++)
{
cout << " " << copy_of_queue.PeekFront();
copy_of_queue.Dequeue();
}
cout << "." << endl;
/*
cout << "Checking Assignment Operator tester " << endl;
LinkedQueue<string> assigned_queue;
assigned_queue.Enqueue("ha");
assigned_queue.Enqueue("ba");
assigned_queue = queue;
cout << assigned_queue << endl;*/
/* cout << "Assigned queue contains, from front to back, ";
for (int i = 0; i < 6; i++)
{
cout << " " << assigned_queue.PeekFront();
assigned_queue.Dequeue();
}
cout << "." << endl;
cout << "Original queue contains, from front to back,";
for (int i = 0; i < 6; i++) {
cout << " " << queue.PeekFront();
queue.Dequeue();
}
cout << "." << endl << endl; */
} // end copyConstructorTester
int main()
{
CopyConstructorAndAssignmentTester();
char a;
cin >> a;
//ConcatenateTester();
//return 0;
} // end main
编辑:哦,废话,这比我想象的要多。XD。我以为我犯了一个明显的错误。
这可能不是您要寻找的答案,我发现很难在您的代码中发现错误,缺乏被操纵的状态的全部信息。
链接的列表逻辑看起来还不错:就用于复制的逻辑而言,没有什么能让我跳出来。放置蒸馏形式:
first_node = last_node = new Node(other.first_node->data);
for (Node* other_node = other.first_node->next; other_node; other_node = other_node->next)
{
Node* new_node = new Node(other_node->data);
last_node->next = new_node;
last_node = new_node;
}
last_node->next = nullptr;
我相信这是您拥有的,就整体逻辑而言应该是正确的。任何问题都可能会在其他地方找到。然而,减少与您合作的状态的数量减少的生活应该使您的生活更容易。此" new_chain_ptr"是不必要的,您只能将结果直接写入'Back_ptr_'。
但是,我有不同的建议。您的其余队列正常工作,是的,包括这些方法,例如"入口"?如果是这样,只需使用已经有效的内容,就可以更琐碎地实现您的复制构造函数。从状态开始以进行空排队,然后读取另一个队列中的元素,然后将这些元素"入选"到您的副本中。现在,您可以避免在低级链接列表逻辑中纠结自己,通过使用您已经知道的零件的功能:
:// Create empty queue.
first_node = last_node = nullptr;
// Enqueue elements from other queue.
for (Node* other_node = other.first_node; other_node; other_node = other_node->next)
Enqueue(other_node->data);
在迭代中,这可能会花费您一个额外的分支机构,但请记住,正确的性总是在效率之前,并且一旦使其正常工作,就可以回来并优化。记住如果在这种情况下逻辑无法正常工作,请处理自我分配。
是的,调试器还将为您带来巨大的优势,以加快对代码性质的理解,而更快地发现错误。