0

割り当てのために、あるオブジェクトのリンクリストを別のオブジェクトの後ろに追加する必要があります。私はこれを持っています:

WORD you("you");//where you's linked list now contains y o u
WORD are("are");//where are's linked list now contains a r e

そして私はこれをしたい:

you.Insert(are,543);//(anything greater they the current objects length is
                    //attached to the back. So that 543 can be anything > you's length

したがって、リンクされたリンクリストには次のものが含まれているはずです。

y o u a r e

前面と文字の間のどこにでも挿入できましたが、背面に挿入しようとすると、プログラムがすぐにクラッシュします。誰かが私が何が悪いのかを理解するのを手伝ってくれますか?デバッガーを使ってみたところ、1行を指していますが、何が悪いのかわかりません。私はその行を関数の到来としてマークしました:

void WORD::Insert(WORD & w, int pos)
{
if(!w.IsEmpty())
{
    alpha_numeric *r = w.Cpy();
    alpha_numeric *loc;

    (*this).Traverse(loc,pos);//pasing Traverse a pointer to a node and the     position in the list

    //if(loc == 0)
    //{
    //  alpha_numeric *k = r;//k is pointing to the begin. of the copied words list
    //  while(k -> next != 0)
    //  {
    //      k = k -> next;//k goes to the back 
    //  }
    //  k -> next = front;//k(the back) is now the front of *this
    //  front = r;//front now points to r, the copy
    //}
    else if(loc == back)
    {

        back -> next = r; //<<<-------DEBUGGER SAYS ERROR HERE?
        length += w.Length();
        while(back -> next!= 0)
        {
            back = back -> next;
        }
    }
    /*else
    {
        alpha_numeric *l = r;

        while(l -> next != 0)
        {
            l = l -> next;
        }
        l -> next = loc -> next;
        loc -> next = r;
    }
    length += w.Length();
}*/
}

また、これが役立つ場合に使用したトラバース関数です

void WORD::Traverse(alpha_numeric * & p, const int & pos)
{
if(pos <= 1)
{
    p = 0;
}
else if( pos > (*this).Length())
{
    p = back;
}
else
{
    p = front;
    for(int i = 1; i < pos - 1; i++)
    {
        p = p -> next;
    }
}

}

私はクラスのプライベートセクションでポインターとして宣言しました。*戻る

これが私がコンストラクターに入れる方法です

WORD::WORD()
{
alpha_numeric *p;

front = new alpha_numeric;
front = 0;
length = 0;
back = front;

for(p = front; p != 0; p = p -> next)
{
    back = back -> next;
}
}
4

2 に答える 2

0

リストが空のときにブロックが更新backされないことが問題の原因であると強く思います。if(loc==0)

この場合、backは残さ== 0れ、追加操作に失敗します。

于 2012-06-08T03:44:10.537 に答える
0

* backは、トラバース関数の正しいノードを指していません。次のようになります。

else if( pos > (*this).Length())
{
    alpha_numeric *k = (*this).front;
    while( k -> next != 0)
    {
        k = k -> next;
    }
    back = k;
    p = back;
}
于 2012-06-08T04:26:47.870 に答える