2016-10-23 5 views
-1

二重リンクされたキューを作成する関数を作成しています。リストにたどりつき、各ノードのデータがゼロになるか条件が満たされるまで増分する必要があります。何らかの理由で、最後のノードのamount = 0でqueueTotal関数を実行し、queueTotalのelse関数に行くと、segfaultが返されます。私はtail-> prevにアクセスしようとしているので、それは決して宣言されていないと思う。どうすればprevノードにアクセスできますか?tail-> prevにアクセスする際にエラーが発生しました

のisEmpty():

bool List::isEmpty() { 
    return head == 0; 
} 

クラスコンストラクタ:

List::List() { 
    head=0; 
    tail=0; 
} 

オーバーロード=

List::Node* List::operator= (Node* input) { 
    Node* cell = new Node; 
    cell->amount = input->amount; 
    cell->price = input->price; 
    cell->next = input->next; 
    cell->prev = input->prev; 
    return cell; 
} 

プッシュ(エンキュー)関数:

void List::push(int amount, double price) { 
    Node* cell = new Node; 
    cell->amount = amount; 
    cell->price = price; 
    if(isEmpty()) { 
      tail = cell; 
    } 


    else { 
      head->prev = cell; 
    } 

    cell->next = head; 
    head = cell; 

queueTotal(デキュー)関数:プッシュ機能IF(のisEmpty()){尾=細胞において

double List::queueTotal(int total, double price) { 
    Node * cell = new Node; 
      this->ListPrint(); 
    if (isEmpty()) std::cout << "Attempting to dequeue empty queue" << std::endl; 
    cell = tail; 
    double basis = 0.0; 
    double gain = total * price; 
    for (int i = 0; total; i++) { 

      if(cell->amount > 0) { 
        cell->amount -= 1; 
        total -= 1; 
      } 
      else { 
        basis += (i * cell->price); 
        i = 0; 
        if(head->next == 0) { 
          head = 0; 
        } 

        else{ 
          tail->prev->next = 0; 
        } 

        tail = tail->prev; 

      } 

    } 
    double subtotal = gain - basis; 
    totalGain += subtotal; 
    return subtotal; 

} 
+2

このような問題を解決する適切なツールは、デバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低でも、あなたはあなたが行った観察と一緒に、[編集]あなたの質問あなたの問題を再現[、最小完全、かつ検証](http://stackoverflow.com/help/mcve)の例を含むようにする必要があります\しますデバッガ。 –

+0

push(エンキュー)関数: 'if(isEmpty()){ tail = cell; } '。新しいデータをリストにプッシュすると、 'head'は常に' null'と 'isEmpty()'関数は常にtrueを返します。 –

答えて

0

。ヘッド=セル; }頭を初期化する必要があるから

関連する問題