Advertisement
Guest User

Untitled

a guest
Feb 14th, 2021
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace CustomLinkedList
  6. {
  7.     class CustomLinkedList<T>
  8.     {
  9.         public Node<T> Head { get; set; }
  10.         public Node<T> Tail { get; set; }
  11.         public int Count { get; set; }
  12.  
  13.         public void AddFirst(T node)
  14.         {
  15.             Node<T> newHead = new Node<T>(node);
  16.             if (this.Count == 0)
  17.             {
  18.                 this.Head = this.Tail = newHead;
  19.                 this.Count++;
  20.                 return;
  21.             }
  22.             newHead.Next = Head;
  23.             this.Head.Previous = newHead;
  24.             this.Head = newHead;
  25.             this.Count++;
  26.         }
  27.  
  28.         public void AddLast(T node)
  29.         {
  30.             Node<T> newTail = new Node<T>(node);
  31.             if (this.Count == 0)
  32.             {
  33.                 this.Head = this.Tail = newTail;
  34.                 this.Count++;
  35.                 return;
  36.             }
  37.             newTail.Previous = this.Tail;
  38.             this.Tail.Next = newTail;
  39.             this.Tail = newTail;
  40.             this.Count++;
  41.         }
  42.  
  43.         public T RemoveFirst()
  44.         {
  45.             if (this.Count == 0)
  46.             {
  47.                 throw new NullReferenceException("The collection is empty!");
  48.             }
  49.  
  50.             T value = this.Head.Value;
  51.             this.Head = this.Head.Next;
  52.             if (this.Head != null)
  53.             {
  54.                 this.Head.Previous = null;
  55.             }
  56.             else
  57.             {
  58.                 this.Tail = null;
  59.             }
  60.             this.Count--;
  61.             return value;
  62.         }
  63.  
  64.         public T RemoveLast()
  65.         {
  66.             if (this.Count == 0)
  67.             {
  68.                 throw new NullReferenceException("The collection is empty!");
  69.             }
  70.  
  71.             T value = this.Tail.Value;
  72.             this.Tail = this.Tail.Previous;
  73.             if (this.Tail != null)
  74.             {
  75.                 this.Tail.Next = null;
  76.             }
  77.             else
  78.             {
  79.                 this.Head = null;
  80.             }
  81.             this.Count--;
  82.             return value;
  83.         }
  84.  
  85.         public void ForEach(Action<T> action)
  86.         {
  87.             var currentNode = this.Head;
  88.             while (currentNode != null)
  89.             {
  90.                 action(currentNode.Value);
  91.                 currentNode = currentNode.Next;
  92.             }
  93.         }
  94.  
  95.         public T[] ToArray()
  96.         {
  97.             T[] array = new T[this.Count];
  98.             var currentNode = this.Head;
  99.             int count = 0;
  100.  
  101.             while (currentNode != null)
  102.             {
  103.                 array[count] = currentNode.Value;
  104.                 currentNode = currentNode.Next;
  105.                 count++;
  106.             }
  107.             return array;
  108.         }
  109.     }
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement