生活随笔
收集整理的這篇文章主要介紹了
                                
C#集合练习题(链表LinkedListT)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
 
                                
                            
                            
                            封裝一個集合
 提供push(T value)在尾部添加新元素,
pop()刪除并返回最后一個元素,、
shift()刪除并返回第一個元素,
unshift(T value)在首位添加新元素;
底層用鏈表模式保存數據
 實現用foreach循環自定義集合
 
 class MyCollecion<T> : IEnumerable<T>{LinkedList<T> arr = new LinkedList<T>();public LinkedListNode<T> First{get{return arr.First;}}//在尾部添加新元素public void Push(T value){arr.AddLast(value);}//刪除并返回最后一個元素public T Pop(){LinkedListNode<T> node = arr.Last;arr.RemoveLast();return node.Value;}//刪除并返回第一個元素,public T Shift(){LinkedListNode<T> node = arr.First;arr.RemoveFirst();return node.Value;}//在首位添加新元素public void Unshift(T value){arr.AddFirst(value);}public IEnumerator<T> GetEnumerator(){return new Enumtor<T>(this);}IEnumerator IEnumerable.GetEnumerator(){throw new NotImplementedException();}}/// <summary>/// 迭代器/// </summary>/// <typeparam name="T"></typeparam>internal class Enumtor<T> : IEnumerator<T>{private MyCollecion<T> myCollecion;LinkedListNode<T> node;public Enumtor(MyCollecion<T> myCollecion){this.myCollecion = myCollecion;this.node = myCollecion.First;//初始化指向頭節點}public T Current{get{LinkedListNode<T> tempNode = node;node = node.Next;return tempNode.Value;}}object IEnumerator.Current{get{return this;}}public void Dispose(){Console.WriteLine();}public bool MoveNext(){//如果節點不為空 if (node != null){return true;}return false;}public void Reset(){this.node = myCollecion.First;}} 
測試類:
 
                            總結
                            
                                以上是生活随笔為你收集整理的C#集合练习题(链表LinkedListT)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。