链表遍历跳过值
Linked List traversal skips values
我是指针新手,我有点困惑。 我编写了一个函数来合并和排序两个排序的链表。但是,当我在调用函数后打印列表时,它没有新合并列表的所有值。在调试代码并检查变量和内存位置时,它看起来像跳过了位置,只是跳转到最后一个内存位置。函数执行后,我需要将值放在新列表中,并将 list1 和 list2 留空。这是我在头文件中的方法:
template <class Type>
void orderedLinkedList<Type>::mergeLists(orderedLinkedList<Type> &list1, orderedLinkedList<Type> &list2)
{
nodeType<Type> *lastSmall; //pointer to the last node of the merged list.
nodeType<Type> *first1 = list1.first;
nodeType<Type> *first2 = list2.first;
if (list1.first == NULL){ //first sublist is empty
this->first = list2.first;
list2.first = NULL;
}
else if (list2.first == NULL){ // second sublist is empty
this->first = list1.first;
list1.first = NULL;
}
else{
if (first1->info < first2->info){ //Compare first nodes
this->first = first1;
first1 = first1->link;
lastSmall = this->first;
}
else{
this->first = first2;
first2 = first2->link;
lastSmall = this->first;
}
while (first1 != NULL && first2 != NULL)
{
if(first1->info < first2->info){
lastSmall->link = first1;
lastSmall = lastSmall->link;
first1 = first1->link;
}
else{
lastSmall->link = first2;
lastSmall = lastSmall->link;
first2 = first2->link;
}
} //end while
if (first1 == NULL) //first sublist exhausted first
lastSmall->link = first2;
else //second sublist exhausted first
lastSmall->link = first1;
list1.first = NULL;
list1.last = NULL;
list2.first = NULL;
list2.last = NULL;
}
}
然后在我的主要.cpp我有:
int main()
{
orderedLinkedList<int> list1;
orderedLinkedList<int> list2;
orderedLinkedList<int> newList;
list1.insert(2);
list1.insert(6);
list1.insert(7);
list2.insert(3);
list2.insert(5);
list2.insert(8);
newList.mergeLists(list1, list2);
newList.print();
return 0;
}
我的打印功能以防万一:
template <class Type>
void linkedListType<Type>::print() const
{
nodeType<Type> *current; //pointer to traverse the list
current = first; //set current so that it points to
//the first node
while (current != NULL) //while more data to print
{
cout << current->info << " ";
current = current->link;
}
}//end print
有人可以告诉我我在这里做错了什么吗?输出应该是 2 3 5 6 7 8,但它是 2 3 7 8。
谢谢
编辑: 这是我的插入功能。请注意,这个函数来自我正在使用的书。它包含在我需要添加 mergeLists 方法的同一类中。它是专门为有序列表编写的:
template<class Type>
void orderedLinkedList<Type>::insert(const Type& newItem)
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
nodeType<Type> *newNode;
bool found;
newNode = new nodeType<Type>;
newNode->info = newItem;
newNode->link = NULL;
//case1 list is empty
if(this->first == NULL)
{
this->first = newNode;
this->last = newNode;
this->count++;
}
else //if the list is not empty
{
current = this->first;
found = false;
while(current != NULL && !found)
{
if(current->info >= newItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
//case2 insert newNode at the head
if(current == this->first)
{
newNode->link = this->first;
this->first = newNode;
this->count++;
}
else //case 3
{
trailCurrent->link = newNode;
newNode->link = current;
if(current == NULL)
this->last = newNode;
this->count++;
}
}
}
}
根据书中的3个案例是:
案例1:
该列表最初为空。包含新项的节点是唯一节点 因此,列表中的第一个节点。
案例2:
新项小于列表中的最小项。新项目在 列表的开头。在这种情况下,我们需要调整列表的头部指针—— 也就是说,首先。此外,计数递增 1。
案例3:
该项目将插入列表中的某个位置。
案例3a:
新项大于列表中的所有项。在这种情况下,新的 项将插入到列表的末尾。因此,电流值为空 并将新项目插入到跟踪电流之后。此外,计数也会增加 由 1.
案例3b:
新项目将插入列表中间的某个位置。在此 在这种情况下,新项目将插入到跟踪当前和当前之间。 此外,计数递增 1。
错误出在您的插入方法中。当您在插入后调用列表上的print
时,您会注意到最后一个值被覆盖:
list1.insert(2); list1.print();
list1.insert(6); list1.print();
list1.insert(7); list1.print();
list2.insert(3); list2.print();
list2.insert(5); list2.print();
list2.insert(8); list2.print();
将输出:
2 26
2 7
3 3 5
3
8
这是因为每次迭代trailCurrent->link = newNode;
都会被调用,第一次发生这种情况时会剪切列表。
所以例如当7
插入list1
时,循环会先在trailCurrent
2
时将trailCurrent->link
设置为7
,然后继续trailCurrent->link
设置为7
当trailCurrent
6
时。但是由于2
现在指向7
而不是6
,链条丢失了,你只能2
和7
。
获取另一本书
你用来学习这个的书已经过时了。C 样式指针和手动内存分配不应在现代C++中使用。尝试获得一本现代书籍,教授如何使用智能指针和现代集合,并教授正确的调试技术,这样您就可以轻松地检测出您现在偶然发现的问题。
感谢所有试图提供帮助的人。最后,这是一个简单的逻辑错误。 我无意中在插入方法的while循环末尾添加了一个大括号,这导致循环执行的代码不应该多次导致奇怪的行为。请参阅以下更正后的代码:
while(current != NULL && !found)
{ //<-- Removed this
//code
this->count++;
}
} //<-- Removed this
现在工作代码是:
template<class Type>
void orderedLinkedList<Type>::insert(const Type& newItem)
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
nodeType<Type> *newNode;
bool found;
newNode = new nodeType<Type>;
newNode->info = newItem;
newNode->link = NULL;
//case1 list is empty
if(this->first == NULL)
{
this->first = newNode;
this->last = newNode;
this->count++;
}
else //if the list is not empty
{
current = this->first;
found = false;
while(current != NULL && !found)
if(current->info >= newItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
//case2 insert newNode at the head
if(current == this->first)
{
newNode->link = this->first;
this->first = newNode;
this->count++;
}
else //case 3 insert newNode anywher in the list
{
trailCurrent->link = newNode;
newNode->link = current;
if(current == NULL)
this->last = newNode;
this->count++;
}
}
}
- 遍历链表时的无限循环
- C++声明双链表,使用两个 for 循环双向遍历列表并打印
- 关于链表遍历和调试的困惑
- 链表遍历跳过值
- 无法遍历链表
- 自定义链表const_iterator无法遍历列表的非const实例
- 在 C++ 中遍历链表比在具有类似内存访问的 Go 中慢
- C++ 遍历链表
- 遍历链表时出现分段错误
- 如何在 c++ 中多次遍历链表
- 使用引用遍历链接列表不起作用
- C++链表遍历和修改
- 如果我们使用链表数组来实现哈希表,则可以以不需要遍历的方式实现"add"。这是真的还是假的?
- 链表的循环遍历
- 遍历链表: while(ptr!=NULL) vs while(ptr->next!=NULL)?
- 遍历链接列表时出现分段错误
- 遍历链表并修改或插入节点C++
- C++中的反向遍历单链表
- Firemonkey:如何使用TScreen.Forms在应用程序中遍历所有表单
- 遍历链表的列表