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 ConsoleApp18
- {
- class Program
- {
- static void Main(string[] args)
- {
- ListLinked<int> list = new ListLinked<int>();
- list.Add(5);
- list.Add(4);
- list.Add(10);
- list.Add(550);
- list.Add(1);
- foreach (int i in list)
- {
- Console.WriteLine(i);
- }
- Console.ReadKey();
- }
- }
- public class ListLinked<T> : IEnumerable<T>
- {
- private Node<T> _root;
- private Node<T> lastNode;
- public ListLinked()
- {
- _root = new Node<T>(default(T));
- lastNode = _root;
- }
- public void Add(T data)
- {
- lastNode.Next = new Node<T>(data);
- lastNode = lastNode.Next;
- }
- public IEnumerator<T> GetEnumerator()
- {
- return new LLEnumerator<T>(_root);
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- public class LLEnumerator<T> : IEnumerator<T>
- {
- private Node<T> currenNode;
- public LLEnumerator(Node<T> current)
- {
- this.currenNode = current;
- }
- public T Current => currenNode.Data;
- object IEnumerator.Current => Current;
- public bool MoveNext()
- {
- if (currenNode == null)
- {
- return false;
- }
- currenNode = currenNode.Next;
- return (currenNode != null);
- }
- public void Dispose()
- {
- }
- public void Reset()
- {
- throw new NotImplementedException();
- }
- }
- public class Node<T>
- {
- public T Data { get; }
- public Node<T> Next { get; set; }
- public Node(T data)
- {
- Data = data;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment