从C+;中的链表中删除元素+; 我现在正努力学习C++,因为我要学习一门课,我来自java。我目前正在阅读《跳进C++》一书并完成练习。在阅读了关于链表的部分之后,它告诉我创建自己的链表,并有一个删除元素的方法(在练习中使用指针)
到目前为止,我已经能够为我的链表添加值,并显示我的链表。执行remove-element方法后,让程序明确地告诉我它已经删除了特定内存地址上的值,我再次显示列表,发现我的值仍然以某种方式出现在假定已删除的内存地址上 这是我的删除方法:从C+;中的链表中删除元素+; 我现在正努力学习C++,因为我要学习一门课,我来自java。我目前正在阅读《跳进C++》一书并完成练习。在阅读了关于链表的部分之后,它告诉我创建自己的链表,并有一个删除元素的方法(在练习中使用指针),c++,list,pointers,linked-list,C++,List,Pointers,Linked List,到目前为止,我已经能够为我的链表添加值,并显示我的链表。执行remove-element方法后,让程序明确地告诉我它已经删除了特定内存地址上的值,我再次显示列表,发现我的值仍然以某种方式出现在假定已删除的内存地址上 这是我的删除方法: // remove an element from the linked list void removeElement(int remValue) { // to remove an element, we go through the list, fi
// remove an element from the linked list
void removeElement(int remValue) {
// to remove an element, we go through the list, find the value given
// if we find it, stop
// to remove, disconnect the link
// relink the two values now (ie. value 1->2->3->NULL, 2 is removed, 1->3->NULL )
LinkedList* current = head;
LinkedList* next = current;
while(current != NULL) {
if(current->value == remValue) { // if match
break; // break out of while
}
else {
cout << "Value " << current->value << " does not match " << remValue << ".\n";
next = current; // save in case
current = current->pNextValue; // go to next value
}
} // end while
if(current == NULL) { // if we reached end of list
cout << "Can't remove value: no match found.\n"; // no match, cant remove
} else { // found match
cout << "Deleting: " << current << "\n";
delete current;
current = next->pNextValue; // current is updated
}
}
//从链表中删除元素
void removelement(int remValue){
//要删除一个元素,我们遍历列表,找到给定的值
//如果我们找到了,停下来
//要拆卸,请断开连杆
//现在重新链接两个值(即值1->2->3->NULL,2被删除,1->3->NULL)
LinkedList*当前=头;
LinkedList*next=当前;
while(当前!=NULL){
如果(当前->值==remValue){//if匹配
break;//中途中断
}
否则{
cout您实际上并没有取消您删除的节点的链接
您需要跟踪上一个节点,并使其next
指针指向当前节点next
节点。还要考虑一种特殊情况,即要删除的节点是第一个节点。您实际上没有取消删除的节点的链接
您需要跟踪上一个节点,并使其next
指针指向当前节点next
节点。还需要考虑当要删除的节点是第一个节点时的特殊情况。您需要更新列表中的链接以删除删除的节点
请注意,使用delete
操作符不会更改内存中的任何值。它只是告诉操作系统您不再使用内存中的该位置,它可以将其回收用于其他用途。您需要更新列表中的链接以删除删除的节点
请注意,使用delete
操作符不会更改内存中的任何值。它只是告诉操作系统您不再使用内存中的该位置,它可以将其回收用于其他用途。@Joachim Pileborg是正确的,您需要记录上一个节点,而不是下一个
请尝试以下代码:
// remove an element from the linked list
void removeElement(int remValue) {
LinkedList* prev = head; // empty header
LinkedList* current = head->pNextValue; // the first valid node
while(current != NULL) {
if(current->value == remValue) {
break;
}
else {
cout << "Value " << current->value << " does not match " << remValue << ".\n";
prev = current;
current = current->pNextValue; // go to next value
}
}
if(current == NULL) { // if we reached end of list or the list is empty
cout << "Can't remove value: no match found.\n";
} else {
cout << "Deleting: " << current << "\n";
prev->pNextValue = current->pNextValue; // unlink the node you remove
delete current; // delete the node
}
}
//从链表中删除元素
void removelement(int remValue){
LinkedList*prev=head;//头为空
LinkedList*current=head->pNextValue;//第一个有效节点
while(当前!=NULL){
如果(当前->值==remValue){
打破
}
否则{
cout@Joachim Pileborg是对的,您需要记录上一个节点,而不是下一个
请尝试以下代码:
// remove an element from the linked list
void removeElement(int remValue) {
LinkedList* prev = head; // empty header
LinkedList* current = head->pNextValue; // the first valid node
while(current != NULL) {
if(current->value == remValue) {
break;
}
else {
cout << "Value " << current->value << " does not match " << remValue << ".\n";
prev = current;
current = current->pNextValue; // go to next value
}
}
if(current == NULL) { // if we reached end of list or the list is empty
cout << "Can't remove value: no match found.\n";
} else {
cout << "Deleting: " << current << "\n";
prev->pNextValue = current->pNextValue; // unlink the node you remove
delete current; // delete the node
}
}
//从链表中删除元素
void removelement(int remValue){
LinkedList*prev=head;//头为空
LinkedList*current=head->pNextValue;//第一个有效节点
while(当前!=NULL){
如果(当前->值==remValue){
打破
}
否则{
不能包含
#包括
使用名称空间std;
类节点
{
公众:
节点*下一步;
int数据;
Node();
~Node();
作废打印();
};
类链接列表
{
公众:
整数长度;
节点*头;
LinkedList();
~LinkedList();
无效添加(整数数据);
无效删除(int数据);
节点*搜索(整数数据);
作废打印();
孔隙大小();
};
Node::Node(){
//设置默认值;
}
节点::~Node(){
下一步;
删除当前文件;
这个->长度--;
}否则{
节点*previous=this->head;
节点*当前=头部->下一步;
while(当前!=NULL){
如果(当前->数据==数据){
打破
}
否则{
先前=当前;
当前=当前->下一步;
}
}
如果(当前==NULL){
cout next=当前->下一步;
删除当前文件;
这个->长度--;
}
}
}
节点*LinkedList::搜索(int数据){
节点*头部=此->头部;
while(head){
如果(头部->数据==数据){
回流头;
}否则{
头部=头部->下一步;
}
}
cout长度==0){
cout搜索(106);
节点->打印();
删除名单;
返回0;
}
#包括
#包括
使用名称空间std;
类节点
{
公众:
节点*下一步;
int数据;
Node();
~Node();
作废打印();
};
类链接列表
{
公众:
整数长度;
节点*头;
LinkedList();
~LinkedList();
无效添加(整数数据);
无效删除(int数据);
节点*搜索(整数数据);
作废打印();
孔隙大小();
};
Node::Node(){
//设置默认值;
}
节点::~Node(){
下一步;
删除当前文件;
这个->长度--;
}否则{
节点*previous=this->head;
节点*当前=头部->下一步;
while(当前!=NULL){
如果(当前->数据==数据){
打破
}
否则{
先前=当前;
当前=当前->下一步;
}
}
如果(当前==NULL){
cout next=当前->下一步;
删除当前文件;
这个->长度--;
}
}
}
节点*LinkedList::搜索(int数据){
节点*头部=此->头部;
while(head){
如果(头部->数据==数据){
回流头;
}否则{
头部=头部->下一步;
}
}
cout长度==0){
cout搜索(106);
节点->打印();
删除名单;
返回0;
}
作为一个旁注,在现代C++中,你不需要真的<代码>删除<代码>(C++中的14也不是真的<代码>新< /代码>)