初学者,用c#怎样实现队列
答案:1 悬赏:50 手机版
解决时间 2021-02-01 00:45
- 提问者网友:锁深秋
- 2021-01-31 12:21
初学者,用c#怎样实现队列
最佳答案
- 五星知识达人网友:洎扰庸人
- 2021-01-31 14:00
/// <summary>
/// 环形队列
/// </summary>
public class MyQueue<T>
{
private T[] _array; // 队列容器
private int _head; // 队头指针
private int _tail; // 队尾指针
private int _size; // 容器大小
private const int increment = 5; // 队列增量
static T[] _emptyArray = new T[0];
public MyQueue()
{
_array = _emptyArray;
}
public MyQueue(int capacity)
{
if (capacity < 0) throw new ArgumentOutOfRangeException("capacity");
_array = new T[capacity];
_head = 0;
_tail = 0;
_size = 0;
}
/// <summary>
/// 队列元素数目
/// </summary>
public int Count { get { return _size; } }
/// <summary>
/// 入队
/// </summary>
/// <param name="item">队列元素</param>
public void Enqueue(T item)
{
if (_size == _array.Length)
{
SetCapacity(_size + increment);
}
_array[_tail] = item;
_tail = (_tail + 1) % _array.Length;
_size++;
}
/// <summary>
/// 出队
/// </summary>
/// <returns></returns>
public T Dequeue()
{
if (_size == 0) throw new InvalidOperationException("空队列");
T removed = _array[_head];
_array[_head] = default(T);
_head = (_head + 1) % _array.Length;
_size--;
return removed;
}
/// <summary>
/// 动态调整队列大小
/// </summary>
/// <param name="capacity"></param>
private void SetCapacity(int capacity)
{
T[] newarray = new T[capacity];
if (_size > 0)
{
if (_head < _tail) {
Array.Copy(_array, _head, newarray, 0, _size);
}else{
Array.Copy(_array, _head, newarray, 0, _array.Length - _head);
Array.Copy(_array, 0, newarray, _array.Length - _head, _tail);
}
}
_array = newarray;
_head = 0;
_tail = (_size == capacity) ? 0 : _size;
}
}
class Program
{
static void Main(string[] args)
{
MyQueue<int> mq = new MyQueue<int>();
mq.Enqueue(1);
mq.Enqueue(2);
mq.Enqueue(3);
mq.Enqueue(4);
Console.ReadKey(true);
}
}
/// 环形队列
/// </summary>
public class MyQueue<T>
{
private T[] _array; // 队列容器
private int _head; // 队头指针
private int _tail; // 队尾指针
private int _size; // 容器大小
private const int increment = 5; // 队列增量
static T[] _emptyArray = new T[0];
public MyQueue()
{
_array = _emptyArray;
}
public MyQueue(int capacity)
{
if (capacity < 0) throw new ArgumentOutOfRangeException("capacity");
_array = new T[capacity];
_head = 0;
_tail = 0;
_size = 0;
}
/// <summary>
/// 队列元素数目
/// </summary>
public int Count { get { return _size; } }
/// <summary>
/// 入队
/// </summary>
/// <param name="item">队列元素</param>
public void Enqueue(T item)
{
if (_size == _array.Length)
{
SetCapacity(_size + increment);
}
_array[_tail] = item;
_tail = (_tail + 1) % _array.Length;
_size++;
}
/// <summary>
/// 出队
/// </summary>
/// <returns></returns>
public T Dequeue()
{
if (_size == 0) throw new InvalidOperationException("空队列");
T removed = _array[_head];
_array[_head] = default(T);
_head = (_head + 1) % _array.Length;
_size--;
return removed;
}
/// <summary>
/// 动态调整队列大小
/// </summary>
/// <param name="capacity"></param>
private void SetCapacity(int capacity)
{
T[] newarray = new T[capacity];
if (_size > 0)
{
if (_head < _tail) {
Array.Copy(_array, _head, newarray, 0, _size);
}else{
Array.Copy(_array, _head, newarray, 0, _array.Length - _head);
Array.Copy(_array, 0, newarray, _array.Length - _head, _tail);
}
}
_array = newarray;
_head = 0;
_tail = (_size == capacity) ? 0 : _size;
}
}
class Program
{
static void Main(string[] args)
{
MyQueue<int> mq = new MyQueue<int>();
mq.Enqueue(1);
mq.Enqueue(2);
mq.Enqueue(3);
mq.Enqueue(4);
Console.ReadKey(true);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