C# 数据结构基础-实现循环队列
来源:互联网 发布:淘宝p图软件 2016 编辑:程序博客网 时间:2024/06/04 00:47
队列 队列的概念是先进先出,这个应该不用多说了。看下面那个从网上找的现成图片。
循环队列 循环队列在逻辑上将队列中的数据摆成环形,如下图:
下面直接上代码。
/// <summary> /// 循环队列 /// 2015年1月4日 /// </summary> /// <typeparam name="T"></typeparam> public class MyCircleQueue<T> { /// <summary> /// 队列数组 /// </summary> private T[] _queue; /// <summary> /// 队首索引 /// </summary> private int _front; /// <summary> /// 队尾索引 /// </summary> private int _rear; /// <summary> /// 队列的内存大小,但实际可用大小为_capacity-1 /// </summary> private int _capacity; public MyCircleQueue(int queueSize) { if (queueSize < 1) throw new IndexOutOfRangeException("传入的队列长度不能小于1。"); //设置队列容量 _capacity = queueSize; //创建队列数组 _queue = new T[queueSize]; //初始化队首和队尾索引 _front = _rear = 0; } /// <summary> /// 添加一个元素 /// </summary> /// <param name="item"></param> public void Push(T item) { //队列已满 if (GetNextRearIndex() == _front) { //扩大数组 T[] newQueue = new T[2 * _capacity]; if (newQueue == null) throw new ArgumentOutOfRangeException("数据容量过大,超出系统内存大小。"); //队列索引尚未回绕 if (_front == 0) { //将旧队列数组数据转移到新队列数组中 Array.Copy(_queue, newQueue, _capacity); } else { //如果队列回绕,刚需拷贝再次, //第一次将队首至旧队列数组最大长度的数据拷贝到新队列数组中 Array.Copy(_queue, _front, newQueue, _front, _capacity - _rear - 1); //第二次将旧队列数组起始位置至队尾的数据拷贝到新队列数组中 Array.Copy(_queue, 0, newQueue, _capacity, _rear + 1); //将队尾索引改为新队列数组的索引 _rear = _capacity + 1; } _queue = newQueue; _capacity *= 2; } //累加队尾索引,并添加当前项 _rear = GetNextRearIndex(); _queue[_rear] = item; } /// <summary> /// 获取队首元素 /// </summary> /// <returns></returns> public T FrontItem() { if (IsEmpty()) throw new ArgumentOutOfRangeException("队列为空。"); return _queue[GetNextFrontIndex()]; } /// <summary> /// 获取队尾元素 /// </summary> /// <returns></returns> public T RearItem() { if (IsEmpty()) throw new ArgumentOutOfRangeException("队列为空。"); return _queue[_rear]; } /// <summary> /// 弹出一个元素 /// </summary> /// <returns></returns> public T Pop() { if (IsEmpty()) throw new ArgumentOutOfRangeException("队列为空。"); _front = GetNextFrontIndex(); return _queue[_front]; } /// <summary> /// 队列是否为空 /// </summary> /// <returns></returns> public bool IsEmpty() { return _front == _rear; } /// <summary> /// 获取下一个索引 /// </summary> /// <returns></returns> private int GetNextRearIndex() { if (_rear + 1 == _capacity) { return 0; } return _rear + 1; } /// <summary> /// 获取下一个索引 /// </summary> /// <returns></returns> private int GetNextFrontIndex() { if (_front + 1 == _capacity) { return 0; } return _front + 1; } }
源码下载
0 0
- C# 数据结构基础-实现循环队列
- 【数据结构基础】循环队列
- 数据结构基础(7) --循环队列的设计与实现
- 数据结构基础(5)--C语言实现循环队列--静态
- 数据结构基础(7) --循环队列的设计与实现
- 数据结构基础8_循环队列(顺序实现)
- 数据结构基础(7) --循环队列的设计与实现
- 数据结构基础之循环队列C语言实现
- 数据结构基础——循环队列的C++实现
- 数据结构:循环队列--Java实现
- 数据结构循环队列Java实现
- 数据结构-循环数组实现队列
- 数据结构 -- 队列 & 循环队列 -- 数组实现
- C#数据结构回顾之循环队列
- C#实现循环顺序队列(队列)
- 【数据结构】队列-循环队列
- 【数据结构-队列】循环队列
- 数据结构(队列):循环队列
- (30 hackdays day 6) UserApp – 管管那帮用户
- AwesomePlayer的准备工作
- 云存储之网盘浅析
- jsoncpp 构造空数组
- Android 出现case expressions must be constant expressions
- C# 数据结构基础-实现循环队列
- python中os和os.path模块
- (30 hackdays day 8) Physical web - 给鞋子一个网址
- [LeetCode] Word Break II
- (30 hackdays day 9) Rekognition - 借我借我一双慧眼吧
- python解析xml并按照其结构输出
- Linux Nginx
- 取一个字符串的子串
- iOS 开发 - iOS Core Animation学习记录