0

データ構造クラスに対する私の課題は、単語を作成するリンク リストのバージョンを実装することです。このクラスは WORD と呼ばれ、文字のリンクされたリストです。必要な操作の 1 つは、ある WORD を別の WORD から削除することです。例えば、

WORD t1("testing");
WORD t2("es");

t1.Remove(t2);

cout<<t1; //Outputs tting

WORD t1("testing");
WORD t2("t");
t1.RemoveAll(t2);
cout<<t1; //Outputs esing

これらの両方のコードは次のとおりです。

//Deletes an entire word from the list
//boolean passed to delete first occurrence of that word
//or all occurrences

void WORD::Delete(WORD & B, bool deleteAll)
{
    alpha_numeric *next, *last_word_start, *prev, *currB;

    bool found = false;
    next = last_word_start = prev = this->head;
    currB = B.head;

    while(next != NULL)
    {
        if(next->symbol == currB->symbol)
        {
            next = next->next;
            currB = currB->next;
        }
        else
        {
            prev = last_word_start;
            last_word_start = last_word_start->next;
            next = last_word_start;
            currB = B.head;
        }

        if(currB == NULL)
        {
            prev->next = next;

            while(last_word_start != next)
            {
                if(last_word_start == this->head)
                {
                    this->head = last_word_start->next;
                }
                alpha_numeric *temp = last_word_start->next;
                delete last_word_start;
                last_word_start = temp;
            }

            if(!deleteAll)
                break;

            currB = B.head;
        }
    }
}

だから私の質問は:

  1. この関数の実行時間は? 最悪のシナリオは O(n^2) だと思います

  2. これをより効率的にするにはどうすればよいですか?

最適化などについて学び始めています。これを最適化しようとするときの思考プロセスは何ですか? 主に nlog(n) に落とし込もうとしています。

4

0 に答える 0