2016-11-17 6 views
-4

新しいノードをリストに追加すると、ヘッドノードに問題があります。ファイルから複数の行を読み込む必要があります。各行は関数f(x)= ...になり、ノードは関数内の特異表現であるため、例えばノード1は25x^2であり、ノード2は15xとなる可能性があります。だから、私のNodeクラスは係数を保持しているので、ノード1では25、指数xは0になります。私が思うような問題を引き起こしているコードは次のとおりです。ここクラスとC++のリンクリスト

Node* n = new Node(); 
List nodeList; 
nodeList.setHead(NULL); 

while(not at the end of line) 
{ 
//This while loop just inputs from file, and stores values into Node class. 
if(!nodeList.getHead()) //so this is first node being input. 
{ 
    //i collect the values for coefficient and exponent here... 
    n->setCoef(coef); 
    n->setExp(exp); 
    nodeList.insertNode(n); 
} 
else //so this is not the first node in the list. 
{ 
    //i collect the values for coefficient and exponent again here... 
    //After this code below, the head node changes to n's coef and exp. 
    //I know this is because n is still pointing at the head node 
    //but I keep getting runtime errors when trying to fix this. 
    n->setCoef(coef); 
    n->setExp(exp); 
    nodeList.insertNode(n); 
} 

}

私のリストである:: insertNode(ノード* n)がクラス:行ごとにノードを挿入する

void List::insertNode(Node* n){ 
//if theres no head node, just set it to the n node and continue. 
if (!head) 
    head = n; 
else{ 
    Node* ptr = head; //used to traverse through list. 
    bool likeTerms = false; 
    while(ptr) //This while loop checks to make sure theres no like terms. 
    { 
     if (ptr->getExp() == n->getExp()){ 
      likeTerms = true; 
      break; 
     } 
     ptr = ptr->getNext(); 
    } 
    //If they aren't like terms, just add the node to the end. 
    if (!likeTerms){ 
     ptr = head; 
     while(ptr->getNext() != NULL) 
     { 
      ptr = ptr->getNext(); //traverses to the last node in list. 
     } 
     ptr->setNext(n); //Adds the new node to the spot after the last node 
    } 
    else if (likeTerms == true)//If the x exponents have like terms, 
           //then just combine them. 
     ptr->setCoef(ptr->getCoef()+n->getCoef()); 
} 

}

答えて

-1

コードも簡素化することができますif-else条件の2つのステートメントは同等であるため、以下のようになります。変数Node* nは、while-loopに作成する必要があります。nodeListには、関数f(x)の最後の項目を含むノードが1つしか含まれません。

while (not at the end of line) 
{ 
    Node* n = new Node; 
    n->setCoef(coef); 
    n->setExp(exp); 
    nodeList.insertNode(n); 
} 

void List::insertNode(Node* n)も簡素化できます。以下は簡略版です。

void List::insertNode(Node* n) { 
    Node* ptr = header; 
    Node* prev = NULL; 
    bool same_exp_occurred = false; 
    while (ptr) { 
     if (n->getExp() == ptr->getExp()) { 
      ptr->setCoef(ptr->getCoef()+n->getCoef()); 
      same_exp_occurred = true; 
      break; 
     } 
     prev = ptr; 
     ptr = ptr->getNext(); 
    } 

    if (!same_exp_occurred && prev) { 
     prev->setNext(n); 
    } 
} 
+0

コードを改善していただきありがとうございます。私は間違いなくそれを見ていきます。私は問題を発見した。私は問題をうまく言いませんでしたが、基本的には1つのノードしか作成せず、同じノードを毎回異なる値でリストに追加しようとしていました。 Node * nを使用する代わりに、nodeList.insertNode(new Node(coef、exp、NULL)))という行をnodeList.insertNode(n);に置き換えました。 – CMW

+0

ようこそ。私はコードの単純化に重点を置いており、単に説明やコメントなしでwhileループに 'Node * n = new Node;'というコードを置くだけです。その結果、私はあなたの質問に直接答えません。 – Kai

関連する問題