0

最終的に、プログラムは名前のリストをアルファベット順に出力し、その名前に関連付けられた追加の属性も表示します。つまり、出力画面は次のようになります。

Ares: Greek, fire, sword.
Freia: Norse, water, bow and arrow.
Poseidon: Greek, horses, ocean.
Thor: Norse, chariot, hammer.
Zeus: Greek, cloud, lightning.

繰り返しますが、このリストでは最初の名前はアルファベット順に並べられていますが、属性はそれらの横に印刷されています。int main( ) に関しては、これらの名前をどのように並べ替えて整理する必要があるのか​​ わかりません。ソートする必要があるソートされていないリストがあります(これらの名前を正しい順序に追加/挿入する関数を使用)。

    //
    // This is a standard library support code to the chapters of the book
    // "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
    //

    #ifndef STD_LIB_FACILITIES_GUARD
    #define STD_LIB_FACILITIES_GUARD 1

    #include <cmath>
    #include <iostream>
    #include <vector>
    #include <stdexcept>
    #include <string>

    using namespace std;

    //------------------------------------------------------------------------------

    // Helper function to show an error message
    inline void error(const string& errormessage)
    {
    throw runtime_error(errormessage);
    }

    //------------------------------------------------------------------------------

    #endif // STD_LIB_FACILITIES_GUARD

    //------------------------------------------------------------------------------

    struct Link {
    string name;
    string mythology;
     string vehicle;
    string weapon;

    Link* prev;
    Link* succ;
    Link(const string& n, const string& a, const string& b, const string&c,Link* p = 0,    
    Link* s = 0)
: name(n), mythology(a), vehicle(b), weapon(c), prev(p), succ(s) { }
    };



    Link* insert(Link* p, Link* n)    // insert n before p; return n
    {
    if (n==0) return p;
    if (p==0) return n;
     n->succ = p;        // p comes after n
    if (p->prev) p->prev->succ = n;
     n->prev = p->prev;    // p's predecessor becomes n's predecessor
     p->prev = n;        // n becomes p's predecessor
     return n;
     }

    void print_all(Link *p)
    {
Link *current;
current = p;
while(current)
{
    cout<<"For this link we have: \n";
    cout<<"Name: "<<current->name<<".\n";
    cout<<"Info1: "<<current->mythology<<".\n";
    cout<<"Info2: "<<current->vehicle<<".\n";
    cout<<"Info3: "<<current->weapon<<".\n";
    current = current->succ;
}

    }
    Link * add_after_find(Link *p, Link *n,const string& s )
     {   Link *current = 0;
current = p;
/* empty list */
if(p == 0)
{   cout<<"List is empty so string not found so not added after it. \n";

    return  0;
}
/*  DO WE NEED ONE LINK ONLY */
else if(p->succ == 0)   /* one link only */
{
    if(p->name == s)
    {

        /* add after link with s */
        /* p in front */
        p->succ = n;
        n->prev = p;
        p->prev = 0;
        n->succ = 0;

        return p;      
    }   /* end of if names =  */
    else {
        cout<<"String not found in link listed so not added. \n";
        return p;

    }

}  /* end of one link */

else /* two or more links */

{   
    current = p;
    while(current->succ)
    {
        if (s == current->name)


        {

            /* then n goes AFTER this link */
            n->prev = current;
            n->succ = current->succ;
            current->succ = n;


            return p;
        }  /* end of name matches */

        else 
        {
            current = current->succ;
        }
    }// end of while
    /* if outside of while then we are at last link with a current -> name 
     so s not found  */
    cout<<"String is not found so not add after it. \n";
    return p;
}  // end of else 2 or more
}  // end of function

int main()
{      
    Link*newlist = new Link("Thor","Norse","chariot","hammer");
    newlist = add_after_find(newlist,new Link("Hera","Greek", "horse", "arrow"),"Thor");
    newlist = add_after_find(newlist,new Link("Poseidon","Greek", "ocean", "trident"),"Freia");
     newlist = add_after_find(newlist,new Link("Ares","Greek", "fire", "sword"),"Poseidon");
     newlist = add_after_find(newlist,new Link("Zeus","Greek", "cloud", "lightning"),"Ares");

    print_all(newlist);
    cout<<"Now let's alphabetize these five gods.\n";
    system("Pause");
    return 0;
}
4

1 に答える 1

1

これは宿題だと思いますが、そうでない場合、簡単な答えは、そのコンテナー内std::listのメソッドを使用する必要があるということです。sort

何を入れmainますか?ほとんどのsort_list( newlist )場合、: の行に何かがあり、ポインターが参照によって渡されます (リストの先頭がおそらく変更されるため)。それをどのように実装するかについては、実装したいソートアルゴリズムに依存します.最も単純なのはおそらくバブルソートであり、リストの次の最良の選択はマージソートです. それらについては Google で検索してください。アルゴリズムについてサポートが必要な場合は、戻って質問してください。

それまでの間、私が質問へのコメントとして提起した問題に取り組むことをお勧めします: コードのフォーマット、メモリ リーク (位置が見つからない場合の挿入時とプログラムの終了時の両方)、データ構造の正確さ常に...詳細な分析は行っていませadd_after_findんが、リストの末尾に要素を追加する必要がある場合に失敗する可能性があると感じています...並べ替えを検討する前に、作成する必要があります入力が正しいことを確認してください。コードを追加するよりも、現在の問題をそのままデバッグする方が簡単です。

于 2011-11-10T08:57:17.150 に答える