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 ConsoleApp20
- {
- class Program
- {
- static void Main(string[] args)
- {
- //ArrayEnumerator enumerator = new ArrayEnumerator("Test1", "Test2", "Test3", "Test4");
- //while (enumerator.MoveNext())
- //{
- // Console.WriteLine(enumerator.Current);
- //}
- LinkedListEnumerator enumLL = new LinkedListEnumerator(10, 15, 104, 211);
- while (enumLL.MoveNext())
- {
- Console.WriteLine(enumLL.Current);
- }
- }
- }
- 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 IEnumerator<int> GetEnumerator()
- {
- throw new NotImplementedException();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- class LinkedListEnumerator : IEnumerator<int>
- {
- public int Current => _data[curElem];
- object IEnumerator.Current => Current;
- private int[] _data;
- private int curElem = -1;
- public LinkedListEnumerator(params int[] data)
- {
- _data = data;
- }
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public bool MoveNext()
- {
- curElem++;
- if (curElem >= _data.Length) return false;
- else return true;
- }
- public void Reset()
- {
- curElem = -1;
- }
- }
- 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment