VitalyD

Untitled

Jul 22nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.56 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.             LinkedList ll1 = new LinkedList();
  20.             ll1.Add(1);
  21.             ll1.Add(3);
  22.             ll1.Add(4);
  23.             ll1.Add(5);
  24.             foreach (Node a in ll1)
  25.             {
  26.                 Console.WriteLine(a.Data);
  27.             }
  28.             Console.ReadKey();
  29.         }
  30.     }
  31.  
  32.  
  33.  
  34.  
  35.     class LinkedList : IEnumerable<int>
  36.     {
  37.         private Node _root;
  38.         public int Count { get; private set; }
  39.  
  40.         public void Add(int value)
  41.         {
  42.             Node curNode = _root;
  43.  
  44.             if (curNode != null)
  45.             {
  46.                 while (curNode.Next != null)
  47.                 {
  48.                     curNode = curNode.Next;
  49.                 }
  50.                 curNode.Next = new Node(value, null);
  51.             }
  52.             else
  53.             {
  54.                 _root = new Node(value, null);
  55.             }
  56.         }
  57.  
  58.         public void Remove(int value)
  59.         {
  60.             Node curNode = _root;
  61.  
  62.             if (curNode.Data == value)
  63.             {
  64.                 curNode = curNode.Next;
  65.             }
  66.             else
  67.             {
  68.                 Node removeNode = SearchNode(value);
  69.                 if (removeNode.Next != null)
  70.                 {
  71.                     removeNode.Next = removeNode.Next.Next;
  72.                 }
  73.                 else
  74.                 {
  75.                     Console.WriteLine("Такого элемента нет");
  76.                 }
  77.  
  78.             }
  79.         }
  80.  
  81.         public void Balblabla()
  82.         {
  83.             Node currentNode = _root;
  84.             currentNode = currentNode.Next;
  85.         }
  86.  
  87.         public void RemoveAt(int index)
  88.         {
  89.             int count = 0;
  90.             Node currentNode = _root;
  91.             Node prevNode = currentNode;
  92.             while (count <= index)
  93.             {
  94.                 if (count == index)
  95.                 {
  96.                     prevNode.Next = currentNode.Next;
  97.                     currentNode = null;
  98.                 }
  99.                 else
  100.                 {
  101.                     prevNode = currentNode;
  102.                     currentNode = currentNode.Next;
  103.                 }
  104.                 count++;
  105.             }
  106.         }
  107.  
  108.         public void Reverse()
  109.         {
  110.             Node curNode = _root, nextNodes = null;
  111.             while (curNode != null)
  112.             {
  113.                 Node stor = curNode.Next;
  114.                 curNode.Next = nextNodes;
  115.                 nextNodes = curNode;
  116.                 curNode = stor;
  117.             }
  118.             _root = nextNodes;
  119.         }
  120.  
  121.         public bool Contains(int value)
  122.         {
  123.             Node currentNode = _root;
  124.             while (currentNode.Data != value)
  125.             {
  126.                 currentNode = currentNode.Next;
  127.                 if (currentNode.Next == null)
  128.                 {
  129.                     break;
  130.                 }
  131.             }
  132.             if (currentNode.Data == value)
  133.             {
  134.                 Console.WriteLine(true);
  135.                 return true;
  136.             }
  137.             else
  138.             {
  139.                 Console.WriteLine(false);
  140.                 return false;
  141.             }
  142.         }
  143.  
  144.         private Node SearchNode(int value)
  145.         {
  146.             Node curNode = _root;
  147.             while (curNode.Next != null)
  148.             {
  149.                 if (curNode.Next.Data != value)
  150.                 {
  151.                     curNode = curNode.Next;
  152.                 }
  153.                 else
  154.                 {
  155.                     return curNode;
  156.                 }
  157.             }
  158.             return curNode;
  159.         }
  160.  
  161.         public void Show()
  162.         {
  163.             Node currentNode = _root;
  164.             while (currentNode != null)
  165.             {
  166.                 Console.Write(currentNode.Data + " ");
  167.                 currentNode = currentNode.Next;
  168.             }
  169.         }
  170.  
  171.  
  172.         public LinkedListEnumerator GetEnumerator()
  173.         {
  174.             return new LinkedListEnumerator(_root);
  175.         }
  176.  
  177.         IEnumerator IEnumerable.GetEnumerator()
  178.         {
  179.             return GetEnumerator();
  180.         }
  181.  
  182.         IEnumerator<int> IEnumerable<int>.GetEnumerator()
  183.         {
  184.             return GetEnumerator();
  185.         }
  186.     }
  187.  
  188.  
  189.     class LinkedListEnumerator : IEnumerator<int>
  190.     {
  191.         bool isFirst = true;
  192.         Node current;
  193.         Node front;
  194.         public LinkedListEnumerator(Node root)
  195.         {
  196.             current = root;
  197.             front = root;
  198.         }
  199.  
  200.         public object Current
  201.         {
  202.             get
  203.             {
  204.                 return current;
  205.             }
  206.         }
  207.  
  208.         int IEnumerator<int>.Current
  209.         {
  210.             get
  211.             {
  212.                 return current.Data;
  213.             }
  214.         }
  215.  
  216.         public void Dispose()
  217.         {
  218.  
  219.         }
  220.  
  221.         public bool MoveNext()
  222.         {
  223.             if (current == front && isFirst)
  224.             {
  225.                 isFirst = false;
  226.                 return true;
  227.             }
  228.             if (current.Next != null)
  229.             {
  230.                 current = current.Next;
  231.                 return true;
  232.             }
  233.             else
  234.             {
  235.                 return false;
  236.             }
  237.         }
  238.  
  239.         public void Reset()
  240.         {
  241.             current = front;
  242.         }
  243.     }
  244.  
  245.     class ArrayEnumerator : IEnumerator<string>
  246.     {
  247.         public string Current => _data[_currentElement];
  248.         object IEnumerator.Current => Current;
  249.  
  250.         private string[] _data;
  251.         private int _currentElement = -1;
  252.  
  253.         public ArrayEnumerator(params string[] data)
  254.         {
  255.             _data = data;
  256.         }
  257.  
  258.         public void Dispose()
  259.         {
  260.             throw new NotImplementedException();
  261.         }
  262.  
  263.         public bool MoveNext()
  264.         {
  265.             return !(++_currentElement >= _data.Length);
  266.         }
  267.  
  268.         public void Reset()
  269.         {
  270.             _currentElement = -1;
  271.         }
  272.     }
  273.  
  274.     class Node
  275.     {
  276.         public Node Next;
  277.         public int Data;
  278.  
  279.         public Node(int data, Node next)
  280.         {
  281.             Next = next;
  282.             Data = data;
  283.         }
  284.     }
  285. }
Add Comment
Please, Sign In to add comment