私はリンクリストの特定の名前を追加、削除、インデックスで検索、およびカウントするいくつかの反復メソッドを持っていますが、再帰的なソリューションを使用してそれらを実装したいと思います。反復関数を再帰関数に変換する方法はありますか?
私は本当に始める方法がわかりません。変換を行うために私が従うべきガイドラインはありますか?ここで
は私のコードです:
すべて印刷する方法
public void Delete(string name)
{
Node last = lastNode;
if (last.patient.Name == name)
{
firstNode = firstNode.Next;
}
else if (lastNode.patient.Name == name)
{
Node temp = firstNode;
while (temp.Next != lastNode)
{
temp = temp.Next;
}
lastNode = temp;
temp.Next = null;
}
else
{
for (Node current = firstNode; current != null; current = current.Next)
{
if (current.patient.Name == name)
{
last.Next = current.Next;
}
else
{
last = current;
}
}
size--;
}
}
を削除するには、getノード方法
public Node GetNode(string name)
{
for (Node current = firstNode; current != null; current = current.Next)
{
if (current.patient.Name == name)
{
return current;
}
}
return null;
}
LinkedListの方法で
public int CountName(string name)
{
int count = 0;
for (Node current = firstNode; current != null; current = current.Next)
{
count++;
if (current.patient.Name == name) return count;
}
return 0;
}
を特定の名前をカウントします何私のリンクリスト方式
public List<string> PrintAll()
{
List<String> temp = new List<string>();
if (firstNode == null)
{
temp.Add("There are no items in the linked list");
return temp;
}
else
{
Node helpNode = firstNode;
while (helpNode != null)
{
temp.Add(helpNode.GetPatient().Name);
helpNode = helpNode.getNext();
}
return temp;
}
}
SOはコード作成サービスではないので、 – Arion
あなたの 'CountName'メソッドは、指定された名前のノードの数を返すのではありません。指定された名前を持つ* first *ノードまでのノード。それは意図ですか? –
ようこそStackOverflowへ。ヘルプドキュメントの投稿ガイドラインを読み、それに従ってください。 [on topic](http://stackoverflow.com/help/on-topic)および[How to Ask](http://stackoverflow.com/help/how-to-ask)をここで適用してください。 StackOverflowは、デザイン、コーディング、リサーチまたはチュートリアルサービスではありません。 インターネット上には、反復と再帰の間の変換を扱うチュートリアルやその他のサイトや、Stack Overflowに関する以前の多くの質問があります。あなたは「新しい」質問を投稿する前にそれらを使う必要があります。 – Prune