public interface ICycle<T>:IEnumerable<T> { T Get(); } public class Cycle<T>:ICycle<T> { private class Node<F> { public Node(F f) { Data=f; } public F Data {get;set;} public Node<F> Next {get;set;} }
private Node<T> Head=null;
private Node<T> Last=null;
public Cycle(IEnumerable<T> list) { if(list==null||!list.Any()){throw new ArgumentException("argument is null or empty.");} foreach(var item in list)
{ if(Head==null){Last=new Node<T>(item);Head=Last;} else{Last.Next=new Node<T>(item);Last=Last.Next;} Last.Next=Head; } Last=Head; } public T Get(){var d=Last.Data;Last=Last.Next;return d;} public IEnumerator<T> GetEnumerator() { var temp=Head; while(temp!=null){var d=temp.Data;temp=temp.Next;yield return d;} } public IEnumerator IEnumerable.GetEnumerator() { var temp=Head; while(temp!=null){var d=temp.Data;temp=temp.Next;yield return d;} } }