データが重複している連続したアイテムを削除する関数を書いています。例:リストを渡す
-> a-> b-> c-> c-> a-> b-> b-> b-> a-> null
結果として
-> a-> b-> c-> a-> b-> a-> null
リスト項目の定義と関数宣言を以下に示します。
struct litem {
char data;
litem* next;
};
Moコードは次のようになります
int remove_dumplicates(litem *&list)
{
int count = 0;
struct litem * current = NULL;
current = list;
struct litem *deleteNode;
if (current == NULL ) return;
while(current->next != NULL)
{
if ( current->data == current->next->data) // check for the duplicates
{
count++;
deleteNode =current->next;
current>next= current->next->next;
delete deleteNode;
}
return (count);
}
}
これは、望ましい結果を達成するための正しい方法ですか?