【发布时间】:2016-06-11 17:47:11
【问题描述】:
我正在尝试为我的双向链表类实现删除功能。
#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();
};
这就是目前的移除功能。
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;
当我尝试运行它时,什么也没有发生,它甚至不返回 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;
}
任何帮助将不胜感激。
这就是我调用函数的方式。
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
【问题讨论】:
-
您是否使用调试器单步调试您的代码?
-
你调用删除了吗?
-
@Anedar 不,我没有
-
在你的双链表及其链接中画出 3 个元素的图片。拿一支红铅笔,画出当你移除中间的项目时会改变的链接。检查您的代码是否符合预期。迭代第一个元素,假设它是列表的开头,最后一个元素,如果它是列表的结尾。
-
那么你应该这样做 - 它会给你很多关于正在发生的事情和原因的信息。
标签: c++ linked-list doubly-linked-list dev-c++