Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ClassLibrary1
- {
- public class OwnQueue<T>
- {
- private T[] mass = new T[4];
- int _count = 0, _capacity = 4;
- int _head = 0, _tail = 0;
- public int Count
- {
- get
- {
- return _count;
- }
- set
- {
- if (value > _capacity)
- {
- while(_capacity < value)
- _capacity *= 2;
- T[] newArr = new T[_capacity];
- int j = 0;
- if (_head <= _tail)
- for (int i = _head; i <= _tail; i++)
- newArr[j++] = mass[i];
- if (_head > _tail)
- {
- for (int i = _head; i < mass.Length; i++)
- newArr[j++] = mass[i];
- for (int i = 0; i <= _tail; i++)
- newArr[j++] = mass[i];
- }
- mass = newArr;
- _head = 0;
- _tail = _count - 1;
- }
- _count = value;
- }
- }
- public void Enqueue(T elem)
- {
- this.Count++;
- if (this.Count > 1)
- {
- if (_tail + 1 >= _capacity)
- _tail = 0;
- else
- _tail++;
- }
- mass[_tail] = elem;
- Console.WriteLine($" to {_tail} ({_capacity} and {_count}) ({_head} and {_tail})");
- }
- public T Dequeue()
- {
- if(this.Count <= 0)
- throw new ArgumentNullException("Aboba");
- this.Count--;
- T firstElement = mass[_head];
- Console.WriteLine($" from {_head} ({_capacity} and {_count}) ({_head} and {_tail})");
- if (this.Count != 0)
- {
- if (_head + 1 >= _capacity)
- _head = 0;
- else
- _head++;
- }
- return firstElement;
- }
- public OwnQueue()
- {
- this._count = 0;
- this._capacity = 4;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement