Advertisement
m1okgoodyes

LinkedList

Mar 31st, 2022
1,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 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.             LinkedList<string> linkedlist = new LinkedList<string>();
  15.             linkedlist.Add("tom");
  16.             linkedlist.Add("sam");
  17.             linkedlist.Add("max");
  18.             foreach(string item in linkedlist)
  19.             {
  20.                 Console.WriteLine(item);
  21.             }
  22.             linkedlist.Remove("tom");
  23.             foreach (string item in linkedlist)
  24.             {
  25.                 Console.WriteLine(item);
  26.             }
  27.             linkedlist.AddFirst("kirill");
  28.             foreach (string item in linkedlist)
  29.             {
  30.                 Console.WriteLine(item);
  31.             }
  32.         }
  33.         public class Node<T>//создание узлов
  34.         {
  35.             public Node(T data)
  36.             {
  37.                 Data = data;    
  38.             }
  39.             public T Data { get; set; }
  40.             public Node<T> Next { get; set; }
  41.         }
  42.         public class LinkedList<T> : IEnumerable<T>
  43.         {
  44.             Node<T> head;
  45.             Node<T> tail;
  46.             int count;
  47.             public void Add(T data)//добавление
  48.             {
  49.                 Node<T> node = new Node<T>(data);
  50.  
  51.                 if (head == null)//проверка пуст ли список
  52.                 {
  53.                     head = node;//устанавливаем начало
  54.                 }
  55.                 else
  56.                 {
  57.                     tail.Next = node;//ссылка на созданный элемент
  58.                 }
  59.                 tail = node;
  60.  
  61.                 count++;
  62.             }
  63.             public void AddFirst(T data)//добавление в начало
  64.             {
  65.                 Node<T> node = new Node<T>(data);//создание нового нода
  66.                 node.Next = head;//ссылка на существующий хэд
  67.                 head = node;//переопределение хэда на только чтро созданный
  68.                 if(count == 0)
  69.                 {
  70.                     tail = head;//если нет больше нодов чтобы ссылки хэд тейл были на единтсвенном
  71.                 }
  72.                 count++;
  73.             }
  74.  
  75.             IEnumerator IEnumerable.GetEnumerator()//реализация интерфейса
  76.             {
  77.                 return ((IEnumerable)this).GetEnumerator();
  78.             }
  79.             IEnumerator<T> IEnumerable<T>.GetEnumerator()
  80.             {
  81.                 Node<T> current = head;
  82.                 while(current != null)
  83.                 {
  84.                     yield return current.Data;
  85.                     current = current.Next;
  86.                 }
  87.             }
  88.             public bool Remove(T data)//удаление объекта
  89.             {
  90.                 Node<T> current = head;
  91.                 Node<T> previous = null;
  92.  
  93.                 while (current != null)
  94.                 {
  95.                     if(current.Data.Equals(data))//проверка на ввод данных
  96.                     {
  97.                         if (previous != null)//если узел в середине или в конце
  98.                         {
  99.                             previous.Next = current.Next;//приравнивает ссылка ссылку проскакивая элемент
  100.                                 if (current.Next == null)//если каррент последний
  101.                                 {
  102.                                     tail = previous;
  103.                                 }    
  104.                         }
  105.                         else
  106.                         {
  107.                             head = head.Next;//если узел в начале
  108.  
  109.                             if(head == null)
  110.                             {
  111.                                 tail = null;
  112.                             }
  113.                         }
  114.                         count--;
  115.                         return true;
  116.                     }
  117.                     previous = current;
  118.                     current = current.Next;
  119.                 }
  120.                 return false;
  121.             }
  122.  
  123.            
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement