2016-04-08 5 views
0

この関数は、リンクリストのコピー構成です。コードは、最初のwhileループのどこかで中断します。ただ単に新しいリンクされたリストを作成しています。何が原因で起こっているのかわかりません。また、他に何かが必要な場合はお知らせください。ありがとう。リンクリストのコピーコンストラクタ関数のセグメンテーション違反が引き続き発生するのはなぜですか?

DLL::DLL(DLL& n) { 
    if (n.headPtr == NULL) { 
     headPtr = NULL; 
    } 
    else { 
     //keeps track of the last node in the list               
     Node* lastNode = NULL; 
     //keeps track of old list                   
     Node* oldListNode = n.headPtr; 

     while (oldListNode != NULL) { 
      //create a new Node for new list until reaches end of old list        
      Node* newNode = new Node; 
      newNode->pred = NULL; 
      newNode->succ = NULL; 
      newNode->ssn = oldListNode->ssn; 
      newNode->name = oldListNode->name; 
      //add newNode to new List                  
      if (headPtr == NULL) { //new list is empty              
       headPtr = newNode; 
       lastNode = headPtr; 
      } 
      else { 
       //adds new node to the end of the list              
       lastNode->succ = newNode; 
       newNode->pred = lastNode; 
       lastNode = newNode; 
      } 
      //Goes to the next node in the old list;              
      oldListNode = oldListNode->succ; 
     } 
    } 
    //now puts all information from old list into new list            
    itemCount = n.itemCount; 
    if (n.headPtr == NULL) { 
     headPtr = NULL; 
    } 
    else { 
     if (n.itemCount > 0) { 
      cout << "in while loop, "; 
      Node *origList = n.headPtr; //pointer accessing old node's data      
      Node *secondHeadPtr = new Node; //second hptr for new ll       
      Node *currNode = secondHeadPtr; // points to second ll node that will be filled  
      Node *nextNode; //going to point to added node          
      while (currNode != NULL) { 
       cout << "in while loop, "; 
       nextNode = new Node; //next node, currnode to point to       
       currNode->ssn = origList->ssn; 
       currNode->name = origList->name; 
       currNode->succ = nextNode; //create beginning of second ll, next node   
       nextNode->pred = currNode; 
       currNode = currNode->succ; 
       origList = origList->succ; 
       cout << currNode->name << " " << currNode->ssn << " "; 
      } 
     } 
    } 
} 

ヘッダファイル

#include <string> 
using namespace std; 

struct Node { 
    string ssn; 
    string name; 
    Node* succ; 
    Node* pred; 
}; 

class DLL { 
private: 
    Node* headPtr; 
    int itemCount; 

public: 
    DLL(); 
    DLL(DLL& n); 
    virtual ~DLL(); 
    Node* getHeadPtr(); 
    int search(string ss) const; 
    bool insert(string ss, string name, int & count); 
    bool remove(string ss, int & count); 
    int size(); 
    void display(); 
}; 

テストファイル

#include "DLL.h" 
#include <iostream> 
#include <string> 

using namespace std; 

int main() { 

    DLL myList; 
    int counter = 0; 
    int dCounter = 0; 

    myList.insert("30", "Jack Eblin", counter); 
    myList.insert("40", "Liz Smith", counter); 
    myList.insert("10", "Mike Dutter", counter); 
    myList.insert("20", "Kitty Lekberg", counter); 
    myList.insert("50", "Carma Meshew", counter); 

    cout << "After insertion, we should have 10 20 30 40 50 in order" << endl; 
    myList.display(); 

    cout << "Searching 30 in the list, result should be 2" << endl; 
    cout << myList.search("30") << endl; 

    myList.remove("10", dCounter); 
    myList.remove("50", dCounter); 

    cout << "After deletion, we should have 20 30 40 in order" << endl; 
    myList.display(); 


    cout << "Testing copy constructor" << endl; 
    DLL* temp = new DLL(myList); 

    cout << "Contents of the original list" << endl; 
    myList.display(); 

    cout << "Contents of the new list, the memory address of the this list must be different from the\ 
    original list" << endl; 

    if (myList.getHeadPtr() != nullptr) { 
     temp->display(); 
    } 
    return 0; 
} 
+0

あなたのコピーコンストラクタは本当に 'const DLL&'引数を取るべきです。 –

+4

それをデバッグしてみてください。 「どこか」はあまり役に立ちません。 –

+5

インデントが一貫しておらず、コードの品質が悪い場合はコンパニオンです。あなたはコードにもっと勤勉であることを学ぶ必要があり、フォーマットは自動的に改善されます。 – IInspectable

答えて

3

あなたはコピーコンストラクタで正しくheadPtrを設定したことがないように見えます。コピーコンストラクタに入っているので、クラスメンバは初期化するまで初期化されません。つまり、headPtrは何でもでき、何かをすることができますが、その値を設定することは未定義の動作です。あなたは

if(headPtr == NULL) 

headPtrされたら、まだ我々が起こるために何が起こっているか見当がつかないのでintializedされていません。おそらくそれはNULLではない値のいくつかの並べ替えを持っていますし、あなたのコピーのheadPtrは決して設定されません。

+0

これは意味がありますが、segフォルトが発生する場所です。 – Tom

+0

@Tom問題ありません。 – NathanOliver

+0

@Tomそれが理にかなって問題を解決するなら、答えを受け入れるべきです;-) –