链表 pop_back() 函数问题
Linked List pop_back() function issues
List.H
void List::pop_back()
{
if (size == 0)
cout << "The list is empty and there is no node to pop off the back of the list" << endl;
else if (size == 1)
{
delete tail;
head = tail = iterator = NULL;
}
else
{
NodeRef temp = tail;
while (iterator->next != NULL)
iterator = iterator->next;
tail = iterator;
delete temp;
size--;
}
}
void List::begin() //Set the iterator to the head of the list
{
iterator = head;
}
void List::push_front(int data) //Inserting a new node in the front of the list
{
if (size == 0) //If there is no nodes in the list, execute the if statement
{
head = new Node(data); //create a new node, and have head point to it
tail = head; //have tail point to the new node also.
}
else //If there are nodes in the list, execute the else statement
{
NodeRef newNode = new Node(data); //create a new node
newNode->next = head; //have the next pointer point to the head of the next node.
head = newNode; //have the head pointer point to the new node inserted at the beginning of the list
}
size++; //Increment the size counter
}
void List::print()
{
iterator = head; //Have iterator point to the head
if (size == 0)
cout << "There is nothing in the list" << endl;
else
{
while (iterator!= NULL)
{
cout << iterator->data << endl; //Display contents in node
iterator = iterator->next; //Move to the next node;
}
}
}
List.cpp
int main()
{
List B;
B.push_front(5);
B.push_front(4);
B.push_front(3);
B.begin();
B.pop_back();
B.print();
return 0;
}
所以我遇到的问题是,在调用 pop_back() 函数之后,我调用了 print() 函数。它弹出了最后一个节点,但列表末尾有一个垃圾编号。我相信正在发生的事情是它正在显示最终节点 Next* 这是一个地址。我知道它与 pop_back() 和 iterator->next 的 else 部分有关,导致它指向一个地址。
您有两个问题:
- 你的
pop_back()
函数的 else
条件 - 你遍历你的列表直到结束,你正确地释放了最后一个节点的内存,但是你没有next
new 尾部的指针指向 NULL。新尾巴的 next
指针仍然指向一些随机的未分配内存,这实际上是一个错误。
- 您实际上并没有移动尾指针。您的内部
while()
循环只是让迭代器指向与之前相同的尾部。
尝试将 pop_back()
中的 else
语句更改为:
iterator = head;
while (iterator->next->next != NULL) {
iterator = iterator->next;
}
tail = iterator;
delete tail->next;
tail->next = NULL;
--size;
此代码假设您的列表大小至少为 2(上述 if
语句处理 0 和 1,因此对于此代码示例应该没问题)。
List.H
void List::pop_back()
{
if (size == 0)
cout << "The list is empty and there is no node to pop off the back of the list" << endl;
else if (size == 1)
{
delete tail;
head = tail = iterator = NULL;
}
else
{
NodeRef temp = tail;
while (iterator->next != NULL)
iterator = iterator->next;
tail = iterator;
delete temp;
size--;
}
}
void List::begin() //Set the iterator to the head of the list
{
iterator = head;
}
void List::push_front(int data) //Inserting a new node in the front of the list
{
if (size == 0) //If there is no nodes in the list, execute the if statement
{
head = new Node(data); //create a new node, and have head point to it
tail = head; //have tail point to the new node also.
}
else //If there are nodes in the list, execute the else statement
{
NodeRef newNode = new Node(data); //create a new node
newNode->next = head; //have the next pointer point to the head of the next node.
head = newNode; //have the head pointer point to the new node inserted at the beginning of the list
}
size++; //Increment the size counter
}
void List::print()
{
iterator = head; //Have iterator point to the head
if (size == 0)
cout << "There is nothing in the list" << endl;
else
{
while (iterator!= NULL)
{
cout << iterator->data << endl; //Display contents in node
iterator = iterator->next; //Move to the next node;
}
}
}
List.cpp
int main()
{
List B;
B.push_front(5);
B.push_front(4);
B.push_front(3);
B.begin();
B.pop_back();
B.print();
return 0;
}
所以我遇到的问题是,在调用 pop_back() 函数之后,我调用了 print() 函数。它弹出了最后一个节点,但列表末尾有一个垃圾编号。我相信正在发生的事情是它正在显示最终节点 Next* 这是一个地址。我知道它与 pop_back() 和 iterator->next 的 else 部分有关,导致它指向一个地址。
您有两个问题:
- 你的
pop_back()
函数的else
条件 - 你遍历你的列表直到结束,你正确地释放了最后一个节点的内存,但是你没有next
new 尾部的指针指向 NULL。新尾巴的next
指针仍然指向一些随机的未分配内存,这实际上是一个错误。 - 您实际上并没有移动尾指针。您的内部
while()
循环只是让迭代器指向与之前相同的尾部。
尝试将 pop_back()
中的 else
语句更改为:
iterator = head;
while (iterator->next->next != NULL) {
iterator = iterator->next;
}
tail = iterator;
delete tail->next;
tail->next = NULL;
--size;
此代码假设您的列表大小至少为 2(上述 if
语句处理 0 和 1,因此对于此代码示例应该没问题)。