免責事項:これは課題のためのものですが、私はあなたのコードを書くことを望んでいません。私は間違っていることや正しい方向への指摘をアドバイスしています。クラスメンバ(変数?)を関数に渡すにはどうすればよいですか?
フォーマットされたデータ(時間、気温、風速)を持つファイルがあります。私は3つの異なる "連鎖"でリンクリストを作成することになっています。各チェーンは同じデータを持っていますが、時間のあるチェインはすべてのデータを時系列順に読み込み、テンポラリのチェーンは一時的な順番でデータを読み込み、風速のチェーンは風速順のデータを読み込みます。
私は、指定されたポインタからデータを読み込み、リストを時間通りに正しく構成できる関数を作成しました。しかし今、私は同じ関数を使って同じポインタを読み込み、他の2つのリストを整理しようとしています。ここで
は、関連するコードです:
void datalogger::addData(int timestamp, double temperature, double windspeed){
weatherdata *n;
n = new weatherdata;
n->time = timestamp;
n->temp = temperature;
n->wind = windspeed;
n->next = nullptr;
ll.addData(n, 1);
}
void linkedlist::addData(weatherdata *newData, int selectionNo){
weatherdata *selection;
switch(selectionNo){
//linkedlist: TIME
case 1:
selection = timeHead;
break;
//linkedList: TEMP
case 2:
selection = tempHead;
break;
//linkedList: WIND
case 3:
selection = windHead;
break;
}
int marker = 0;
weatherdata *node;
weatherdata *current;
weatherdata *nextNumber;
node = new weatherdata;
node = newData;
node->next = nullptr;
//very first entry
if (selection == nullptr){
selection = node;
marker = 1;
}
//item should instead be in the first position
if (newData->time < selection->time){
nextNumber = timeHead;
selection = node;
node->next = nextNumber;
}
else {
//traverse the list until we find the correct place to put it in
current = selection;
nextNumber = selection->next;
//while "not yet at the end of the list"
while (nextNumber != nullptr){
//if its the same as another item DELETE
if ((newData->time == current->time) || (newData->time == nextNumber->time)) {
marker = 1;
break;
}
//the new item goes between the current and nextNumber
else if ((current->time < newData->time) && (newData->time < nextNumber->time)){
current->next = node;
node->next = nextNumber;
break;
}
//otherwise increment current and nextNumber for the next comparison
else {
current = nextNumber;
nextNumber = nextNumber->next;
}
}
//item goes at the end of the list
if ((nextNumber == nullptr) && (marker != 1)){
current->next = node;
}
}
switch(selectionNo){
case 1:
timeHead = selection;
break;
case 2:
tempHead = selection;
break;
case 3:
windHead = selection;
break;
}
}
基本的に私が何をしたいのか、このオンされています
whicheverLinkedListHeadIwant:このに
timeHead->時間
を - >のいずれかのクラスメンバーが好ましい
このように、同じ機能を使用して3つのリンクされたリストをすべて処理できます。ご存知のように、linkedlist :: addData関数の最初にswitch文を使用して、 "whicheverLinkedListHeadIwant"を処理するコードを取得できましたが、私の人生では "whicheverClassMemberIwant"を取得することはできません作業。
これは私が試したものです:
char* classMemberSelection = "temp";
または char* classMemberSelection = "wind"
whicheverLinkedListHeadIwant->classMemberSelection;
をしかし、コンパイラはちょうどclassMemberSelectionがメンバーではないというエラーを返します。私はそれが欲しいと思っていても、第二の部分を作る方法を失っている。
この質問が意味をなさない場合は申し訳ありません。私は可能な限り明確になるように最善を尽くしましたが、私はまだこれのすべてで初心者です。私がプロジェクトの追加部分を投稿する必要があるなら、私は喜んでくれるでしょう。
を呼び出すここでしようとするカップルのアイデアです。 3つすべてにデータを追加します。次に、すべての項目を追加した後で3つのリストをソートします。 B)挿入する項目をパラメータとし、挿入機能が項目を配置する場所を決定するために使用する3つの比較関数のいずれかへのポインタをとる、リンクされたリスト内の1つの挿入関数。 – user4581301
あなたが試したものがうまくいかない理由に関する注記:あなたのコードを読みやすくするために使用している素敵な名前はすべてコンパイラによって取り除かれ、オフセットとアドレスに変換されます。プログラムの 'addData'は0xFEDD00Dのようなもので、' selection'はスタックポインタ+16です。 "wind"は実行時に何も意味しません。コンパイラは変数名ではなく単なる文字列です。 – user4581301
これは疑わしい 'node = new weatherdata; node = newData; ' – greatwolf