簡體   English   中英

實現C#IEnumerable <T> 對於LinkedList類

[英]Implementing C# IEnumerable<T> for a LinkedList class

我正在嘗試使用Linux上的monoDevelop在C#中編寫自定義LinkedList類,僅僅是為了測試和學習。 以下代碼永遠不會編譯,我不知道為什么! 它甚至沒有告訴我什么是錯的。 它的全部內容是:錯誤:編譯器似乎崩潰了。 檢查構建輸出板以獲取詳細信息。 當我去檢查輸出板時,它也沒有用處:Unhandled Exception: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();
}

然而! 如果你手動實現這個,你就錯過了一個技巧。 “迭代器塊”會容易得多。

以下是完整的實施; 你並不需要在所有寫一個枚舉類(你可以刪除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();
}

當我嘗試粘貼的代碼時,我在嘗試構建時遇到2個錯誤。

myLinkedList'沒有實現接口成員'System.Collections.IEnumerable.GetEnumerator()'。 '.myLinkedList.GetEnumerator()'無法實現'System.Collections.IEnumerable.GetEnumerator()',因為它沒有匹配的返回類型'System.Collections.IEnumerator'。

解決方案是在第一個類中實現以下內容。

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

第二個錯誤是:

myLinkedList.myLinkedListIterator'沒有實現接口成員'System.Collections.IEnumerator.Current'。 'JonasApplication.myLinkedList.myLinkedListIterator.Current'無法實現'System.Collections.IEnumerator.Current',因為它沒有匹配的'object'返回類型。

第二類的解決方案可能是在第二類中實現的。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM