2016-04-22 8 views
-1

ありがとうございます。二重リンクリストコアダンプ

私は二重リンクリストを作成しています。

すべてうまくいきましたが、新しいクラスノードをどこかに追加したときに、左のポインタがそれ以前のノード(今は2つのスペース)を指していることに気付きました。

だから、私は、ノードが新しいノードを指すようにと言わライン51に続いてライン上で46

を新しいノードポインタを追加しました。

ので:

  • まず、私は最後に私がノードを指すようにtemp3を伝えるその後、私はリストをポインタtemp2ループを作る

  • 空間に

  • を新しいノードの一時をオフに持っていましたtemp2のノードの後

機能の実行後

は、順序がtemp2->temp->temp3

私の主なポイントする必要があります:私は51行を追加した後、私のプログラムがコアダンプ(セグメンテーションフォールト)と出て終了します。

どうすればこの問題を解決できますか?私は頭のポインタの代わりに何かを追加するときにのみ発生します。

void add(node *&head, node *&tail, node *&current) 
{ 
    node *temp = new node; //creates a pointer pointing to a new class node 
    cin >> temp->letter; // user input 

    current = head; // creates a pointer to point at the first node 
    while (current != NULL) // while list isn't empty 
    { 
     if (current->letter == temp->letter) 
     { // letter already exists 
      cout << "DUPLICATE: " << temp->letter << endl << endl; 
      return; 
     } 
     else 
     { // loop through list moving tail pointer to the end while checking for duplicates 
      tail = current; 
      current = current->right_link; 
     } 
    } 

    current = temp; // current = new added node 

    if (isEmpty(head)) 
    { // if first node 
     temp->left_link = NULL; 
     temp->right_link = NULL; 
     head = temp; // head and 
     tail = temp; // tail both point to first and only node. 
    } 
    else 
    { // if new letter value is less than head value 
     if(temp->letter < head->letter) 
     { 
      temp->right_link = head; // node points (right) to head 
      temp->left_link = NULL; // left most node point to nothing. 
      head->left_link = temp; // head (currently the second node) points (left) to first node 
      head = temp; // head pointer moves to the first position 
     } 
     else 
     { // if new node goes anywhere other than head 
      node *temp2 = head; // new node to cycle through list 
      while(temp2->right_link != NULL && temp2->right_link->letter < temp->letter) 
      { // if temp2 points to a node and that node's value is less than temp node value 
       temp2 = temp2->right_link; 
      } 
      node *temp3 = temp2->right_link; 
      temp->right_link = temp2->right_link; // when temp2 stops looping, temp will point to 
                // the same node as temp2. 
      temp2->right_link = temp; // temp2's current node will point to temp, causing temp 
           // to be added into the list (after temp2) 
      temp3->left_link = temp; // point the node (after the newly inserted node) left to new node 
      temp->left_link = temp2; // connects the left pointer between temp and temp2 
      if(temp->right_link == NULL) 
       tail = temp; 
     } 
    } 
    cout << "ADDED : " << temp->letter << endl << endl; 
} 
+0

あなたは[mcve]を投稿できますか?また、デバッガを使ってみましたか? – xvan

答えて

0

temp2->right_link == NULL

46  node *temp3 = temp2->right_link; 

がNULLポインタであれば、そうすることはできませんあなたは、デバッガを使用した場合に明らかにされている必要があります

51  temp3->left_link = temp; 

+0

それは明らかに、明らかに助けてくれたはずです。私はこのデバッガの内容が誰でも話しているものを探します。 –