Advertisement
m1okgoodyes

DoublyList

Apr 1st, 2022
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6.  
  7. namespace tren
  8. {
  9.     public class Program
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             //двухсвязный список
  14.             DoubleLinkedlist<string> linkedlist = new DoubleLinkedlist<string>();
  15.             linkedlist.Add("tom");
  16.             linkedlist.Add("sam");
  17.             linkedlist.Add("kirill");
  18.             foreach(string s in linkedlist)
  19.             {
  20.                 Console.WriteLine(s);
  21.             }
  22.             foreach(string s in linkedlist)
  23.             {
  24.                 Console.WriteLine(s);
  25.             }
  26.             linkedlist.Remove("tom");
  27.             linkedlist.AddFirst("max");
  28.             foreach(string s in linkedlist)
  29.             {
  30.                 Console.WriteLine(s);
  31.             }
  32.             Console.WriteLine(linkedlist.Contains("max"));
  33.         }
  34.         public class DoublyNode<T>//Создание узлов
  35.         {
  36.             public DoublyNode(T data)
  37.             {
  38.                 Data = data;
  39.             }
  40.             public T Data { get; set; }
  41.             public DoublyNode<T> Previous { get; set; }
  42.             public DoublyNode<T> Next { get; set; }
  43.         }
  44.         public class DoubleLinkedlist<T>: IEnumerable<T>//двухсвязный список
  45.         {
  46.             DoublyNode<T> head;//головной(первый) элемент
  47.             DoublyNode<T> tail;//хвостовой(последний) элемент
  48.             int count;//счетчик
  49.  
  50.             public void Add(T data)
  51.             {
  52.                 DoublyNode<T> node = new DoublyNode<T>(data);
  53.                 if(head == null)
  54.                 {
  55.                     head = node;
  56.                 }
  57.                 else
  58.                 {
  59.                     tail.Next = node;
  60.                     node.Previous = tail;
  61.                 }
  62.                 tail = node;
  63.                 count++;
  64.             }
  65.             public void AddFirst(T data)
  66.             {
  67.                 DoublyNode<T> node = new DoublyNode<T>(data);
  68.                 if(head == null)
  69.                 {
  70.                     head = node;
  71.                     tail = node;
  72.                 }
  73.                 else
  74.                 {
  75.                     head.Previous = node;
  76.                     node.Next = head;
  77.                 }
  78.                 head = node;
  79.                 count++;
  80.             }
  81.             public bool Remove(T data)//удаление
  82.             {
  83.                 DoublyNode<T> current = head;
  84.                 while(current != null)//поиск удаляемого узла
  85.                 {
  86.                     if(current.Data.Equals(data))
  87.                     {
  88.                         break;
  89.                     }
  90.                     current = current.Next;
  91.                 }
  92.                 if(current != null)
  93.                 {
  94.                     if(current.Next != null)//если узел не последний
  95.                     {
  96.                         current.Next.Previous = current.Previous;//ссылка след элемента приравнивается с предыдущему элементу
  97.                     }
  98.                     else
  99.                     {
  100.                         tail = current.Previous;//если последний, переустанавливаем tail
  101.                     }
  102.  
  103.                     if (current.Previous != null)//если узел не первый
  104.                     {
  105.                         current.Previous.Next = current.Next;//ссылка пред элемента приравнивается к следующему элементу
  106.                     }
  107.                     else
  108.                     {
  109.                         head = current.Next;//если первый, переустанавливаем head
  110.                     }
  111.                     count--;
  112.                     return true;
  113.                 }
  114.                 return false;
  115.             }
  116.             public bool Contains(T data)//содержит ли искомое
  117.             {
  118.                 DoublyNode<T> current = head;
  119.                 while(current != null)
  120.                 {
  121.                     if(current.Data.Equals(data))
  122.                     {
  123.                         return true;
  124.                     }
  125.                     current=current.Next;
  126.                 }
  127.                 return false;
  128.             }
  129.             IEnumerator IEnumerable.GetEnumerator()
  130.             {
  131.                 return ((IEnumerable)this).GetEnumerator();
  132.  
  133.             }
  134.             IEnumerator<T> IEnumerable<T>.GetEnumerator()
  135.             {
  136.                 DoublyNode<T> current = head;
  137.                 while (current != null)
  138.                 {
  139.                     yield return current.Data;
  140.                     current = current.Next;
  141.                 }
  142.             }
  143.         }
  144.     }
  145.        
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement