C++ 删除双链表c++;
我正在尝试为我的双链表类实现一个remove函数C++ 删除双链表c++;,c++,linked-list,doubly-linked-list,dev-c++,C++,Linked List,Doubly Linked List,Dev C++,我正在尝试为我的双链表类实现一个remove函数 #include <iostream> #include <string> #include <vector> #include <stdio.h> #include <stdlib.h> using namespace std; class DblLinkedBag { private: struct node{ strin
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class DblLinkedBag
{
private:
struct node{
string data;
node* next;
node* prev;
}*start=NULL;
int itemCount;
string item;
node *head;
node *tail;
public:
DblLinkedBag();
~DblLinkedBag();
int getCurrentSize();
bool isEmpty();
bool add(string value);
bool remove(string item);
void clear();
bool contains(string target);
int getFrequencyOf();
string retStart();
string getItem();
};
当我尝试运行它时,什么也没有发生,它甚至没有返回false。这可能是因为我实现add函数的方式吗?这是我的add函数
bool DblLinkedBag::remove(string value)
{
node* to_remove = head;
while(to_remove && to_remove->data != item)
to_remove = to_remove->next;
// Do the removal if we found it
if(to_remove)
{
// If it was at the head, advance the head to the next item
if(to_remove == head)
head = head->next;
// Remove from the list
if(to_remove->next)
to_remove->next->prev = to_remove->prev;
if(to_remove->prev)
to_remove->prev->next = to_remove->next;
// Free the removed node
delete to_remove;
itemCount--;
return true;
}
return false;
bool DblLinkedBag::add(string value)
{
node* n;
bool add=false;
cout<<itemCount<<endl;
if(itemCount==0)
{
n=new node;
n->data=value;
n->prev=NULL;
head=n;
tail=n;
add=true;
}
if(itemCount>0 && itemCount<7)
{
n= new node;
n->data=value;
n->prev=tail;
tail->next=n;
tail=n;
add=true;
}
itemCount++;
return add;
}
bool DblLinkedBag::add(字符串值)
{
节点*n;
bool add=false;
库特普雷夫=尾巴;
tail->next=n;
尾=n;
加法=真;
}
itemCount++;
返回添加;
}
任何帮助都将不胜感激
这就是我调用函数的方式
void displayBag(int size)
{ DblLinkedBag bag;
cout << "The bag contains " <<size
<< " items:" << endl;
int numberOfEntries = size;
//for (int i = 0; i < numberOfEntries; i++)
// {
// cout <<bagItems[i] << " ";
// } // end for
// cout << endl << endl;
} // end displayBag
void copyConstructorTester()
{
DblLinkedBag bag;
string items[6] = {"zero", "one", "two", "three", "four", "five"};
for (int i = 0; i < 6; i++)
{
cout << "Adding " << items[i] << endl;
bag.add(items[i]);
// bool success = bag.add(items[i]);
//if (!success)
// cout << "Failed to add " << items[i] << " to the bag." << endl;
}
void bagTester()
{
DblLinkedBag bag;
cout << "Testing the Link-Based Bag:" << endl;
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 1 (true)" << endl;
cout<<"*BAG TESTER*"<<endl;
displayBag(bag.getCurrentSize());
string items[] = {"one", "two", "three", "four", "five", "one"};
cout << "Add 6 items to the bag: " << endl;
for (int i = 0; i < 6; i++)
{
bag.add(items[i]);
} // end for
displayBag(bag.getCurrentSize());
bag.display();
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 0 (false)" << endl;
cout << "getCurrentSize: returns " << bag.getCurrentSize()
<< "; should be 6" << endl;
cout << "Try to add another entry: add(\"extra\") returns "
<< bag.add("extra") << endl;
cout << "contains(\"three\"): returns " << bag.contains("three")
<< "; should be 1 (true)" << endl;
// cout << "contains(\"ten\"): returns " << bag.contains("ten")
// << "; should be 0 (false)" << endl;
// cout << "getFrequencyOf(\"one\"): returns "
// << bag.getFrequencyOf("one") << " should be 2" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 1 (true)" << endl;
// cout << "getFrequencyOf(\"one\"): returns "
// << bag.getFrequencyOf("one") << " should be 1" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 1 (true)" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 0 (false)" << endl;
// cout << endl;
displayBag(bag.getCurrentSize());
cout << "After clearing the bag, ";
bag.clear();
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 1 (true)" << endl;
} // end bagTester
int main()
{
copyConstructorTester();
bagTester();
return 0;
} // end main
void displayBag(整数大小)
{dbllinkabag袋;
cout这是如何绘制双链接列表来计算节点的删除
Node 1
+----------+------+------+
| Previous | Data | Next |
| -0- | ? | |
+----------+------+------+
^ |
| Node 2 V
+----------+------+------+
| Previous | Data | Next |
+----------+------+------+
^ |
| Node 3 V
+----------+------+------+
| Previous | Data | Next |
| | ? | -0- |
+----------+------+------+
让我们假设以下指针:
Node * node1 = head;
Node * node2(node1->next);
Node * node3(node2->next);
一种可能是从前面的链接开始:
将节点3的上一个链接点设置为节点1:
node3->previous = node1;
Node 1
+----------+------+------+
| Previous | Data | Next |
| -0- | ? | |
+----------+------+------+
^ ^ |
| | |
+--+ | |
| | Node 2 V
| +------+------+------+
| | Prev | Data | Next |
| +------+------+------+
| |
+---+ |
| Node 3 V
+----------+------+------+
| Previous | Data | Next |
| | ? | -0- |
+----------+------+------+
下一步是使节点1的next
链接指向节点3:
node1->next=node3;
Node 1
+----------+------+------+
| Previous | Data | Next |
| -0- | ? | |
+----------+------+------+
^ ^ |
| | |
+--+ | +------+
| | Node 2 |
| +------+------+------+ |
| | Prev | Data | Next | |
| +------+------+------+ |
| | |
| | +-----+
+---+ | |
| Node 3 V V
+----------+------+------+
| Previous | Data | Next |
| | ? | -0- |
+----------+------+------+
在上图中,当您跟踪从节点1
到节点3
和节点3
到节点1
的链接时,您会注意到未访问节点2
因此,可以安全地删除节点2
最后,删除节点2
delete node2;
总之:
Node * current = head;
Node * previous = head;
// Find the node
while (current->data != key)
{
previous = current;
current = current->next;
}
// Assume node was found.
Node after = current->next;
// Make previous point to the node after the current.
previous->next = after;
// Make the node after the current node point to the previous.
if (after != NULL)
{
after->previous = previous;
}
// Delete the current node.
delete current;
你是否使用调试器来完成代码?你调用了“删除”吗?@ ANNEDAR。我没有画出双链接列表中的3个元素及其链接的图片。取一个红色的铅笔,画出中间移除项目时会改变的链接。检查代码是否符合预期。重复第一个步骤。唱它是列表的开始,如果它是列表的结束,那么它是最后一个元素。然后你应该这样做——它会给你很多关于正在发生什么以及为什么发生的信息。