2017-03-30 128 views
0

我發佈了這個帖子到Reddit,但沒有得到任何評論,所以我決定看看我能否在這裏得到一些幫助。雙鏈表列表問題(具體複製構造函數和刪除函數)

我目前的項目中給出了一個雙向鏈表.h文件和一個.cpp文件,我們需要在.cpp中實現.h。儘管我很擔心鏈接列表,但我覺得即使我有權利或有點權利的事情都是sl and不馴的,並且不能滿足所有的要求。這裏是.h文件:

#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(); 
}; 

,這裏是.cpp文件:

#include "DLL.h" 
#include <iostream> 
#include <string> 
using namespace std; 

// default constructor is already implemented 
// do not modify the default constructor 
DLL::DLL(){ 
    headPtr = nullptr; 
    itemCount=0; 
} 

// return the head pointer of the list 
// it is already implemented, do not modify it 
Node* DLL::getHeadPtr(){ 
    return headPtr; 
} 

// copy construct, which copies an existing list n 
// the new list is a different object from n 
// the new list and object n have the same contents 
// Please implement it 
DLL::DLL(DLL& n){ 
     /*Node *first = n.getHeadPtr(); 
    first = headPtr; 
    while(headPtr->succ) 
     headPtr = headPtr->succ; 
    while(headPtr){ 
     first->succ = headPtr->succ; 
     headPtr->succ = headPtr->succ->succ; 
    }*/ 
} 

// destructor 
// release every node of the list 
// Please implement it 
DLL::~DLL(){ 
    Node *tmp = this->headPtr; 
    Node *temp; 
    while(tmp->pred) 
     tmp = tmp->pred; 
    while(tmp) 
    { 
     temp = tmp->succ; 
     delete tmp; 
     tmp = temp; 
    } 
    tmp =NULL; 
    temp = NULL; 
} 

// if some node has SSN matcthes string ss 
// return the index value of the node 
// the index value of the first node is 0, the second node is 1, etc. 
// if there is node matching ss, return -1 
int DLL::search(string ss)const{ 
    int count = 0; 
    Node *tmp = headPtr; 
    while(tmp != NULL){ 
     count++; 
    if(tmp->ssn == ss) 
     return count -1; 
    tmp = tmp->succ; 
    } 
    return NULL; 
} 

// insert (ss, name) to the existing list 
// the SSN values are each node are organized in INCREASING order 
// if there is a node matching ss value, return false; otherwise true 
// else create a node with (ss, name), insert the node to the proper position 
// parameter count is the counter of number of valid insertion 
// when you implement this method, consider the following situations: 
// 1. list is empty 
// 2. node should be inserted to the beginning of the list 
// 3. node should be inserted to the middle of the list 
// 4. node should be inserted to the end of the list 
//I use the append function, then sort afterwards 
bool DLL::insert(string ss, string name, int & count){ 
    //Create Node 
    Node *newPtr = new Node; 
    newPtr->ssn = ss; 
    newPtr->succ = NULL; 
    //If list is empty 
    if(headPtr == NULL){ 
     headPtr = newPtr; 
     return true; 
    } 
    //If list is not empty 
    else{ 
     Node* temp = headPtr; 
     while(temp->succ != NULL){ 
      temp = temp->succ; 
      itemCount++; 
      count++; 
     } 
    temp->succ = newPtr; 
    //Following part is to sort from least to greatest (based on Lab 7) 
    //Store head to temp node 
    temp = headPtr; 
    string tempVal; 
    int counter = 0; 
    //Set temp to next, increase count 
    while (temp){ 
     temp = temp->succ; 
     counter++; 
    } 
    //Make temp head again 
    temp = headPtr; 
    //While less than count and has next node, check if val at temp is greater than val at next 
    for (int j=0; j<count; j++){ 
     while (temp->succ){ 
      if (temp->ssn > temp->succ->ssn){ 
       //Store val at temp to temp val 
       tempVal = temp->ssn ; 
       //Make temp's val the value at temp->next 
       temp->ssn = temp->succ->ssn; 
       //Make temp->next's val, temp's old val 
       temp->succ->ssn = tempVal; 
      } 
      //Make temp the next value and check again 
      temp = temp->succ; 
     } 
     //Move temp back to head  
     temp = headPtr; 
    } 
    return true; 
    } 
    return false; 
} 


