Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Iterator
- {
- class Program
- {
- static void Main(string[] args)
- {
- //Numbers numbers = new Numbers();
- //foreach (int n in numbers)
- //{
- // Console.WriteLine(n);
- //}
- DoublyLinkedList<object> list = new DoublyLinkedList<object>();
- list.AddFirst("Harry");
- list.Add("Bob");
- list.Add("John");
- list.Add("Kate");
- list.Add(3.1416);
- list.Remove("John");
- foreach (var x in list.BackEnumerator())
- {
- Console.WriteLine(x);
- }
- }
- }
- class DoublyNode<T>
- {
- public T Data { get; set; }
- public DoublyNode<T> Previous { get; set; }
- public DoublyNode<T> Next { get; set; }
- public DoublyNode(T data)
- {
- Data = data;
- }
- }
- class DoublyLinkedList<T> : IEnumerable<T>
- {
- DoublyNode<T> head;
- DoublyNode<T> tail;
- int count;
- public void Add(T data)
- {
- DoublyNode<T> node = new DoublyNode<T>(data);
- if (head == null)
- {
- head = node;
- }
- else
- {
- node.Previous = tail;
- tail.Next = node;
- }
- tail = node;
- count++;
- }
- public void AddFirst(T data)
- {
- DoublyNode<T> node = new DoublyNode<T>(data);
- if (tail == null)
- {
- tail = node;
- }
- else
- {
- node.Next = head;
- head.Previous = node;
- }
- head = node;
- count++;
- }
- public bool Remove(T data)
- {
- bool flag = false;
- DoublyNode<T> current = head;
- while (current != null)
- {
- if (current.Data.Equals(data))
- {
- break;
- }
- current = current.Next;
- }
- if (current != null)
- {
- current.Next.Previous = current.Previous;
- current.Previous.Next = current.Next;
- }
- return flag;
- }
- public IEnumerable<T> BackEnumerator()
- {
- DoublyNode<T> current = tail;
- while (current != null)
- {
- yield return current.Data;
- current = current.Previous;
- }
- }
- public IEnumerator<T> GetEnumerator()
- {
- DoublyNode<T> current = head;
- while (current != null)
- {
- yield return current.Data;
- current = current.Next;
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- class Numbers
- {
- public IEnumerator GetEnumerator()
- {
- for (int i = 0; i < 6; i++)
- {
- if (i == 3)
- {
- yield break;
- }
- yield return i * i;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement