This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                7年前关闭。
            
                    
它仅返回一个地址,没有任何调试器错误,尽管我的DEV C ++和Code :: Blocks编译器都显示send dont send windows错误,但它们仅初始化类对象,我已经包含了代码,有人可以告诉我为什么发生这种情况

#include <iostream>
#include <conio.h>
using namespace std;
struct Node
{
    int data;
    Node *nextptr;
};


class CLLIST{

private:

     Node*firstptr;
     Node*lastptr;

public:
     CLLIST(){

     cout << "Constructor Called !";
      firstptr=lastptr=NULL;
}

void insert_at_back(int val){

         if(firstptr==NULL) //it means start of C.LIST
         {
             Node*temptr = new Node; //means firstptr = temptr
             firstptr->data=val;
             firstptr=temptr;
             firstptr->nextptr=firstptr;
         } else{

             Node*temp1 = new Node;
             Node*temp2 = new Node;

             temp1 = firstptr;
             while(temp1->nextptr!=firstptr) //traversing
             {
                 temp2 = temp1->nextptr;
                 temp2->data = val; //inserted at back
                 temp2->nextptr=firstptr; //circle completed
             }
         }
}

void printit(){

           // functiont o print all the circular link lists data
           Node*temp3ptr= new Node;
           temp3ptr = firstptr;

           while(temp3ptr->nextptr!=firstptr)//traversing
           {
              cout << temp3ptr->data;
              cout << endl;
           }
}
};


  int main()
  {
    CLLIST obj1;

    obj1.insert_at_back(10);
    obj1.insert_at_back(20);
    obj1.insert_at_back(30);

    obj1.printit();

    cout << "Done !";

    getch();
  }

最佳答案

当前代码存在一些问题(可能还有更多问题,但是您应该首先解决这些问题)



问题1:

         if(firstptr==NULL) //it means start of C.LIST
         {
             Node*temptr = new Node; //means firstptr = temptr
             firstptr->data=val;
             firstptr=temptr;
             firstptr->nextptr=firstptr;
         } else{


^对于firstptr->data=val;,即使它仍是firstptr,您也要取消引用NULL。交换它和下一行,使其显示为:

         if(firstptr==NULL) //it means start of C.LIST
         {
             Node*temptr = new Node; //means firstptr = temptr
             firstptr=temptr;
             firstptr->data=val;
             firstptr->nextptr=firstptr;
         } else{


或者更好的方法是:直接firstptr = new Node;并跳过temptr



问题2:

     Node*temp1 = new Node;
     Node*temp2 = new Node;

     temp1 = firstptr;
     while(temp1->nextptr!=firstptr) //traversing
     {
         temp2 = temp1->nextptr;
         temp2->data = val; //inserted at back
         temp2->nextptr=firstptr; //circle completed
     }


^这是内存泄漏; new为它们在堆上分配内存,当您分配临时节点指向firstptr时,其地址会丢失。只需声明temp1temp2即可:

     Node*temp1;
     Node*temp2;




问题3:

    while(temp1->nextptr!=firstptr)


^此while循环将永远不会运行,因为:


您以firstptr开头为null
然后,您添加一个节点,并且firstptr->next指向firstptr
然后,当您尝试添加第二个节点时,它完成了分配temp1 = firstptr;的工作,但是while循环未运行,因为firstptr->next == firstptr




问题4:

正如@aleguna指出的那样:

       Node*temp3ptr= new Node;
       temp3ptr = firstptr;


^这是另一个内存泄漏,出于与问题2中所述相同的原因。只需声明temp3ptr即可:

       Node*temp3ptr;




问题5:

       while(temp3ptr->nextptr!=firstptr)//traversing
       {
          cout << temp3ptr->data;
          cout << endl;
       }


^这里您需要一种方法来实际遍历循环链接列表,现在它只是重复打印第一个节点(嗯,从技术上讲,代码的其他部分还不允许您将第二个节点添加到链接列表中, )

就像是:

       while(temp3ptr->nextptr!=firstptr)//traversing
       {
          cout << temp3ptr->data;
          cout << endl;
          // Can technically do it without the if-check since this
          // is circular, but better to code defensively.
          if (temp3ptr->next != NULL) {
              temp3ptr = temp3ptr->next;
          }
       }

07-24 09:52