//element of chain.
struct studentRecord
{
int score;
string* name;
int operator !=(studentRecord x) const
{return (score != x.score);}
};
// binsort, theChain is a single linked list of students and its element is student Record
void binSort(chain<studentRecord>& theChain, int range)
{// Sort by score.
// initialize the bins
chain<studentRecord> *bin;
bin = new chain<studentRecord> [range + 1];
// distribute student records from theChain to bins
int numberOfElements = theChain.size();
for (int i = 1; i <= numberOfElements; i++)
{
studentRecord x = theChain.get(0);
theChain.erase(0);
bin[x.score].insert(0,x);
}
// collect elements from bins
for (int j = range; j >= 0; j--)
while (!bin[j].empty())
{
studentRecord x = bin[j].get(0);
bin[j].erase(0); ////////// here
theChain.insert(0,x);
}
delete [] bin;
}
chain<studentRecord>
単一の連結リストです。bin ソートでは、bin はbin[arrange +1]
要素が であるへのポインタchain<studentRecord>
です。コードbin[j].erase(i)
はdelete
ノードになりますi
。bin[j].get(i)
node の要素 (つまり、studentRecord) を取得しますi
。コードでwhile (!bin[j].empty())
はチェーンを空にするために を使っていますが、bin[j] は要素配列なので空にすることはできませんよね?
ありがとう。