fr0stn1k

Первый таск (IEnumerable)

Jul 11th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.09 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace CSHARPMEDIUM
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             LinkedList<int> list = new LinkedList<int>();
  12.             list.Add(10);
  13.             list.Add(15);
  14.             list.Add(20);
  15.  
  16.             // Тест 1-го таска (IEnumerable)
  17.             Book myLib = new Book("Книга с книгами");
  18.             Book b1 = new Book("Blood, sweat and Pixels");
  19.             Book b2 = new Book("C#4.0");
  20.             b1.Parent = myLib;
  21.             b2.Parent = myLib;
  22.  
  23.             foreach (Book item in myLib)
  24.             {
  25.                 Console.WriteLine($"Book: {item.Name}");
  26.             }
  27.             // Конец теста
  28.  
  29.             list.ForEach(Console.WriteLine);
  30.             Console.WriteLine(list.Count);
  31.  
  32.             Stack<string> strings = new Stack<string>();
  33.             strings.Push("Test1");
  34.             strings.Push("Test2");
  35.             strings.Push("Test3");
  36.             strings.Push("Test4");
  37.             Console.WriteLine(strings.Pop());
  38.             Console.WriteLine(strings.Pop());
  39.             Console.WriteLine(strings.Pop());
  40.  
  41.  
  42.         }
  43.     }
  44.  
  45.     //Задача 1: Сделать реализацию IEnumerable
  46.  
  47.     class Book : IEnumerable<Book>
  48.     {
  49.         public static List<Book> Library = new List<Book>();
  50.         public Book Parent;
  51.         public string Name;
  52.  
  53.         public Book(string name)
  54.         {
  55.             Library.Add(this);
  56.             Name = name;
  57.         }
  58.  
  59.         public IEnumerator<Book> GetEnumerator()
  60.         {
  61.             return new LibraryEnumerator(Library.FindAll((b) => b.Parent == this).ToArray());
  62.         }
  63.  
  64.         IEnumerator IEnumerable.GetEnumerator()
  65.         {
  66.             return GetEnumerator();
  67.         }
  68.     }
  69.  
  70.     class LibraryEnumerator : IEnumerator<Book>
  71.     {
  72.         public Book Current => _elements[_current];
  73.         private Book[] _elements;
  74.         private int _current = -1;
  75.         object IEnumerator.Current => Current;
  76.  
  77.         public LibraryEnumerator(Book[] elements)
  78.         {
  79.             _elements = elements;
  80.         }
  81.  
  82.         public void Dispose()
  83.         {
  84.             _elements = null;
  85.         }
  86.  
  87.         public bool MoveNext()
  88.         {
  89.             _current++;
  90.             return _current < _elements.Length;
  91.         }
  92.  
  93.         public void Reset()
  94.         {
  95.             _current = -1;
  96.         }
  97.     }
  98.  
  99.  
  100.     // Method GetCurrent()
  101.  
  102.     //Задача 2: Сделать метод Contains. Проверяет есть ли заданное значенеи в спике
  103.     //Задача 3: Сделать метод Remove, который удаляет эллемент под каким-либо номером
  104.     //Задача 4: Оптимизировать добавление эллемента, через хранение ссылки на конец списка
  105.  
  106.     class LinkedList<T>
  107.     {
  108.         private ListElement _start;
  109.  
  110.         public int Count
  111.         {
  112.             get
  113.             {
  114.                 int count = 0;
  115.                 ForEach((x) => count++);
  116.                 return count;
  117.             }
  118.         }
  119.  
  120.         public void Add(T data)
  121.         {
  122.             var newElement = new ListElement(data);
  123.  
  124.             if (_start == null)
  125.             {
  126.                 _start = newElement;
  127.                 return;
  128.             }
  129.  
  130.             ListElement current = _start;
  131.             while (current.Next != null)
  132.             {
  133.                 current = current.Next;
  134.             }
  135.  
  136.             current.Next = newElement;
  137.         }
  138.  
  139.         public void ForEach(Action<T> body)
  140.         {
  141.             ListElement current = _start;
  142.             while (current != null)
  143.             {
  144.                 body(current.Data);
  145.                 current = current.Next;
  146.             }
  147.         }
  148.  
  149.         public class ListElement
  150.         {
  151.             public static T StaticData;
  152.  
  153.             public T Data;
  154.             public ListElement Next;
  155.  
  156.             public ListElement(T data)
  157.             {
  158.                 Data = data;
  159.             }
  160.         }
  161.     }
  162.  
  163.     //Задача 5: сделать обобщённую очередь
  164.  
  165.     class Stack<T>
  166.     {
  167.         private StackElement _head;
  168.  
  169.         public void Push(T data)
  170.         {
  171.             var newElement = new StackElement(data);
  172.             newElement.Next = _head;
  173.             _head = newElement;
  174.         }
  175.  
  176.         public T Pop()
  177.         {
  178.             var h = _head;
  179.             _head = h.Next;
  180.             return h.Data;
  181.         }
  182.  
  183.         public T Peek()
  184.         {
  185.             return _head.Data;
  186.         }
  187.  
  188.         public Stack<T> Clone()
  189.         {
  190.             Stack<T> newStack = new Stack<T>();
  191.             newStack._head = _head;
  192.             return newStack;
  193.         }
  194.  
  195.         public class StackElement
  196.         {
  197.             public T Data;
  198.             public StackElement Next;
  199.  
  200.             public StackElement(T data)
  201.             {
  202.                 Data = data;
  203.             }
  204.         }
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment