Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;简单链表尝试:don';我不太明白什么';发生什么事了 我试图严格地用C++实现我目前正在研究的算法,简单链表的递归函数。以下是我得到的: #include <iostream> using namespace std; class Liste { private : int val; Liste *suivante; public : Liste(int val = 0, Liste *suivante = NULL) { this->val = val; this->suivante = suivante; } void afficherElement() const { cout << "Adresse : " << this << endl; cout << "Valeur : " << val << endl; cout << "Adresse suivante : " << suivante << endl; cout << endl; } int tete() const { return val; } Liste reste() const { return *suivante; } bool estVide() const { return (suivante == NULL); } Liste prefixer(int val) { Liste *nouvelle = new Liste(val, this); return *nouvelle; } Liste suffixer(int val) { suivante = new Liste(val); afficherElement(); // test (last element won't be displayed) return *suivante; } }; int main() { Liste uneListe(3); // 1st element uneListe.suffixer(5).suffixer(8).suffixer(10); // adding 3 more cout << "-----------\n\n"; uneListe.afficherElement(); // displaying 1st element : ok uneListe.reste().afficherElement(); // displaying 2nd element : pointer is NULL !!??? // uneListe.reste().reste().afficherElement(); --> segmentation fault, predictably enough return 0; } #包括 使用名称空间std; 班级名单{ 私人: int-val; 李斯特·苏万特; 公众: Liste(int val=0,Liste*suivante=NULL){ 这->val=val; 这->suivante=suivante; } void afficherElement()常量{ cout_C++_List_Linked List - Fatal编程技术网 val=val; 这->suivante=suivante; } void afficherElement()常量{ cout,c++,list,linked-list,C++,List,Linked List" /> val=val; 这->suivante=suivante; } void afficherElement()常量{ cout,c++,list,linked-list,C++,List,Linked List" />

C++;简单链表尝试:don';我不太明白什么';发生什么事了 我试图严格地用C++实现我目前正在研究的算法,简单链表的递归函数。以下是我得到的: #include <iostream> using namespace std; class Liste { private : int val; Liste *suivante; public : Liste(int val = 0, Liste *suivante = NULL) { this->val = val; this->suivante = suivante; } void afficherElement() const { cout << "Adresse : " << this << endl; cout << "Valeur : " << val << endl; cout << "Adresse suivante : " << suivante << endl; cout << endl; } int tete() const { return val; } Liste reste() const { return *suivante; } bool estVide() const { return (suivante == NULL); } Liste prefixer(int val) { Liste *nouvelle = new Liste(val, this); return *nouvelle; } Liste suffixer(int val) { suivante = new Liste(val); afficherElement(); // test (last element won't be displayed) return *suivante; } }; int main() { Liste uneListe(3); // 1st element uneListe.suffixer(5).suffixer(8).suffixer(10); // adding 3 more cout << "-----------\n\n"; uneListe.afficherElement(); // displaying 1st element : ok uneListe.reste().afficherElement(); // displaying 2nd element : pointer is NULL !!??? // uneListe.reste().reste().afficherElement(); --> segmentation fault, predictably enough return 0; } #包括 使用名称空间std; 班级名单{ 私人: int-val; 李斯特·苏万特; 公众: Liste(int val=0,Liste*suivante=NULL){ 这->val=val; 这->suivante=suivante; } void afficherElement()常量{ cout

