为什么我对指针>next == NULL 的检查在我的链表中出现错误?
Why is my check for pointer->next == NULL coming up false in my linked list?
第一次将当前->下一个值设置为null时插入链表。在打印过程中,我用它来检查我是否在列表的末尾。当我在调试器中检查它时,它显示一个空值,但我的if语句[if(current->next == null)]并不能阻止无限打印。我的代码如下:
#include "List.h"
#include <cstdlib>
List::List(){
//precondition:none
//postcondition:empty list;position @ 1
head = NULL;
current = NULL;
previous = NULL;
position = 1;
}
List::~List(){
//precondition:link exists
//List is now an empty linked list
makeEmpty();
}
void List::goToNext(){
if(!isAtEnd()){
if(current->next == NULL){
previous == current;
current == NULL;
}
else{
previous = current;
current = current->next;
position +=1;
}
}
}
void List::goToStart(){
if(position = 1){
return;
}
else{
current = head;
previous = NULL;
position = 1;
}
}
void List::makeEmpty(){
if(!isEmpty()){
this->goToStart();
while(isAtEnd() == false)
{
goToNext();
delete previous;
previous = current;
}
head = NULL;
previous = NULL;
current = NULL;
position = 1;
}
}
bool List::isEmpty(){
if(this->head == NULL && this->current == NULL && this->previous == NULL){
return true;
}
}
bool List::isAtEnd(){
if(current == NULL){
return true;
}
else{
return false;
}
}
ItemType List::CurrentItem(){
if(isAtEnd() != true){
return current->data;
}
}
void List::insert(ItemType item){
nodeType * temp = new nodeType;
temp->data = item;
if(head == NULL){
temp->next = NULL;
head = temp;
current = head;
current->next = NULL;
}
else if(position == 1){
head = temp;
head->next = current;
current = head;
}
else if(!isAtEnd() && current->next == NULL){
temp->next = current;
current = temp;
if(previous != NULL){
previous->next = current;
}
}
else{
current = temp;
current->next = NULL;
}
}
void List::deleteCurrentItem(){
previous->next = current->next;
delete current;
current = previous->next;
position -= 1;
}
int List::currentPosition(){
return position;
}
////////---print function from main---///////
void print(List & testList){
int storedPosition = testList.currentPosition();
testList.goToStart();
while (!testList.isAtEnd())
{
cout << testList.CurrentItem() << endl;
testList.goToNext();
}
testList.goToStart();
for ( int i = 1; i < storedPosition; i++)
testList.goToNext();
}
在List::goToNext()
中,您希望在设置previous
和current
时使用赋值,而不是使用比较器。
previous = current;
current = NULL;
相关文章:
- 反向给定链表中的K节点
- C++,指针数组,指向双链表中的条目
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 链表中的显示功能无法正常工作
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- 链表中的 C++ 析构函数
- 遍历链表时的无限循环
- 在链表中的第 n 位插入显示分割错误
- 我如何能够访问双向链表中的嵌套结构
- 为什么我的函数没有打印出双向链表中的第一个节点?
- 我在双链表上的此打印功能时遇到问题
- 如何将节点添加到链表C++的前面?我想多了这个概念
- C++我的第一个链表我做错了什么
- 对象数组作为链表中的字符数组 - 我对对齐的假设是否有效
- 我如何使用C ++以相反的顺序打印此单个链表中的元素
- 我不能删除双向链表中的最小数字,然后删除下一个
- 我的双向链表中的查找函数损坏
- 我将如何在不损坏列表的情况下删除链表中的节点
- 我写了这个程序来反转链表中的元素,在编译这个程序后,在reverse()中显示错误.为什么