链表遍历跳过值

Linked List traversal skips values

本文关键字:遍历 链表      更新时间:2023-10-16

我是指针新手,我有点困惑。 我编写了一个函数来合并和排序两个排序的链表。但是,当我在调用函数后打印列表时,它没有新合并列表的所有值。在调试代码并检查变量和内存位置时,它看起来像跳过了位置,只是跳转到最后一个内存位置。函数执行后,我需要将值放在新列表中,并将 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 2

6
2 7
3 3 5
3
8

这是因为每次迭代trailCurrent->link = newNode;都会被调用,第一次发生这种情况时会剪切列表。

所以例如当7插入list1时,循环会先在trailCurrent2时将trailCurrent->link设置为7,然后继续trailCurrent->link设置为7trailCurrent6时。但是由于2现在指向7而不是6,链条丢失了,你只能27

获取另一本书

你用来学习这个的书已经过时了。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++;
}
}
}