// remove node containing ss value 
// if there is no node containing ss, return false; otherwise true 
// consider the following situations: 
// 1. list is empty 
// 2. node containing ss value is the first node 
// 3. node containing ss value is in the middle of the list 
// 4. node containing ss value is the last node of the list 
bool DLL::remove(string ss, int & count){ 
    /*Node *temp = headPtr; 
    while(temp){ 
     if(temp->ssn == ss){ 
      temp->pred->succ = temp->succ; 
      temp->succ->pred = temp->pred; 
      count++; 
      return true; 
     } 
     temp = temp->succ; 

    } 
    return false;*/ 
} 

// return the number of the nodes 
// it is already implemented, do not modify it 
int DLL::size(){ 
    return itemCount; 
} 

// iterate through each node 
// print out SSN and memory address of each node 
// do not modify this method 
void DLL::display(){ 
    Node* temp; 
    temp = headPtr; 
    while (temp!= nullptr) { 
     cout << temp->ssn << "\t" << temp << endl; 
     temp = temp->succ; 
    } 
} 

我想我的搜索功能是好的,有評論說,說不要修改功能是由由教授,所以他們很好,和.h文件是好的,因爲它也包括在內。儘管如此,我真的很努力地使用複製構造函數和刪除函數。我的刪除函數的當前內容被註釋掉了,因爲每次達到它時test.exe都會崩潰。然後複製構造函數我只是失去了,我做了大量的研究和閱讀其他帖子,但我無法弄清楚。我有我的意見,因爲每次它到達該功能程序崩潰,就像使用刪除功能。我的插入函數應該按順序插入所有內容,如果插入函數已插入,我會將每個項目追加到列表中,然後將它們按順序排序,從不檢查SSN是否已經在。它有效,但顯然不完全。任何建議都將是非常有用的,因爲你可以告訴我雙向鏈接列表掙扎了很多。先謝謝你。

+0

在旁註:我猜對返回值的評論是來自教授,你應該**嚴格遵循這些(例如:你的搜索函數應該返回'-1'時,找不到值,你的返回NULL) – ccKep

+0

@ccKep它實際上返回-1當沒有找到,我會說實話我不知道爲什麼,我知道它不應該,但由於某種原因它確實。 – paul5345

+0

在這種情況下,您應該完全調試程序流(並檢查實際上是您編譯的代碼),該函數無法產生小於0的值。 – ccKep

回答

0

似乎在構造

/*Node *first = n.getHeadPtr(); 
first = headPtr; 
while(headPtr->succ) 

這裏headPtr與空指針的一個問題是一個nullptr,因此headPtr->succ引發運行時錯誤。

+0

實際上,在複製構造函數中,「headPtr」不是'nullptr' - 它是不確定的,因爲它沒有被初始化。 –

+0

@MichaelBurr我有點困惑,不會在調用該函數時,使用類似於DLL時的headPtr * newList = new DLL(oldList);對不起,我真的在鏈接列表中掙扎 – paul5345

+0

這是複製構造函數初始化新副本的工作。如果你沒有實現copy ctor,編譯器會創建一個非常基本的編譯器,但是在鏈接列表的情況下,基本的編譯器會出錯 - 這就是爲什麼你(程序員)需要編寫拷貝構造函數。複製構造函數是編譯器將調用像「new DLL(oldList)」這樣的表達式的東西。所以,如果你有一個自定義的(這是'DLL'類 - 谷歌'三'規則'所需的),它必須做所有的工作。 –