C++;简单链表尝试:don';我不太明白什么';发生什么事了 我试图严格地用C++实现我目前正在研究的算法,简单链表的递归函数。以下是我得到的: #include <iostream> using namespace std; class Liste { private : int val; Liste *suivante; public : Liste(int val = 0, Liste *suivante = NULL) { this->val = val; this->suivante = suivante; } void afficherElement() const { cout << "Adresse : " << this << endl; cout << "Valeur : " << val << endl; cout << "Adresse suivante : " << suivante << endl; cout << endl; } int tete() const { return val; } Liste reste() const { return *suivante; } bool estVide() const { return (suivante == NULL); } Liste prefixer(int val) { Liste *nouvelle = new Liste(val, this); return *nouvelle; } Liste suffixer(int val) { suivante = new Liste(val); afficherElement(); // test (last element won't be displayed) return *suivante; } }; int main() { Liste uneListe(3); // 1st element uneListe.suffixer(5).suffixer(8).suffixer(10); // adding 3 more cout << "-----------\n\n"; uneListe.afficherElement(); // displaying 1st element : ok uneListe.reste().afficherElement(); // displaying 2nd element : pointer is NULL !!??? // uneListe.reste().reste().afficherElement(); --> segmentation fault, predictably enough return 0; } #包括 使用名称空间std; 班级名单{ 私人: int-val; 李斯特·苏万特; 公众: Liste(int val=0,Liste*suivante=NULL){ 这->val=val; 这->suivante=suivante; } void afficherElement()常量{ cout,c++,list,linked-list,C++,List,Linked List,类Liste包含一个值和一个引用,而不是列表:单链接列表是指向包含值的元素的指针和指向下一个节点的指针 您可以将value+指针元素用作列表对象,忽略val成员。这需要对某些方法进行不同的编码,例如tete()和reste() 但是,自从使用 typedef Liste * real_list_type; 这就是你的想法(?-见下文),让我们看看这些方法 bool estVide() const { return (suivante == NULL); } 这与真正的_list _类型仅仅是

类Liste包含一个值和一个引用,而不是列表:单链接列表是指向包含值的元素的指针和指向下一个节点的指针

您可以将value+指针元素用作列表对象,忽略val成员。这需要对某些方法进行不同的编码,例如tete()和reste()

但是,自从使用

typedef Liste * real_list_type;
这就是你的想法(?-见下文),让我们看看这些方法

bool estVide() const { return (suivante == NULL); }
这与真正的_list _类型仅仅是一个list*相矛盾;如果将其与方法reste()比较,它实际上是测试尾部是否为空,而不是列表本身!(它将与使用值+指针对象作为list对象同步。)

这是不好的:它用一个新的对象替换suivante,不管其中存储了什么(内存泄漏)。你必须这样做

Liste suffixer(int val) {
  if( suivante == NULL ){
     suivante = new Liste(val);
  } else {
     suivante->suffixer( val );
  }
  return *this;
}
以后

我认为这是使它尽可能接近抽象概念的最好方法。注意,没有“isEmpty”-这是通过测试表示列表的List*变量是否等于NULL来完成的,但是您不能有一个方法来实现

template<typename T>
class List {
public:
  List( T v, List* t = nullptr ) : value(v), next(t){}
  ~List(){ delete next; }
  List* prepend( T v ){
    return new List( v, this );
  }
  List* append( T v ){
    if( next == nullptr ){
      next = new List( v );
    } else {
      next->append( v );
    }      
    return this;
  }
  T head(){ return value; }
  List* tail(){ return next; }
  void dump(){
    List* curr = this;
    std::string del = "";
    while( curr != nullptr ){
      std::cout << del << curr->value;
      del = ", ";
      curr = curr->next;
    }
   std::cout << std::endl;
  }

private:
  T value;
  List* next;
};

