2016-07-13 10 views
0

Ich arbeite derzeit mit einer Linklisten-Datenstruktur. Die Liste weist einen Zeiger auf einen Kopfknoten und einen Zeiger auf einen Endknoten auf.Segmentierungsfehler bei Instanziierung

Die Knotenklasse ist als follode

template<class Type> class Node 
{ 
private: 
    Type* storedVertex; 
    Node<Type>* next; 

public: 
    Node() 
    { 
     storedVertex = NULL; 
     next = NULL; 
    } 

    Node(Type &vertex): next(NULL) 
    { 
     storedVertex = &vertex; 
    } 

    Type* get_Vertex() 
    { 
     return storedVertex; 
    } 

    Node<Type>* get_Next() 
    { 
     return this->next; 
    } 

    void set_Next(Node<Type>* _node) 
    { 
     this->next = _node; 
    } 

}; 

die Linklist-Klasse:

template<class Type> class LinkList 
{ 
private: 
    int sz; //number of entries in linked list. 
    Node<Type> *head = NULL; 
    Node<Type> *tail = NULL; 

public: 
    LinkList(): sz(0) 
    { 
     //O(1) 
    } 

    ~LinkList() 
    {} 

    void del_All() 
    { 
     while (head != NULL) 
     { 
      Node<Type>* tmp = head; 
      head = head->next; 
      delete tmp; 
     } 
     sz = 0; 
     tail = NULL; 
    } 
// /* 
    void push_Front(Type _vertex) 
    { 
     Node<Type>* _node = new Node<Type>(_vertex); 
     if(head == NULL) 
     { 
      head = _node; 
      tail = _node; 
      sz++; 
     } 
     else 
     { 
      _node->next = head; 
      head = _node; 
      sz++; 
     } 
    } 

    void add_node(Type _vertex) 
    { 
     Node<Type>* _node = new Node<Type>(_vertex); 
     //Adds node to the back of the linkList. 
     if (head == NULL) 
     { 
      //cout << "ptr" << ptr->getNext() << endl; 
      head = _node; 
      tail = _node; 
      sz++; 
     } 
     else 
     { 
      Node<Type>* ptr = head; 
      while(ptr->get_Next() != NULL) 
      { 
       ptr = ptr->get_Next(); 
      } 
      ptr->set_Next(_node); 
      this->tail = _node; 
      sz++; 
     } 
     //LinkList::printList(); 
    } 
bool is_Empty() 
    { 
     if(head==0) cout << "Empty" << endl; 
     return head==0; 
    } 

    Node<Type>* get_Head() 
    { 
     cout << "DEBUG:: IN GET HEAD" << endl; 
     if(head == NULL) return NULL; 
     return head; 
    } 

    void set_Head(Node<Type> _Node) 
    { 
     push_Front(_Node); 
    } 

    Type get_Tail() 
    { 
     if(tail == NULL) return NULL; 
     return tail; 
    } 

//Accessors 
    int size() 
    { 
     return sz; 
    } 

    bool search_for(string data) 
    { 
     cout << "DEBUG 3" << endl; 
     if(this->get_Head() == NULL) 
     { 
      cout << "DEBUG 3.5" << endl; 
      return false; 
     } 
     else 
     { 
      cout << "DEBUG 4" << endl; 
      Node<Type>* ptr = head; 
      if(ptr = NULL) return false; 
      cout << "DEBUG AGAIN" << endl; 

      cout << "search_for() while loop was "; 
      while(ptr != NULL) 
      { 
       if(ptr->get_Vertex()->get_Data() == data) 
       { 
        cout << "not skipped" << endl; 
        return true; 
       } 
       ptr = ptr->get_Next(); 
       cout << "not "; 
      } 
      cout << "skipped."; 

      return false; 
     } 
    } 
}; 

Main:

#include <iostream> 
#include <string> 
#include "Vertex.h" 
#include "HashTable.h" 

using namespace std; 
int main() 
{ 
      LinkList<string> list; 
      cout << "*****Insert*****" << endl; 
      cout << "Data(string) to insert: "; 
      string data; 
      cin >> data; 

      Vertex<string>* vertex = new Vertex<string>(data); 

      bool insert; 
      insert = list.add_node(vertex); 
      return 0; 
} 

Die Dateien auf meinem Ende kompilieren. Wenn ich die Funktion add_Node ausführe, wird sie ausgeführt, bis sie auf Anweisungen wie (head == NULL) stößt. Hier kommt der Seg-Fehler. Ich verstehe nicht, ist dies, weil ich Kopf auf NULL initiiert?

+0

Können Sie auch veröffentlichen Sie Ihre 'main()' zu zeigen, wie Sie diese Klasse verwenden? – cplusplusrat

+0

Das 'main' kann nicht stimmen, Sie haben Ihre Klassen' Node' oder 'LinkList' nicht irgendwo benutzt. – cplusplusrat

+0

Wenn Ihr Compiler diese 'Node * head = NULL;' - Syntax unterstützt, dann unterstützt er 'nullptr' - verwenden Sie es anstelle von' NULL'. – kfsone

Antwort

0

Der einzige Fehler, den ich sehen kann, ist:

if(ptr = NULL) return false; 
+0

:) klassisch ... Ich habe das vermisst. – cplusplusrat

+0

@cplusplusrat Danke! Ich kann die Frage von OP nicht kommentieren. Wie cplusplusrat sagte, bitte posten so viele Informationen wie möglich. – J11