C# LinkedList<T> 雙向鏈表

來源:互聯網
上載者:User

標籤:

interface IMyList<T>{    // O(1)    // Add an item at the beginning of the list.    void AddFirst(T item);     // O(1)    // Add an item at the end of the list.    void AddLast(T itme);     // O(1)    // Remove the item from the list. If the list contains multiple    // copies/references of the item, remove one of them.    void Remove(T item);     // O(1)    // Reverse the list.    void Reverse(); }class MyList<T> : IMyList<T>, IEnumerable{    private bool _isReverse = false;/    private LinkedList<T> _list = new LinkedList<T>();//雙向鏈表<br>            private Dictionary<T, List<LinkedListNode<T>>> _dict = new Dictionary<T, List<LinkedListNode<T>>>();//用來臨時儲存Node     public void AddFirst(T item)    {        LinkedListNode<T> node = _isReverse ? _list.AddLast(item) : _list.AddFirst(item);        AddDict(item, node);    }     private void AddDict(T item, LinkedListNode<T> node)    {        List<LinkedListNode<T>> samelist = null;        if (!_dict.TryGetValue(item, out samelist))        {            samelist = new List<LinkedListNode<T>>();        }        samelist.Add(node);        _dict[item] = samelist;    }     public void AddLast(T item)    {        LinkedListNode<T> node = _isReverse ? _list.AddFirst(item) : _list.AddLast(item);        AddDict(item, node);    }     public void Remove(T item)    {        List<LinkedListNode<T>> sameList = null;        if (_dict.TryGetValue(item, out sameList))        {            if (sameList.Count > 0)            {                _list.Remove(sameList[0]);                sameList.RemoveAt(0);                _dict.Remove(item);            }        }     }     public void Reverse()    {        _isReverse = !_isReverse; ;    }     /// <summary>    /// 迭代器的實現    /// </summary>    /// <returns></returns>    public IEnumerator<T> GetEnumerator()    {        LinkedListNode<T> node;        if (!_isReverse)            node = _list.First;        else            node = _list.Last;         while (true)        {            if (node == null)                yield break;             yield return node.Value;             if (!_isReverse)                node = node.Next;            else                node = node.Previous;        }    }     /// <summary>    /// 實現迭代  因為他沒有泛型的類型吧    /// </summary>    /// <returns></returns>    IEnumerator IEnumerable.GetEnumerator()    {        return GetEnumerator();    }}

 

C# LinkedList<T> 雙向鏈表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.