Advertisement
Guest User

CustomLinkedList

a guest
Jun 24th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.59 KB | None | 0 0
  1. namespace p09._01.CustomLinkedList
  2. {
  3.     using System;
  4.     using System.Collections;
  5.     using System.Collections.Generic;
  6.  
  7.     public class DoublyLinkedList<T> : IEnumerable<T>
  8.     {
  9.         private class ListNode<T>
  10.         {
  11.             public ListNode(T value)
  12.             {
  13.                 this.Value = value;
  14.             }
  15.  
  16.             public T Value { get; }
  17.  
  18.             public ListNode<T> NextNode { get; set; }
  19.  
  20.             public ListNode<T> PreviousNode { get; set; }
  21.         }
  22.  
  23.         private ListNode<T> head;
  24.         private ListNode<T> tail;
  25.  
  26.         public int Count { get; private set; }
  27.  
  28.         public void AddFirst(T element)
  29.         {
  30.             ListNode<T> newHead = new ListNode<T>(element);
  31.  
  32.             if (this.Count == 0)
  33.             {
  34.                 this.head = this.tail = newHead;
  35.             }
  36.             else
  37.             {
  38.                 newHead.NextNode = this.head;
  39.                 this.head.PreviousNode = newHead;
  40.                 this.head = newHead;
  41.             }
  42.  
  43.             this.Count++;
  44.         }
  45.  
  46.         public void AddLast(T element)
  47.         {
  48.             ListNode<T> newTail = new ListNode<T>(element);
  49.  
  50.             if (this.Count == 0)
  51.             {
  52.                 this.head = this.tail = newTail;
  53.             }
  54.             else
  55.             {
  56.                 newTail.PreviousNode = this.tail;
  57.                 this.tail.NextNode = newTail;
  58.                 this.tail = newTail;
  59.             }
  60.  
  61.             this.Count++;
  62.         }
  63.  
  64.         public T RemoveFirst()
  65.         {
  66.             ValidateCount();
  67.  
  68.             T firstElement = this.head.Value;
  69.             this.head = this.head.NextNode;
  70.  
  71.             if (this.head != null)
  72.             {
  73.                 this.head.PreviousNode = null;
  74.             }
  75.             else
  76.             {
  77.                 this.tail = null;
  78.             }
  79.  
  80.             this.Count--;
  81.  
  82.             return firstElement;
  83.         }
  84.  
  85.         public T RemoveLast()
  86.         {
  87.             ValidateCount();
  88.  
  89.             T lastElement = this.tail.Value;
  90.             this.tail = this.tail.PreviousNode;
  91.  
  92.             if (this.tail != null)
  93.             {
  94.                 this.tail.NextNode = null;
  95.             }
  96.             else
  97.             {
  98.                 this.head = null;
  99.             }
  100.  
  101.             this.Count--;
  102.  
  103.             return lastElement;
  104.         }
  105.  
  106.         public T[] ToArray()
  107.         {
  108.             T[] array = new T[this.Count];
  109.             int counter = 0;
  110.  
  111.             ListNode<T> currentNode = this.head;
  112.  
  113.             while (currentNode != null)
  114.             {
  115.                 array[counter++] = currentNode.Value;
  116.  
  117.                 currentNode = currentNode.NextNode;
  118.             }
  119.  
  120.             return array;
  121.         }
  122.  
  123.         public void ForEach(Action<T> action)
  124.         {
  125.             ListNode<T> currentNode = this.head;
  126.  
  127.             while (currentNode != null)
  128.             {
  129.                 action(currentNode.Value);
  130.  
  131.                 currentNode = currentNode.NextNode;
  132.             }
  133.         }
  134.  
  135.         public List<T> ToList()
  136.         {
  137.             List<T> list = new List<T>();
  138.  
  139.             var currentNode = this.head;
  140.  
  141.             while (currentNode != null)
  142.             {
  143.                 list.Add(currentNode.Value);
  144.  
  145.                 currentNode = currentNode.NextNode;
  146.             }
  147.  
  148.             return list;
  149.         }
  150.  
  151.         public bool Contains(T value)
  152.         {
  153.             ListNode<T> currentNode = this.head;
  154.  
  155.             while (currentNode != null)
  156.             {
  157.                 if (currentNode.Value.Equals(value))
  158.                 {
  159.                     return true;
  160.                 }
  161.  
  162.                 currentNode = currentNode.NextNode;
  163.             }
  164.  
  165.             return false;
  166.         }
  167.  
  168.         private void ValidateCount()
  169.         {
  170.             if (this.Count == 0)
  171.             {
  172.                 throw new InvalidOperationException(
  173.                     "The list is empty");
  174.             }
  175.         }
  176.  
  177.         public IEnumerator<T> GetEnumerator()
  178.         {
  179.             ListNode<T> nodeHead = this.head;
  180.             ListNode<T> nodeTail = this.tail;
  181.  
  182.             while (nodeHead != null)
  183.             {
  184.                 yield return nodeHead.Value;
  185.  
  186.                 nodeHead = nodeHead.NextNode;
  187.             }
  188.  
  189.             while (nodeTail != null)
  190.             {
  191.                 yield return nodeTail.Value;
  192.  
  193.                 nodeTail = nodeTail.PreviousNode;
  194.             }
  195.         }
  196.  
  197.         IEnumerator IEnumerable.GetEnumerator()
  198.         {
  199.             return this.GetEnumerator();
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement