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 CustomQueue
- {
- public class MyQueue <T>
- {
- private T[] _items;
- private int head, tail;
- public int Count { get; private set; }
- private int _capacity;
- public MyQueue()
- {
- _capacity = 4;
- _items = new T[_capacity];
- head = 0; tail = 0;
- Count = 0;
- }
- #region Methods
- /// In Process:
- /// Ready: Push(), Pop(), Peek(), Count, Contains();
- public void Push(T item)
- {
- if(Count == _capacity)
- {
- ExpandedCapacityQueue();
- }
- _items[tail % _capacity] = item;
- tail++; Count++;
- }
- public T Pop()
- {
- if(Count == 0)
- {
- throw new ArgumentException("NoSuchElementException");
- }
- else
- {
- T item = _items[head % _capacity];
- head++; Count--;
- return item;
- }
- }
- public T Peek()
- {
- if (Count == 0)
- {
- throw new ArgumentException("NoSuchElementException");
- }
- else
- {
- return _items[head % _capacity];
- }
- }
- public bool Contains(T item)
- {
- return _items.Contains(item);
- }
- private void ExpandedCapacityQueue()
- {
- T[] tempitems = _items;
- _items = new T[_capacity * 2];
- for (int i = 0; i < Count; i++)
- {
- _items[i] = tempitems[(head + i) % _capacity];
- }
- head = 0;
- _capacity = _capacity * 2;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment