我的removeBack函数出了什么问题
What is wrong with my removeBack function?
我制作了一个链表,我的其他函数也能工作,但我的removeBack不能。我遍历节点,检查它们是否为NULL,如果节点为NULL,我将链接器复制到指向倒数第二个的上一个指针,并删除包含NULL值的节点。我确信这是有效的,直到两个本应删除的数字没有被删除。这是我的密码。有人能带我到这儿来吗?
#include <string>
#include <iostream>
using namespace std;
class node
{
public:
int data;
node * next;
};
class linkedList
{
private:
node * head;
public:
linkedList()
{
head = NULL;
}
void addFront(int x)
{
node * babynode = new node;
//(*babynode).data = x;
babynode->data = x;
babynode->next = head;
head = babynode;
}
void display()
{
node * finger = head;
while( finger != NULL )
{
cout << finger->data << endl;
finger = finger->next;
}
}
//remove and return the first item in the list
void removeFront()
{
if(head != NULL)
{
node * front = head;
head = head->next;
delete front;
}
else
{
head = NULL;
}
}
void addBack(int x)
{
node * tail = head;
//only if head is empty
if(head == NULL)
{
addFront(x);
}
//if it isnt empty
else{
while(tail->next != NULL)
{
tail = tail->next;
}
node * tail2 = new node;
tail2 -> data = x;
tail2 -> next = tail -> next;
tail -> next = tail2;
}
}
void removeBack()
{
node * one = head;
node * two = one -> next;
while(two != NULL)
{
if(two == NULL)
{
delete one;
head -> next = two;
}
one = one -> next;
two = one -> next;
}
}
};
Main:
#include <iostream>
using namespace std;
#include "myLinkedList.h"
int main()
{
linkedList mylist;
//Step 1:
//Implement insertion to the front or the back of the list
mylist.addBack(2);
mylist.addBack(3);
mylist.addFront(5);
mylist.addFront(7);
mylist.addBack(11);
mylist.addBack(13);
mylist.addBack(17);
mylist.addFront(19);
mylist.addFront(23);
mylist.display(); //23 19 7 5 2 3 11 13 17
//Step 2:
//Implement removal of the first or last item
mylist.removeFront();
mylist.removeFront();
mylist.removeBack();
mylist.removeBack();
mylist.addFront(29);
mylist.addBack(31);
mylist.addBack(37);
mylist.addBack(41);
mylist.display(); //29 7 5 2 3 11 31 37 41
////Step 3:
////Implement a tricimation routine
//mylist.tricmate(); //delete every 3rd item
//mylist.display(); //29 7 2 3 31 37
////Step 4:
////Implement a find and remove method
//mylist.remove(3);
//mylist.remove(29);
//mylist.remove(37);
//mylist.addFront(43);
//mylist.addBack(47);
//mylist.display(); //43 7 2 31 47
////Step 5: sort!
//mylist.sort();
//mylist.display(); //2 7 31 43 47
return 0;
}
您问:
我的removeBack函数出了什么问题?
我看到的是:
void removeBack()
{
node * one = head;
node * two = one -> next; // When the list is empty, this
// will result in undefined behavior.
while(two != NULL)
{
if(two == NULL) // This is never true.
// You've already checked in the while
// statement that two != NULL.
// Hence, you never end up deleting the item
// at the back of the list.
{
delete one;
head -> next = two;
}
one = one -> next;
two = one -> next;
}
}
这应该有效:
void removeBack()
{
// First, check whether the list is empty
if ( head == NULL )
{
return;
}
// Next, check whether there is only one item
if ( head->next == NULL )
{
delete head;
head = NULL;
return;
}
// Now for the rest...
// iterate to get to the end.
node * iter = head;
while ( iter->next->next != NULL )
{
iter = iter -> next;
}
delete iter->next;
iter->next = NULL;
}
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 格式说明符C++有什么问题
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 我的堆栈和库存清单程序的结构有什么问题?
- 此工厂功能有什么问题?
- 以下 C++ 代码有什么问题?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 它解决了什么问题,对于非真空初始化,生命周期在初始化之前就开始了