VitalyD

Untitled

Jun 4th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.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 ConsoleApp14
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             ArrayEnumerator enumerator = new ArrayEnumerator("Test1", "Test2", "Test3", "Test4");
  15.             while (enumerator.MoveNext())
  16.             {
  17.                 Console.WriteLine(enumerator.Current);
  18.             }
  19.         }
  20.     }
  21.  
  22.  
  23.     class LinkedList : IEnumerable<int>
  24.     {
  25.         private Node _root;
  26.         public int Count { get; private set; }
  27.  
  28.         public void Add(int value)
  29.         {
  30.             Node curNode = _root;
  31.  
  32.             if (curNode != null)
  33.             {
  34.                 while (curNode.Next != null)
  35.                 {
  36.                     curNode = curNode.Next;
  37.                 }
  38.                 curNode.Next = new Node(value, null);
  39.             }
  40.             else
  41.             {
  42.                 _root = new Node(value, null);
  43.             }
  44.         }
  45.  
  46.         public void Remove(int value)
  47.         {
  48.             Node curNode = _root;
  49.  
  50.             if (curNode.Data == value)
  51.             {
  52.                 curNode = curNode.Next;
  53.             }
  54.             else
  55.             {
  56.                 Node removeNode = SearchNode(value);
  57.                 if (removeNode.Next != null)
  58.                 {
  59.                     removeNode.Next = removeNode.Next.Next;
  60.                 }
  61.                 else
  62.                 {
  63.                     Console.WriteLine("Такого элемента нет");
  64.                 }
  65.  
  66.             }
  67.         }
  68.  
  69.  
  70.         public void RemoveAt(int index)
  71.         {
  72.             int count = 0;
  73.             Node currentNode = _root;
  74.             Node prevNode = currentNode;
  75.             while (count <= index)
  76.             {
  77.                 if (count == index)
  78.                 {
  79.                     prevNode.Next = currentNode.Next;
  80.                     currentNode = null;
  81.                 }
  82.                 else
  83.                 {
  84.                     prevNode = currentNode;
  85.                     currentNode = currentNode.Next;
  86.                 }
  87.                 count++;
  88.             }
  89.         }
  90.  
  91.         public void Reverse()
  92.         {
  93.             Node curNode = _root, nextNodes = null;
  94.             while (curNode != null)
  95.             {
  96.                 Node stor = curNode.Next;
  97.                 curNode.Next = nextNodes;
  98.                 nextNodes = curNode;
  99.                 curNode = stor;
  100.             }
  101.             _root = nextNodes;
  102.         }
  103.  
  104.         public bool Contains(int value)
  105.         {
  106.             Node currentNode = _root;
  107.             while (currentNode.Data != value)
  108.             {
  109.                 currentNode = currentNode.Next;
  110.                 if (currentNode.Next == null)
  111.                 {
  112.                     break;
  113.                 }
  114.             }
  115.             if (currentNode.Data == value)
  116.             {
  117.                 Console.WriteLine(true);
  118.                 return true;
  119.             }
  120.             else
  121.             {
  122.                 Console.WriteLine(false);
  123.                 return false;
  124.             }
  125.         }
  126.  
  127.         private Node SearchNode(int value)
  128.         {
  129.             Node curNode = _root;
  130.             while (curNode.Next != null)
  131.             {
  132.                 if (curNode.Next.Data != value)
  133.                 {
  134.                     curNode = curNode.Next;
  135.                 }
  136.                 else
  137.                 {
  138.                     return curNode;
  139.                 }
  140.             }
  141.             return curNode;
  142.         }
  143.  
  144.         public void Show()
  145.         {
  146.             Node currentNode = _root;
  147.             while (currentNode != null)
  148.             {
  149.                 Console.Write(currentNode.Data + " ");
  150.                 currentNode = currentNode.Next;
  151.             }
  152.         }
  153.  
  154.         public IEnumerator<int> GetEnumerator()
  155.         {
  156.             throw new NotImplementedException();
  157.         }
  158.  
  159.         IEnumerator IEnumerable.GetEnumerator()
  160.         {
  161.             return GetEnumerator();
  162.         }
  163.     }
  164.  
  165.  
  166.     class LinkedListEnumerator : IEnumerator<int>
  167.     {
  168.         public int Current => throw new NotImplementedException();
  169.  
  170.         object IEnumerator.Current => Current;
  171.  
  172.         private Node _rootnode;
  173.         private Node firstNode;
  174.  
  175.         public void Dispose()
  176.         {
  177.             throw new NotImplementedException();
  178.         }
  179.  
  180.         public bool MoveNext()
  181.         {
  182.             Node curNode = _rootnode;
  183.             firstNode = _rootnode;
  184.  
  185.             if (_rootnode.Next != null)
  186.             {
  187.                 _rootnode = _rootnode.Next;
  188.                 return true;
  189.             }
  190.             return false;
  191.         }
  192.  
  193.         public void Reset()
  194.         {
  195.             _rootnode = firstNode;
  196.         }
  197.     }
  198.  
  199.     class ArrayEnumerator : IEnumerator<string>
  200.     {
  201.         public string Current => _data[_currentElement];
  202.         object IEnumerator.Current => Current;
  203.  
  204.         private string[] _data;
  205.         private int _currentElement = -1;
  206.  
  207.         public ArrayEnumerator(params string[] data)
  208.         {
  209.             _data = data;
  210.         }
  211.  
  212.         public void Dispose()
  213.         {
  214.             throw new NotImplementedException();
  215.         }
  216.  
  217.         public bool MoveNext()
  218.         {
  219.             return !(++_currentElement >= _data.Length);
  220.         }
  221.  
  222.         public void Reset()
  223.         {
  224.             _currentElement = -1;
  225.         }
  226.     }
  227.  
  228.     class Node
  229.     {
  230.         public Node Next;
  231.         public int Data;
  232.  
  233.         public Node(int data, Node next)
  234.         {
  235.             Next = next;
  236.             Data = data;
  237.         }
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment