0

デバッガーエラーなしでアドレスのみを返しますが、DEVC++とCode::Blocksコンパイラの両方がsenddontsend 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();
  }
4

1 に答える 1

3

現在のコードに関するいくつかの問題 (他にもあるかもしれませんが、最初にこれらの問題の解決に集中する必要があります)


問題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{

^ Withfirstptr->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。宣言するだけtemp1で、temp2代わりに:

     Node*temp1;
     Node*temp2;

問題 3 :

    while(temp1->nextptr!=firstptr)

^ この while ループは、次の理由で決して実行されません:

  • あなたfirstptrはnullとして始めます
  • 次に、ノードを追加し、 にfirstptr->next戻りfirstptrます。
  • 次に、2 番目のノードを追加しようとすると、割り当ての作業が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;
       }

^ ここでは、循環的にリンクされたリストを実際に反復処理する方法が必要です。現在は、最初のノードを繰り返し出力しているだけです (技術的には、コードの他の部分では、2 番目のノードをリンクされたリストに追加することはまだ許可されていません。 )

何かのようなもの:

       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;
          }
       }
于 2012-12-13T16:30:36.393 に答える