VitalyD

Untitled

May 20th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 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 list = new ListLinked();
  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.     class ListLinked : IEnumerable<int>
  32.     {
  33.         private Node _root;
  34.         public int Count { get; private set; }
  35.  
  36.         public void Add(int value)
  37.         {
  38.             Node curNode = _root;
  39.  
  40.             if (curNode == null)
  41.             {
  42.                 _root = new Node(value, null);
  43.             }
  44.             else
  45.             {
  46.                 Node newNode = new Node(value, null);
  47.                 newNode.Next = curNode;
  48.                 _root = newNode;
  49.             }
  50.         }
  51.  
  52.         public IEnumerator<int> GetEnumerator()
  53.         {
  54.             Node timeNode = _root;
  55.             while (timeNode != null)
  56.             {
  57.                 yield return timeNode.Data;
  58.                 timeNode = timeNode.Next;
  59.             }
  60.         }
  61.  
  62.         IEnumerator IEnumerable.GetEnumerator()
  63.         {
  64.             throw new NotImplementedException();
  65.         }
  66.     }
  67.  
  68.     class Node
  69.     {
  70.         public Node Next;
  71.         public int Data;
  72.  
  73.         public Node(int data, Node next)
  74.         {
  75.             Next = next;
  76.             Data = data;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment