2017-02-14 13 views
0
void CommunicationNetwork:: buildNetwork(){ 

    string citylist [] = {"Phoenix", "Denver", "Dallas", "St. Louis", "Chicago", "Atlanta", "Washington D.C.", "New York", "Boston"}; 

    head = ("Los Angeles", NULL, NULL); 
    City *temp2 = new City; 

    City *temp = new City; 

    temp=head; 

    for(int i=0;i<9;i++){ 

     temp2->cityName=citylist[i]; //=Pheonix 
     temp->next=temp2;   //segmentation fault here 
     temp=temp2; 
    } 
} 

Cityは、リンクリスト用の構造体です。 nextは次のノードを指します。 tempのNULL nextの値を逆参照しようとしているため、セグメント化エラーが発生します。それは私がtempに値を加えると考えることができる唯一の方法です。 temp->next= temp2以外にNULLをtemp2と置き換えることができる別の方法はありますか?" - >"を使用せずにリンクリストに値を追加するにはどうすればいいですか?

+1

あなたが「TEMP2を移動する必要があります=ループの内側に新しい市区町村を置き、temp1を指します。 – Mike

+0

ヘッドが "temp = head"という行でtempに指されていませんか? – user7554736

+0

'a-> b'は'(* a).b'のショートカットですので、 ' - >'を使わずにコードを機械的に書き換えることができます。 – dasblinkenlight

答えて

0

これは、リンクリストを実装する正しい方法ではありません。その後、

private: 
    City *head, *tail; 

CommunicationNetwork::CommunicationNetwork() 
    : head(NULL), tail(NULL) 
{ 
} 

CommunicationNetwork::~CommunicationNetwork() 
{ 
    City *temp = head; 
    while (temp) 
    { 
     City *next = temp->next; 
     delete temp; 
     temp = next; 
    } 
} 

void CommunicationNetwork::addCity(const std::string &cityName) 
{ 
    City *temp = new City; 
    temp->cityName = citylist[i]; 
    temp->next = NULL; 

    if (!head) head = temp; 
    if (tail) tail->next = temp; 
    tail = temp; 
} 

void CommunicationNetwork::buildNetwork() 
{ 
    std::string citylist [] = {"Los Angeles", "Phoenix", "Denver", "Dallas", "St. Louis", "Chicago", "Atlanta", "Washington D.C.", "New York", "Boston"}; 

    for(int i = 0; i < 10; ++i) 
     addCity(citylist[i]); 
} 

そして:あなたはそれのためにすべての時間を狩りする必要はありませんので、その後、ヘッドノードと一緒にテール・ノードを思い出し恩恵を受ける

private: 
    City *head; 

CommunicationNetwork::CommunicationNetwork() 
    : head(NULL) 
{ 
} 

CommunicationNetwork::~CommunicationNetwork() 
{ 
    City *temp = head; 
    while (temp) 
    { 
     City *next = temp->next; 
     delete temp; 
     temp = next; 
    } 
} 

void CommunicationNetwork::buildNetwork() 
{ 
    std::string citylist [] = {"Los Angeles", "Phoenix", "Denver", "Dallas", "St. Louis", "Chicago", "Atlanta", "Washington D.C.", "New York", "Boston"}; 

    // find the last node in the list... 
    City *last = NULL; 
    if (head) 
    { 
     last = head; 
     while (last->next) 
      last = last->next;   
    } 

    // add the cities to the end of the list... 
    for(int i = 0; i < 10; ++i) 
    { 
     City *temp = new City; 
     temp->cityName = citylist[i]; 
     temp->next = NULL; 

     if (!head) head = temp; 
     if (last) last->next = temp; 
     last = temp; 
    } 
} 

:それはより次のようになりますあなたはそれが働いていたら、手動でリストを捨てると、(C++ 11以降またはstd::forward_liststd::listを使用する代わりに、それはあなたのためのメモリを管理しましょう:

private: 
    std::list<std::string> cities; 

void CommunicationNetwork::addCity(const string &cityName) 
{ 
    cities.push_back(cityName); 
} 

void CommunicationNetwork::buildNetwork() 
{ 
    string citylist [] = {"Los Angeles", "Phoenix", "Denver", "Dallas", "St. Louis", "Chicago", "Atlanta", "Washington D.C.", "New York", "Boston"}; 

    for(int i = 0; i < 10; ++i) 
     addCity(citylist[i]); 
} 
+0

非常に包括的です。私はあなたに1つ借りている – user7554736

+0

面白い種類。これは完全に昨日働いた。私はコードを変更していませんが、今は動作しません。 – user7554736

関連する問題