VitalyD

Untitled

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