Advertisement
Iv555

Untitled

Jan 31st, 2022
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4.  
  5.  
  6. namespace CustomDoublyLinkedList
  7. {
  8.     public class DoublyLinkedList<T>
  9.     {
  10.         private ListNode<T> head;
  11.         private ListNode<T> tail;
  12.         public int Count { get; private set; }
  13.  
  14.         public void AddFirst(T element)
  15.         {
  16.             if (this.Count == 0)
  17.             {
  18.                 head = tail = new ListNode<T>(element);
  19.             }
  20.             else
  21.             {
  22.                 ListNode<T> newHead = new ListNode<T>(element);
  23.                 newHead.NextNode = head;
  24.                 head.PreviosNode = newHead;
  25.                 head = newHead;
  26.             }
  27.  
  28.             Count++;
  29.         }
  30.  
  31.         public void AddLast(T element)
  32.         {
  33.             if (Count == 0)
  34.             {
  35.                 head = tail = new ListNode<T>(element);
  36.             }
  37.             else
  38.             {
  39.                 ListNode<T> newTail = new ListNode<T>(element);
  40.                 tail.NextNode = newTail;
  41.                 newTail.PreviosNode = tail;
  42.                 tail = newTail;
  43.             }
  44.  
  45.             Count++;
  46.         }
  47.  
  48.         public T RemoveFirst()
  49.         {
  50.             if (Count == 0)
  51.             {
  52.                 throw new InvalidOperationException("The list is empty");
  53.             }
  54.  
  55.             T firstElement = head.Value;
  56.             head = head.NextNode;
  57.             if (head != null)
  58.             {
  59.                 head.PreviosNode = null;
  60.             }
  61.             else
  62.             {
  63.                 tail = null;
  64.             }
  65.  
  66.             Count--;
  67.             return firstElement;
  68.         }
  69.  
  70.         public T RemoveLast()
  71.         {
  72.             if (Count == 0)
  73.             {
  74.                 throw new InvalidOperationException("The list is empty");
  75.             }
  76.  
  77.             T lastElement = tail.Value;
  78.             tail = tail.PreviosNode;
  79.             if (tail != null)
  80.             {
  81.                 tail.NextNode = null;
  82.             }
  83.             else
  84.             {
  85.                 head = null;
  86.             }
  87.  
  88.             Count--;
  89.             return lastElement;
  90.         }
  91.  
  92.         public void ForEach(Action<T> action)
  93.         {
  94.             ListNode<T> currentNode = head;
  95.             while (currentNode != null)
  96.             {
  97.                 action(currentNode.Value);
  98.                 currentNode = currentNode.NextNode;
  99.             }
  100.         }
  101.  
  102.         public Action<T> action()
  103.         {
  104.             return x => Console.Write(x + " ");
  105.         }
  106.  
  107.         public T[] ToArray()
  108.         {
  109.             T[] array = new T[Count];
  110.             int counter = 0;
  111.             ListNode<T> currentNode = head;
  112.             while (currentNode != null)
  113.             {
  114.                 array[counter] = currentNode.Value;
  115.                 currentNode = currentNode.NextNode;
  116.                 counter++;
  117.             }
  118.  
  119.             return array;
  120.         }
  121.     }
  122.  
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement