VitalyD

Untitled

Jun 1st, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.10 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp18
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             ListLinked<int> list = new ListLinked<int>();
  15.  
  16.             list.Add(5);
  17.             list.Add(4);
  18.             list.Add(10);
  19.             list.Add(550);
  20.             list.Add(1);
  21.  
  22.             foreach (int i in list)
  23.             {
  24.                 Console.WriteLine(i);
  25.             }
  26.  
  27.         Console.ReadKey();
  28.         }
  29.     }
  30.  
  31.  
  32.     public class ListLinked<T> : IEnumerable<T>
  33.     {
  34.         private Node<T> _root;
  35.         private Node<T> lastNode;
  36.  
  37.         public ListLinked()
  38.         {
  39.             _root = new Node<T>(default(T));
  40.             lastNode = _root;
  41.         }
  42.  
  43.         public void Add(T data)
  44.         {
  45.             lastNode.Next = new Node<T>(data);
  46.             lastNode = lastNode.Next;
  47.         }
  48.  
  49.         public IEnumerator<T> GetEnumerator()
  50.         {
  51.             return new LLEnumerator<T>(_root);
  52.         }
  53.  
  54.         IEnumerator IEnumerable.GetEnumerator()
  55.         {
  56.             return GetEnumerator();
  57.         }
  58.     }
  59.  
  60.  
  61.     public class LLEnumerator<T> : IEnumerator<T>
  62.     {
  63.         private Node<T> currenNode;
  64.  
  65.         public LLEnumerator(Node<T> current)
  66.         {
  67.             this.currenNode = current;
  68.         }
  69.  
  70.         public T Current => currenNode.Data;
  71.  
  72.         object IEnumerator.Current => Current;
  73.  
  74.         public bool MoveNext()
  75.         {
  76.             if (currenNode == null)
  77.             {
  78.                 return false;
  79.             }
  80.             currenNode = currenNode.Next;
  81.             return (currenNode != null);
  82.         }
  83.  
  84.         public void Dispose()
  85.         {
  86.  
  87.         }
  88.  
  89.         public void Reset()
  90.         {
  91.             throw new NotImplementedException();
  92.         }
  93.     }
  94.     public class Node<T>
  95.     {
  96.         public T Data { get; }
  97.         public Node<T> Next { get; set; }
  98.         public Node(T data)
  99.         {
  100.             Data = data;
  101.         }
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment