Advertisement
m1okgoodyes

Queue

Apr 2nd, 2022
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4.  
  5. namespace Queue
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Queue<string> queue = new Queue<string>();
  12.             queue.Enqueue("tom");
  13.             queue.Enqueue("sam");
  14.             queue.Enqueue("max");
  15.             queue.Enqueue("kirill");
  16.             foreach(string s in queue)
  17.             {
  18.                 Console.WriteLine(s);
  19.             }
  20.             Console.WriteLine();
  21.             string header = queue.First();
  22.             Console.WriteLine($"первый элемент: {header}");
  23.             Console.WriteLine();
  24.             string firstitem = queue.Dequeue();
  25.             Console.WriteLine($"извлеченный элемент: {firstitem}");
  26.             Console.WriteLine();
  27.  
  28.             header = queue.First();
  29.             Console.WriteLine($"первый элемент: {header}");
  30.             Console.WriteLine();
  31.             foreach(string s in queue)
  32.             {
  33.                 Console.WriteLine(s);
  34.             }
  35.         }
  36.         public class Node<T>
  37.         {
  38.             public Node(T data)
  39.             {
  40.                 Data = data;
  41.             }
  42.             public T Data { get; set; }
  43.             public Node<T> Next { get; set; }
  44.         }
  45.         public class Queue<T> : IEnumerable<T>
  46.         {
  47.             Node<T> head;
  48.             Node<T> tail;
  49.             int count;
  50.             //добавление в очередь
  51.             public void Enqueue(T data)
  52.             {
  53.                 Node<T> node = new Node<T>(data);
  54.                 Node<T> tempnode = tail;
  55.                 tail = node;
  56.                 if (count == 0)
  57.                 {
  58.                     head = tail;
  59.                 }
  60.                 else
  61.                 {
  62.                     tempnode.Next = tail;
  63.                    
  64.                 }
  65.                 count++;
  66.             }
  67.             //удаление из очереди
  68.             public T Dequeue()
  69.             {
  70.                 if(count == 0)
  71.                 {
  72.                     throw new InvalidOperationException("очередь пуста");
  73.                 }
  74.                 T output = head.Data;
  75.                 head = head.Next;
  76.                 count--;
  77.                 return output;
  78.             }
  79.             public T First
  80.             {
  81.                 get
  82.                 {
  83.                     if(IsEmpty)
  84.                     {
  85.                         throw new InvalidOperationException("очередь пуста");
  86.                     }
  87.                     return tail.Data;
  88.                 }
  89.             }
  90.             public int Count { get { return count; } }
  91.             public bool IsEmpty { get { return count == 0; } }
  92.             public void Clear()
  93.             {
  94.                 head = tail = null;
  95.                 count = 0;
  96.             }
  97.             public bool Contains(T data)
  98.             {
  99.                 Node<T> current = head;
  100.                 while (current != null)
  101.                 {
  102.                     if (current.Data.Equals(data))
  103.                     {
  104.                         return true;
  105.                     }
  106.                     current = current.Next;
  107.                 }
  108.                 return false;
  109.             }
  110.             IEnumerator IEnumerable.GetEnumerator()
  111.             {
  112.                 return ((IEnumerable)this).GetEnumerator();
  113.             }
  114.             IEnumerator<T> IEnumerable<T>.GetEnumerator()
  115.             {
  116.                 Node<T> current = head;
  117.                 while(current != null)
  118.                 {
  119.                     yield return current.Data;
  120.                     current = current.Next;
  121.                 }
  122.             }
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement