Advertisement
Savonarolla

Untitled

Dec 25th, 2020
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace Generics
  6. {
  7.     public class Node<T>
  8.     {
  9.         public Node(T data)
  10.         {
  11.             Data = data;
  12.         }
  13.         public T Data { get; set; }
  14.         public Node<T> Previous { get; set; }
  15.         public Node<T> Next { get; set; }
  16.     }
  17.     public class DoublyLinkedList<T> : IEnumerable<T>  
  18.     {
  19.         Node<T> head;
  20.         Node<T> tail;
  21.         int count;  
  22.  
  23.        
  24.         public void Add (T data)
  25.         {
  26.             Node<T> node = new Node<T>(data);
  27.  
  28.             if (head == null)
  29.                 head = node;
  30.             else
  31.             {
  32.                 tail.Next = node;
  33.                 node.Previous = tail;
  34.             }
  35.             tail = node;
  36.             count++;
  37.         }
  38.         public void AddFirst(T data)
  39.         {
  40.             Node<T> node = new Node<T>(data);
  41.             Node<T> temp = head;
  42.             node.Next = temp;
  43.             head = node;
  44.             if (count == 0)
  45.                 tail = head;
  46.             else
  47.                 temp.Previous = node;
  48.             count++;
  49.         }
  50.        
  51.         public bool Remove(T data)
  52.         {
  53.             Node<T> current = head;
  54.             while (current != null)
  55.             {
  56.                 if (current.Data.Equals(data))
  57.                 {
  58.                     break;
  59.                 }
  60.                 current = current.Next;
  61.             }
  62.             if(current!=null)
  63.             {
  64.                 if(current.Next!=null)
  65.                 {
  66.                     current.Next.Previous = current.Previous;
  67.                 }
  68.                 else
  69.                 {
  70.                     tail = current.Previous;
  71.                 }
  72.                 if(current.Previous!=null)
  73.                 {
  74.                     current.Previous.Next = current.Next;
  75.                 }
  76.                 else
  77.                 {
  78.                     head = current.Next;
  79.                 }
  80.                 count--;
  81.                 return true;
  82.             }
  83.             return false;
  84.         }
  85.  
  86.         public int Count { get { return count; } }
  87.         public bool IsEmpty { get { return count == 0; } }
  88.  
  89.         public void Clear()
  90.         {
  91.             head = null;
  92.             tail = null;
  93.             count = 0;
  94.         }
  95.         public bool Contains(T data)
  96.         {
  97.             Node<T> current = head;
  98.             while (current != null)
  99.             {
  100.                 if (current.Data.Equals(data))
  101.                     return true;
  102.                 current = current.Next;
  103.             }
  104.             return false;
  105.         }
  106.          
  107.         IEnumerator IEnumerable.GetEnumerator()
  108.         {
  109.             return ((IEnumerable)this).GetEnumerator();
  110.         }
  111.  
  112.         IEnumerator<T> IEnumerable<T>.GetEnumerator()
  113.         {
  114.             Node<T> current = head;
  115.             while (current != null)
  116.             {
  117.                 yield return current.Data;
  118.                 current = current.Next;
  119.             }
  120.         }
  121.  
  122.         public IEnumerable<T> BackEnumerator()
  123.         {
  124.             Node<T> current = tail;
  125.             while (current != null)
  126.             {
  127.                 yield return current.Data;
  128.                 current = current.Previous;
  129.             }
  130.         }
  131.        
  132.         public T[] ShowList()
  133.         {
  134.             if (count == 0)
  135.             {
  136.                 throw new Exception("The List is empty");
  137.             }
  138.             else
  139.             {
  140.                 Node<T>[] result = new Node<T>[count];
  141.                 Node<T> node = head;
  142.                 for (int i = 0; i < count; ++i) {  
  143.                         result[i] = node;
  144.                         node = node.Next;
  145.                 }
  146.                 return result;
  147.             }
  148.         }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement