C++ 我的removeBack功能有什么问题?
我制作了一个链表,我的其他函数也可以工作,但是我的removeBack不能。我遍历节点,检查它们是否为NULL,如果节点为NULL,我将链接器复制到指向倒数第二个的上一个指针,并删除包含NULL值的节点。我确信这是有效的,直到两个应该被删除的号码没有被删除。这是我的密码。有人能带我到这里吗C++ 我的removeBack功能有什么问题?,c++,linked-list,C++,Linked List,我制作了一个链表,我的其他函数也可以工作,但是我的removeBack不能。我遍历节点,检查它们是否为NULL,如果节点为NULL,我将链接器复制到指向倒数第二个的上一个指针,并删除包含NULL值的节点。我确信这是有效的,直到两个应该被删除的号码没有被删除。这是我的密码。有人能带我到这里吗 #include <string> #include <iostream> using namespace std; class node { public: int dat
#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;
}
}
};
#包括
#包括
使用名称空间std;
类节点
{
公众:
int数据;
节点*下一步;
};
类链接列表
{
私人:
节点*头;
公众:
linkedList()
{
head=NULL;
}
void addFront(int x)
{
node*babynode=新节点;
//(*babynode)。数据=x;
babynode->data=x;
babynode->next=头部;
头部=巴氏管;
}
无效显示()
{
节点*手指=头部;
while(finger!=NULL)
{
下一步是收集数据;
}
}
//删除并返回列表中的第一项
void removeFront()
{
if(head!=NULL)
{
节点*前端=头部;
头部=头部->下一步;
删除前端;
}
其他的
{
head=NULL;
}
}
void addBack(int x)
{
节点*尾部=头部;
//只有头部是空的
if(head==NULL)
{
addFront(x);
}
//如果它不是空的
否则{
while(tail->next!=NULL)
{
tail=tail->next;
}
节点*tail2=新节点;
tail2->data=x;
tail2->next=tail->next;
tail->next=tail2;
}
}
void removeBack()
{
节点*1=头部;
节点*two=one->next;
while(两个!=NULL)
{
if(two==NULL)
{
删除一项;
头->下一个=两个;
}
一个=一个->下一个;
二=一->下一个;
}
}
};
主要内容:
#包括
使用名称空间std;
#包括“myLinkedList.h”
int main()
{
linkedList mylist;
//步骤1:
//在列表的前面或后面插入
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
//步骤2:
//执行第一项或最后一项的删除
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
////步骤3:
////执行一个tricimation例程
//mylist.tricmate();//每三项删除一次
//mylist.display();//29 7 2 3 31 37
////步骤4:
////实现查找并删除方法
//mylist.remove(3);
//mylist.remove(29);
//mylist.remove(37);
//mylist.addFront(43);
//mylist.addBack(47);
//mylist.display();//43 7 2 31 47
////第五步:分类!
//mylist.sort();
//mylist.display();//2 7 31 43 47
返回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;
}
对下一步是打开调试器并进行一些调试。这是可行的,但我不得不修改它,因为我们不允许使用->下一步->下一步@SamPerales,这是一个奇怪的要求。哦,好吧。只要你有一个有效的解决方案,一切都好。
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;
}