2013-07-17 28 views
1

我想實現我的顯示功能(在我的main.cc文件中)。但是,當我用我的curr指針獲取我的學生對象的數據然後遍歷該列表時,程序核心轉儲。單鏈表的顯示函數中的Seg錯誤

MAIN.CC

#include "Node.h" 
#include "Student.h" 

using namespace std; 

void append (Node **, Node *); //append Node to end of list. 
void display(Node *); //print linked list. 
void input(Student *); //enter new Student object. 
void deleteNode(Node **, string); //delete specific Node. 

string first; 
string mid; 
string last; 
string ssn; 
string age; 

int main() { 

    string flag; //flag checks for delimiter. 
    string name; 
    string stuDelete; //name to be deleted. 

    Student * stuPtr = NULL; 
    Node * head = NULL; 
    Node * newPtr = NULL; 

    do { 
     stuPtr = new Student; 
     input(stuPtr); 
     flag = stuPtr ->getFirst(); //flag = first name 

     if(stuPtr -> getFirst() == "-") { //"-" is delimiter 
      cout << "in if statement\n"; 
      delete stuPtr; //no new Student, no more need for 
          //temporary Student pointer. 
      stuPtr = NULL; 
      cout << "checkpoint1" << endl; 
     } 
     else { 
      newPtr = new Node(stuPtr); 
      append(& head, newPtr); 
      cout << "checkpoint 2" << endl; 
     } 
    } while (first != "-"); //will prompt for more entries unless 
          //delimiter is detected. 

    cout << "checkpoint 3" << endl; 
    display(head); // MY DISPLAY FUNCTION SUCKS 

    if(head) 
     cout << "Enter a Student to be deleted" << endl; 
    while(head) { 
     cout << "Last name: "; 
     cin >> stuDelete; 
     deleteNode(& head, stuDelete); 

     string reqDelete; // asks if you want to keep deleting. 
     cout << "Student has been deleted. Delete another? (Y/N)" << endl; 
     cin >> reqDelete; 

     if(head != NULL && reqDelete == "Y") { 
      display(head); //iterates thru linked list 
      cout << "\nEnter another name to be deleted: \n" << endl; 
     } 
     else if(reqDelete != "Y") 
      cout << "Deletion complete.\n" << endl; 
    } 

    if(head) 
     display(head); 
    else 
    cout << "The list is now empty.\n" 
     << "=============================" << endl; 

     return 0; 

} 

    void display (Node * newPtr) { 

    Node * curr = newPtr; 

    while(curr != NULL) { 
    // getData is used to point to the stud info in a node 
    cout << "{" 
     << curr->getData()->getFirst() << ", " 
     << curr->getData()->getMiddle() << ", " 
     << curr->getData()->getLast()<< ", " 
     << curr->getData()->getSocial()<< ", " 
     << curr->getData()->getAge() 
     << "}" << endl;  
    curr = curr->getNext(); // move to the next obj, traverse stud data 
    } 
    cout << "-----------------------------------------" << endl; 
} 

我會列出我的頭對我的節點和Student類。讓我知道,如果這是不夠的信息。

NODE.H

class Node { 

public: 
    Node(); //Default constructor. 
    Node(Student *); //New constructor. 

    Student * getData(); //get data on Student object. 
    void setData(Student *); //set data for Student object. 

    Node * getNext(); //get the next Node in the linked list. 
    void setNext(Node *); //set the next Node in the list. 

private: 
    Student * data; //pointer to current Student data. 
    Node * next; //pointer within Node to the next Node. 

}; 

#endif 

STUDENT.H

class Student { 

public: 
    Student(); //Default constructor; 
    Student(const string &, const string &, const string &, 
     const string &, const string &); //New constructor. 

    //setters 
    void setName(const string &, const string &, const string &); 
    void setSocial(const string &); 
    void setAge(const string &); 

    //getters 
    string getFirst(); 
    string getMiddle(); 
    string getLast(); 
    string getSocial(); 
    string getAge(); 

private: 
    string stuData[5]; //array of fields for Student data. 
}; 

終端輸出

[email protected]:~/DataStructures/Lab4$ ./a.out 

Enter Student information (Enter '-' to exit) 
First Name: jesus 
Middle Name: h 
Last Name: christ 
Social: 222222222 
Age: 222 
============================== 
checkpoint 2 

Enter Student information (Enter '-' to exit) 
First Name: - 
Student entry finished. 

============================== 
in if statement 
checkpoint1 
checkpoint 3 
Segmentation fault (core dumped) 

構造函數Student類

Student::Student() { 
    stuData[0] = "Firstname"; 
    stuData[1] = "Middlename"; 
    stuData[2] = "Lastname"; 
    stuData[3] = "SSN"; 
    stuData[4] = "##"; 
} 

Student::Student(const string & first, const string & mid, 
    const string & last, const string & ssn, const string & age) { 

    setName(first, mid, last); 
    setSocial(ssn); 
    setAge(age); 
} 

構造函數節點類

Node::Node() { 
    next = NULL; //new Nodes go to the end of the list. 
} 

Node::Node(Student * tempData) { 
    data = tempData; 
    delete tempData; 
    next = NULL; 
} 
+0

請發佈構造函數的節點。也許你沒有初始化Next的情況; – madnut

+0

while(first!=「 - 」);我相信首先沒有初始化。 – PunDefeated

+0

@madnut添加了這兩個類的構造函數。 –

回答

1
  1. 在要刪除的數據,學生的節點構造,使您的節點::數據點無效的內存,因此它是最有可能不顯示功能,吸,而是刪除節點構造函數中的數據。
  2. 請考慮
    2.1格式:'。'之間沒有空格。或' - >'(取消引用操作符),因爲這會使代碼非常難以閱讀
    2.2使用C++ 11結構,如nullptr而不是NULL
    2.3將括號括在ALL中if/for/while/... as in maintenance的代碼人們可能會添加東西,並懷疑它不能按預期工作。
+0

去除兩個構造「刪除TempData的」線固定在賽格故障。非常感謝你! –

+0

我將進行格式更正。不知道如何完全使用nullptr。我會研究這一點。 –

+0

'nullptr'將只提供給你,如果你可以使用C++編譯器11。這是新標準引入的關鍵字。 – ogni42