Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp14
- {
- class Program
- {
- static void Main(string[] args)
- {
- ArrayEnumerator enumerator = new ArrayEnumerator("Test1", "Test2", "Test3", "Test4");
- while (enumerator.MoveNext())
- {
- Console.WriteLine(enumerator.Current);
- }
- LinkedList ll1 = new LinkedList();
- ll1.Add(1);
- ll1.Add(3);
- ll1.Add(4);
- ll1.Add(5);
- foreach (Node a in ll1)
- {
- Console.WriteLine(a.Data);
- }
- Console.ReadKey();
- }
- }
- class LinkedList : IEnumerable<int>
- {
- private Node _root;
- public int Count { get; private set; }
- public void Add(int value)
- {
- Node curNode = _root;
- if (curNode != null)
- {
- while (curNode.Next != null)
- {
- curNode = curNode.Next;
- }
- curNode.Next = new Node(value, null);
- }
- else
- {
- _root = new Node(value, null);
- }
- }
- public void Remove(int value)
- {
- Node curNode = _root;
- if (curNode.Data == value)
- {
- curNode = curNode.Next;
- }
- else
- {
- Node removeNode = SearchNode(value);
- if (removeNode.Next != null)
- {
- removeNode.Next = removeNode.Next.Next;
- }
- else
- {
- Console.WriteLine("Такого элемента нет");
- }
- }
- }
- public void Balblabla()
- {
- Node currentNode = _root;
- currentNode = currentNode.Next;
- }
- public void RemoveAt(int index)
- {
- int count = 0;
- Node currentNode = _root;
- Node prevNode = currentNode;
- while (count <= index)
- {
- if (count == index)
- {
- prevNode.Next = currentNode.Next;
- currentNode = null;
- }
- else
- {
- prevNode = currentNode;
- currentNode = currentNode.Next;
- }
- count++;
- }
- }
- public void Reverse()
- {
- Node curNode = _root, nextNodes = null;
- while (curNode != null)
- {
- Node stor = curNode.Next;
- curNode.Next = nextNodes;
- nextNodes = curNode;
- curNode = stor;
- }
- _root = nextNodes;
- }
- public bool Contains(int value)
- {
- Node currentNode = _root;
- while (currentNode.Data != value)
- {
- currentNode = currentNode.Next;
- if (currentNode.Next == null)
- {
- break;
- }
- }
- if (currentNode.Data == value)
- {
- Console.WriteLine(true);
- return true;
- }
- else
- {
- Console.WriteLine(false);
- return false;
- }
- }
- private Node SearchNode(int value)
- {
- Node curNode = _root;
- while (curNode.Next != null)
- {
- if (curNode.Next.Data != value)
- {
- curNode = curNode.Next;
- }
- else
- {
- return curNode;
- }
- }
- return curNode;
- }
- public void Show()
- {
- Node currentNode = _root;
- while (currentNode != null)
- {
- Console.Write(currentNode.Data + " ");
- currentNode = currentNode.Next;
- }
- }
- public LinkedListEnumerator GetEnumerator()
- {
- return new LinkedListEnumerator(_root);
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- IEnumerator<int> IEnumerable<int>.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- class LinkedListEnumerator : IEnumerator<int>
- {
- bool isFirst = true;
- Node current;
- Node front;
- public LinkedListEnumerator(Node root)
- {
- current = root;
- front = root;
- }
- public object Current
- {
- get
- {
- return current;
- }
- }
- int IEnumerator<int>.Current
- {
- get
- {
- return current.Data;
- }
- }
- public void Dispose()
- {
- }
- public bool MoveNext()
- {
- if (current == front && isFirst)
- {
- isFirst = false;
- return true;
- }
- if (current.Next != null)
- {
- current = current.Next;
- return true;
- }
- else
- {
- return false;
- }
- }
- public void Reset()
- {
- current = front;
- }
- }
- class ArrayEnumerator : IEnumerator<string>
- {
- public string Current => _data[_currentElement];
- object IEnumerator.Current => Current;
- private string[] _data;
- private int _currentElement = -1;
- public ArrayEnumerator(params string[] data)
- {
- _data = data;
- }
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public bool MoveNext()
- {
- return !(++_currentElement >= _data.Length);
- }
- public void Reset()
- {
- _currentElement = -1;
- }
- }
- class Node
- {
- public Node Next;
- public int Data;
- public Node(int data, Node next)
- {
- Next = next;
- Data = data;
- }
- }
- }
Add Comment
Please, Sign In to add comment