gpt4 book ai didi

c# - 为 LinkedList 类实现 C# IEnumerable

转载 作者:行者123 更新时间:2023-11-30 13:11:21 25 4
gpt4 key购买 nike

我正在尝试在 Linux 上使用 monoDevelop 在 C# 中编写自定义 LinkedList 类,只是为了测试和学习。下面的代码永远不会编译,我不知道为什么!!它甚至不告诉我出了什么问题。它只是说:错误:编译器似乎已崩溃。检查构建输出板以获取详细信息。当我去检查输出板时,它也没有帮助:未处理的异常:System.ArgumentException:必须在泛型类型定义上声明指定的字段。参数名称:字段

我能做什么?

using System;
using System.Text;
using System.Collections.Generic;

namespace LinkedList
{
public class myLinkedList<T> : IEnumerable<T>
{
//List Node class
//===============
private class ListNode<T>
{
public T data;
public ListNode<T> next;

public ListNode(T d)
{
this.data = d;
this.next = null;
}

public ListNode(T d, ListNode<T> n)
{
this.data = d;
this.next = n;
}
}

//priavte fields
//===============
private ListNode<T> front;
private int size;

//Constructor
//===========
public myLinkedList ()
{
front = null;
size = 0;
}


//public methods
//===============
public bool isEmpty()
{
return (size == 0);
}

public bool addFront(T element)
{
front = new ListNode<T>(element, front);
size++;
return true;
}

public bool addBack(T element)
{
ListNode<T> current = front;
while (current.next != null)
{
current = current.next;
}

current.next = new ListNode<T>(element);
size++;
return true;
}

public override string ToString()
{
ListNode<T> current = front;
if(current == null)
{
return "**** Empty ****";
}
else
{
StringBuilder sb = new StringBuilder();
while (current.next != null)
{
sb.Append(current.data + ", ");
current = current.next;
}
sb.Append(current.data);

return sb.ToString();
}
}

// These make myLinkedList<T> implement IEnumerable<T> allowing
// a LinkedList to be used in a foreach statement.
public IEnumerator<T> GetEnumerator()
{
return new myLinkedListIterator<T>(front);
}


private class myLinkedListIterator<T> : IEnumerator<T>
{
private ListNode<T> current;
public virtual T Current
{
get
{
return current.data;
}
}
private ListNode<T> front;

public myLinkedListIterator(ListNode<T> f)
{
front = f;
current = front;
}

public bool MoveNext()
{
if(current.next != null)
{
current = current.next;
return true;
}
else
{
return false;
}
}

public void Reset()
{
current = front;
}

public void Dispose()
{
throw new Exception("Unsupported Operation");
}
}
}
}

最佳答案

您需要添加非通用API;所以添加到迭代器:

object System.Collections.IEnumerator.Current { get { return Current;  } }

和可枚举的:

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

然而!如果您手动执行此操作,那么您就错过了一个技巧。 “迭代器 block ”会容易得多。

下面是一个完整的实现;您根本不需要编写枚举器类(您可以完全删除 myLinkedListIterator<T>):

public IEnumerator<T> GetEnumerator()
{
var node = front;
while(node != null)
{
yield return node.data;
node = node.next;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

关于c# - 为 LinkedList 类实现 C# IEnumerable<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12069205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com