int main(){
  typedef List<int> * intList;
  intList list = new List<int>( 1 );
  list->append( 2 )->append( 3 );
  list->dump();
}
模板
班级名单{
公众:
List(tv,List*T=nullptr):值(v),下一个(T){
~List(){删除下一步;}
列表*前置(TV){
返回新列表(v,本);
}
列表*附加(TV){
if(next==nullptr){
下一步=新列表(v);
}否则{
下一步->追加(v);
}      
归还这个;
}
T head(){返回值;}
List*tail(){return next;}
无效转储(){
列表*curr=此;
std::string del=“”;
while(curr!=nullptr){
std::cout next;
}
std::cout append(2)->append(3);
列表->转储();
}

对于右侧此问题,您最需要更改此行
列表后缀(int-val)

列表*后缀(int-val)

然后更改这一行
return*suivante;

返回suivante;
主要使用这一行
uneListe.suffix(5)->suffix(8)->suffix(10);
而不是

uneListe.supfixer(5).supfixer(8).supfixer(10);

您的类方法
Liste::prefixer(int-val)
Liste-supfixer(int-val)
将返回所创建对象的副本,它们应该返回指向该对象(或引用)的指针

e、 g

以下是我第一次尝试的“修复”版本:

#include <iostream>
using namespace std;

class Liste {
private :
    int val;
    bool vide;
    Liste *suivante;
public :
    Liste(int val = 0, bool vide = true, Liste *suivante = NULL) {
        this->val = val;
        this->vide = vide;
        this->suivante = suivante;
    }
    void afficherElement() const {
        cout << "Adresse : " << this << endl;
        cout << "Valeur : " << val << endl;
        cout << "Vide : " << vide << endl;
        cout << "Adresse suivante : " << suivante << endl;
        cout << endl;
    }
    int head() const {
        return val;
    }
    Liste *reste() const {
        return suivante;
    }
    bool estVide() const {
        return vide;
    }
    Liste *prefixer(int val) {
        Liste *nouvelle = new Liste(val, this);
        return nouvelle;
    }
    Liste *suffixer(int val) {
        if(suivante == NULL) {
            suivante = new Liste(val);
            vide = false;
        }
        return suivante;
    }
};

void afficherListe(Liste *uneListe) {
    (*uneListe).afficherElement();
    if(!(*uneListe).estVide()) {
        afficherListe((*uneListe).reste());
    }
}

int main() {
    Liste *test = new Liste(3);
    (*test).suffixer(5);
    afficherListe(test);
    return 0;
}
#包括
使用名称空间std;
班级名单{
私人:
int-val;
布尔维德;
李斯特·苏万特;
公众:
Liste(int val=0,bool vide=true,Liste*suivante=NULL){
这->val=val;
这->视频=视频;
这->suivante=suivante;
}
void afficherElement()常量{

cout我能看到的最大问题是,后缀器/前缀器是“列表”的返回副本,而不是引用。因此,它的外观是
。后缀器(8)
应用于列表对象的副本,而不是您在调用
中创建的副本。后缀器(5)
。您可以尝试通过引用返回后缀器和前缀器吗?例如,Liste&supfixer(int-val)。我认为问题在于,第二个元素之后添加的下一个元素将被添加到另一个列表中,因为您按值返回。如果您进行调试,您可能会看到这一点。另一方面,如果我可以提出建议,我将实际进行设计更改。有一个class
Liste
和一个class
Noeud
。其中基本上是列表有一个指向第一个节点的指针,负责将节点链接在一起。列表不是当前的节点。您可以使用
nullptr
而不是NULL吗?这更像是c++-EY。返回的值是一个错误,它解释了指针值和对象地址的差异。谢谢。将发布一个cod谢谢你的解释。我知道我的列表类不是一个合适的列表,我试图坚持我们在算法课程中练习的相当抽象的“递归列表”概念(一个“包含”列表的列表,“包含”列表等)。Suffix()显然存在内存问题,的确如此。@user3262284好吧,我试着涵盖所有角度。但是estVide不符合你的概念。嗯,谢谢。我要学习你的代码,我以前从未使用过模板。
Liste *Liste::suffixer(int val){
    if(suivante == nullptr)
        suivante = new Liste(val);
    else
        throw std::runtime_error("Generic error message");

    return suivante;
}
Liste &Liste::suffixer(int val){

    ... previous inner method ...

    return *suivante;
}
#include <iostream>
using namespace std;

class Liste {
private :
    int val;
    bool vide;
    Liste *suivante;
public :
    Liste(int val = 0, bool vide = true, Liste *suivante = NULL) {
        this->val = val;
        this->vide = vide;
        this->suivante = suivante;
    }
    void afficherElement() const {
        cout << "Adresse : " << this << endl;
        cout << "Valeur : " << val << endl;
        cout << "Vide : " << vide << endl;
        cout << "Adresse suivante : " << suivante << endl;
        cout << endl;
    }
    int head() const {
        return val;
    }
    Liste *reste() const {
        return suivante;
    }
    bool estVide() const {
        return vide;
    }
    Liste *prefixer(int val) {
        Liste *nouvelle = new Liste(val, this);
        return nouvelle;
    }
    Liste *suffixer(int val) {
        if(suivante == NULL) {
            suivante = new Liste(val);
            vide = false;
        }
        return suivante;
    }
};

void afficherListe(Liste *uneListe) {
    (*uneListe).afficherElement();
    if(!(*uneListe).estVide()) {
        afficherListe((*uneListe).reste());
    }
}

int main() {
    Liste *test = new Liste(3);
    (*test).suffixer(5);
    afficherListe(test);
    return 0;
}