列表实现无法正确求和列表 我试图挖掘C++中的数据结构。因此,我正在学习如何写一份清单。一切似乎都在正常工作,直到重载总和运算符+。对于两个给定的列表,它将列表中的两个最高值相加

列表实现无法正确求和列表 我试图挖掘C++中的数据结构。因此,我正在学习如何写一份清单。一切似乎都在正常工作,直到重载总和运算符+。对于两个给定的列表,它将列表中的两个最高值相加,c++,data-structures,C++,Data Structures,以下是.h文件: typedef struct rob{ int value; struct rob* next; }element; class list{ public: friend list& operator+(list&,list&); friend void merge(list& x,list& y); void show(); bool search(int x); void a

以下是.h文件:

typedef struct rob{
    int value;
    struct rob* next;
}element;

class list{
public:
    friend list& operator+(list&,list&);
    friend void merge(list& x,list& y);

    void show();
    bool search(int x);
    void append(int x);
    bool sortAppend(int x);
    list& operator--(int);

    bool empty() { return (inf.head==nullptr);}
    void clear() { inf.head = nullptr; }
    list() { inf.head = inf.tail = nullptr; }
    ~list() { while(!empty()) { (*this)--;}}

private:
    typedef struct{
        element* head;
        element* tail;
    }info;

    info inf;
};
我知道在.h文件中,typedef可能看起来有点像C,但标题设计是从我正在学习的书中复制的。我试图用自己的想法来破解这些方法


以及相关的功能定义:

#include "list.h"
bool list::sortAppend(int x){

element* newElem = new element;
newElem->value = x;
if (empty()){
    inf.head=inf.tail=newElem;
    newElem->next=nullptr;
    return true;
}
else if ( (newElem->value) < (inf.head->value) ){ 
    newElem->next=inf.head;
    inf.head=newElem;
    return true;
}
else if ( (newElem->value) > (inf.tail->value) ) {
    newElem->next=nullptr;
    inf.tail->next=newElem;
    return true;
}
element* tempHead = inf.head;
while(tempHead!=inf.tail){

    if ( (newElem->value) < (tempHead->next)->value) {
           newElem->next = (tempHead->next);
           tempHead->next = newElem;
           return true;
    }
    else{
    tempHead = tempHead->next;
    }   
}
return false;
}

list& operator+(list& X, list& Y){
    list* tempListArr[2] = {&X, &Y};
    list* tempList = new list;
    for(const list* i: tempListArr)
    {
        element* tempHead = (i->inf).head;
        while(tempHead!= nullptr){
            tempList->sortAppend(tempHead->value);
            tempHead = tempHead->next;
            }   
            tempList->show();
            std::cout << "--\n";
    }
    return *tempList;
}
#包括“list.h”
布尔列表::sortAppend(int x){
元素*newElem=新元素;
newElem->value=x;
if(空()){
inf.head=inf.tail=newElem;
newElem->next=nullptr;
返回true;
}
如果((newElem->value)<(inf.head->value)){
newElem->next=inf.head;
inf.head=newElem;
返回true;
}
else if((newElem->value)>(inf.tail->value)){
newElem->next=nullptr;
inf.tail->next=newElem;
返回true;
}
元素*tempHead=inf.head;
while(tempHead!=inf.tail){
如果((新元素->值)<(临时标题->下一步)->值){
newElem->next=(tempHead->next);
tempHead->next=newElem;
返回true;
}
否则{
tempHead=tempHead->next;
}   
}
返回false;
}
列表和运算符+(列表和X、列表和Y){
列表*tempListArr[2]={&X,&Y};
list*templast=新列表;
用于(常数列表*i:tempListArr)
{
元素*tempHead=(i->inf).head;
while(tempHead!=nullptr){
圣堂武士->排序结束(临时头->值);
tempHead=tempHead->next;
}   
圣堂武士->秀();

std::cout您只是没有将inf.tail设置为中的新tail

else if ((newElem->value) > (inf.tail->value)) {
    newElem->next = nullptr;
    inf.tail->next = newElem;
    inf.tail = newElem; // <-- missing!
    return true;
}
else if((newElem->value)>(inf.tail->value)){
newElem->next=nullptr;
inf.tail->next=newElem;

inf.tail=newElem;//您只是没有将inf.tail设置为

else if ((newElem->value) > (inf.tail->value)) {
    newElem->next = nullptr;
    inf.tail->next = newElem;
    inf.tail = newElem; // <-- missing!
    return true;
}
else if((newElem->value)>(inf.tail->value)){
newElem->next=nullptr;
inf.tail->next=newElem;
inf.tail=newElem;//给定您的代码

list myListWierd;
myListWierd.sortAppend(2);
myListWierd.sortAppend(4);
myListWierd.sortAppend(5);
myListWierd.show();
显示

2
5
因此,
sortAppend
不起作用

问题在于更新尾部,因为
operator+
依赖于使用尾部

我可以为您整理代码并使其工作;安德烈亚斯的答案确实可以做到这一点。但现在,请注意,您假设函数可以工作,但我通过查看移动部分(我们创建的列表)发现它不工作,然后我们尝试以不同的顺序重新创建。作为一般规则,请尝试函数中的所有部分rong,一次一个,可能作为单元测试

现在,让我们提出一些建议,而不是解决这个问题

首先,析构函数除了遍历指针(使用
empty
,它使用
head
而不是tail,所以
head
需要像前面所说的那样设置)

如果你不想泄密,你需要多考虑一下

其次,

list& operator+(list&, list&)
创建指针并返回其内容。这不是一个好主意。永远不要这样做

现在,将签名更改为

list operator+(list&, list&);
然后返回一个列表:

list operator+(list& X, list& Y) {
    list* tempListArr[2] = { &X, &Y };
    list tempList;//copy it over to the calling vode otherwise DANGER
    for (const list* i : tempListArr)
    {
        element* tempHead = (i->inf).head;
        while (tempHead != nullptr) {
            tempList.sortAppend(tempHead->value);
            std::cout << "Adding " << tempHead->value << '\n';
            tempHead = tempHead->next;
        }
        tempList.show();
        std::cout << "--\n";
    }
    return tempList;
}
list操作符+(list&X,list&Y){
列表*tempListArr[2]={&X,&Y};
list templast;//将其复制到调用的vode,否则会造成危险
用于(常数列表*i:tempListArr)
{
元素*tempHead=(i->inf).head;
while(tempHead!=nullptr){
圣堂武士:sortAppend(tempHead->value);
std::cout给定您的代码

list myListWierd;
myListWierd.sortAppend(2);
myListWierd.sortAppend(4);
myListWierd.sortAppend(5);
myListWierd.show();
显示

2
5
因此,
sortAppend
不起作用

问题在于更新尾部,因为
operator+
依赖于使用尾部

我可以为您整理代码并使其工作;安德烈亚斯的答案确实可以做到这一点。但现在,请注意,您假设函数可以工作,但我通过查看移动部分(我们创建的列表)发现它不工作,然后我们尝试以不同的顺序重新创建。作为一般规则,请尝试函数中的所有部分rong,一次一个,可能作为单元测试

现在,让我们提出一些建议,而不是解决这个问题

首先,析构函数除了遍历指针(使用
empty
,它使用
head
而不是tail,所以
head
需要像前面所说的那样设置)

如果你不想泄密,你需要多考虑一下

其次,

list& operator+(list&, list&)
创建指针并返回其内容。这不是一个好主意。永远不要这样做

现在,将签名更改为

list operator+(list&, list&);
然后返回一个列表:

list operator+(list& X, list& Y) {
    list* tempListArr[2] = { &X, &Y };
    list tempList;//copy it over to the calling vode otherwise DANGER
    for (const list* i : tempListArr)
    {
        element* tempHead = (i->inf).head;
        while (tempHead != nullptr) {
            tempList.sortAppend(tempHead->value);
            std::cout << "Adding " << tempHead->value << '\n';
            tempHead = tempHead->next;
        }
        tempList.show();
        std::cout << "--\n";
    }
    return tempList;
}
list操作符+(list&X,list&Y){
列表*tempListArr[2]={&X,&Y};
list templast;//将其复制到调用的vode,否则会造成危险
用于(常数列表*i:tempListArr)
{
元素*tempHead=(i->inf).head;
while(tempHead!=nullptr){
圣堂武士:sortAppend(tempHead->value);

std::您是否应该逐行进行调试以缩小问题范围。您的
清除
函数有几个不相关的问题。它会泄漏内存;而且不会“清除”尾部指针。您的
运算符+
函数中也存在内存泄漏。请参阅,例如,了解如何编写非成员运算符函数,如
运算符+
。这里有一件危险的事情:运算符+返回对没有所有者的堆对象的引用。如果此问题已修复(即,通过返回本地对象)赋值<代码> MyList3=…./>将不再工作,因为没有复制构造函数,它只复制指针,只要本地对象超出范围,就会释放指针。@ HikAMARE——使用C++来读取基本数据结构的问题是通过读取数据结构书,不能简单地使用算法。首先是学习正确的内存管理,实现的是正确的基础。