2016-07-13 6 views
0

現在、リンクリストのデータ構造を使用しています。リストには、ヘッドノードへのポインタとテールノードへのポインタがあります。インスタンス化時のセグメンテーションフォルト

ノードクラスは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; 
    } 

}; 

ようですLINKLISTクラス:

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; 
     } 
    } 
}; 

メイン:

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

ファイルが私の最後にコンパイルされています。 add_Node関数を実行すると、(head == NULL)のような文が出現するまで実行されます。これは、どこでsegフォールトを取得しています。私はNULLに頭を開始したので、これは理解できないのですか?

+0

あなたも、あなたがこのクラスを使用しているかを示すために ''あなたのmain()を投稿することができますか? – cplusplusrat

+0

'main'は正しいとは言えませんが、クラス' Node'または 'LinkList'をクラス内で使用していません。 – cplusplusrat

+0

コンパイラがこの 'Node * head = NULL;'構文をサポートしている場合、 'nullptr'をサポートしています - ' NULL'の代わりに使用してください。 – kfsone

答えて

0

私が見ることができる唯一の間違いは次のとおりです。

if(ptr = NULL) return false; 
+0

:) classic ...私はそれを逃した。 – cplusplusrat

+0

@cplusplusratありがとう! OPの質問にはコメントできません。 cplusplusratが言ったように、できるだけ多くの情報を投稿してください。 – J11

関連する問題