Advertisement
Pearlfromsu

gda

Feb 15th, 2024
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ClassLibrary1
  8. {
  9.     public class OwnQueue<T>
  10.     {
  11.         private T[] mass = new T[4];
  12.         int _count = 0, _capacity = 4;
  13.         int _head = 0, _tail = 0;
  14.         public int Count
  15.         {
  16.             get
  17.             {
  18.                 return _count;
  19.             }
  20.             set
  21.             {
  22.                 if (value > _capacity)
  23.                 {
  24.                     while(_capacity < value)
  25.                         _capacity *= 2;
  26.                     T[] newArr = new T[_capacity];
  27.                     int j = 0;
  28.                     if (_head <= _tail)
  29.                         for (int i = _head; i <= _tail; i++)
  30.                             newArr[j++] = mass[i];
  31.                     if (_head > _tail)
  32.                     {
  33.                         for (int i = _head; i < mass.Length; i++)
  34.                             newArr[j++] = mass[i];
  35.                         for (int i = 0; i <= _tail; i++)
  36.                             newArr[j++] = mass[i];
  37.                     }
  38.                     mass = newArr;
  39.                     _head = 0;
  40.                     _tail = _count - 1;
  41.                 }
  42.                 _count = value;
  43.             }
  44.         }
  45.         public void Enqueue(T elem)
  46.         {
  47.             this.Count++;
  48.             if (this.Count > 1)
  49.             {
  50.                 if (_tail + 1 >= _capacity)
  51.                     _tail = 0;
  52.                 else
  53.                     _tail++;
  54.             }
  55.             mass[_tail] = elem;
  56.             Console.WriteLine($" to {_tail} ({_capacity} and {_count}) ({_head} and {_tail})");
  57.         }
  58.         public T Dequeue()
  59.         {
  60.             if(this.Count <= 0)
  61.                 throw new ArgumentNullException("Aboba");
  62.             this.Count--;
  63.             T firstElement = mass[_head];
  64.             Console.WriteLine($" from {_head} ({_capacity} and {_count}) ({_head} and {_tail})");
  65.             if (this.Count != 0)
  66.             {
  67.                 if (_head + 1 >= _capacity)
  68.                     _head = 0;
  69.                 else
  70.                     _head++;
  71.             }
  72.  
  73.             return firstElement;
  74.         }
  75.  
  76.         public OwnQueue()
  77.         {
  78.             this._count = 0;
  79.             this._capacity = 4;
  80.         }
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement