背景

前两篇文章介绍了 Queue 的实现,很多类库都引入了 Deque,Deque 可以两头添加和删除,然后在 Deque 之上构建 Queue 和 Stack。

Deque

代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DataStuctureStudy.Deques
{
public class Deque<T>
{
private T[] _items = new T[];
private int _size = ;
private int _head = -;
private int _tail = -; private void AllocateNewArray()
{
int newLength = (_size == ) ? : _size * ; T[] newArray = new T[newLength]; if (_size > )
{
// copy contents...
// if the array has no wrapping, just copy the valid range
// else copy from head to end of the array and then from 0 to the tail // if tail is less than head we've wrapped
var newIndex = ;
if (_tail < _head)
{
// copy the _items[head].._items[end] -> newArray[0]..newArray[N]
for (int index = _head; index < _items.Length; index++)
{
newArray[newIndex] = _items[index];
newIndex++;
} // copy _items[0].._items[tail] -> newArray[N+1]..
for (int index = ; index <= _tail; index++)
{
newArray[newIndex] = _items[index];
newIndex++;
}
}
else
{
// copy the _items[head].._items[tail] -> newArray[0]..newArray[N]
for (int index = _head; index <= _tail; index++)
{
newArray[newIndex] = _items[index];
newIndex++;
}
}
_head = ;
_tail = _size - ; // compensate for the extra bump
}
else
{
// nothing in the array
_head = -;
_tail = -;
} _items = newArray;
} public void EnqueueFirst(T item)
{
// if the array needs to grow
if (_items.Length == _size)
{
AllocateNewArray();
} // since we know the array isn't full and _head is greater than 0
// we know the slot in front of head is open
if (_head > )
{
_head--;
}
else
{
// otherwise we need to wrap around to the end of the array
_head = _items.Length - ;
if (_tail == -)
{
_tail = _head;
}
}
_items[_head] = item;
_size++;
} public void EnqueueLast(T item)
{
// if the array needs to grow
if (_items.Length == _size)
{
AllocateNewArray();
} // now we have a properly sized array and can focus on wrapping issues.
// if _tail is at the end of the array we need to wrap around
if (_tail == _items.Length - )
{
_tail = ;
}
else
{
_tail++; if (_head == -)
{
_head = _tail;
}
}
_items[_tail] = item;
_size++;
} public T DequeueFirst()
{
if (_size == )
{
throw new InvalidOperationException("The deque is empty");
} T value = _items[_head]; if (_head == _items.Length - )
{
// if the head is at the last index in the array - wrap around.
_head = ;
}
else
{
// move to the next slot
_head++;
}
_size--;
return value;
} public T DequeueLast()
{
if (_size == )
{
throw new InvalidOperationException("The deque is empty");
} T value = _items[_tail]; if (_tail == )
{
// if the tai is at the first index in the array - wrap around.
_tail = _items.Length - ;
}
else
{
// move to the previous slot
_tail--;
}
_size--;
return value;
} public T PeekFirst()
{
if (_size == )
{
throw new InvalidOperationException("The deque is empty");
}
return _items[_head];
} public T PeekLast()
{
if (_size == )
{
throw new InvalidOperationException("The deque is empty");
}
return _items[_tail];
} public int Count
{
get
{
return _size;
}
}
}
}

算法:基于 RingBuffer 的 Deque 实现的更多相关文章

  1. Google Cardboard的九轴融合算法——基于李群的扩展卡尔曼滤波

    Google Cardboard的九轴融合算法 --基于李群的扩展卡尔曼滤波 极品巧克力 前言 九轴融合算法是指通过融合IMU中的加速度计(三轴).陀螺仪(三轴).磁场计(三轴),来获取物体姿态的方法 ...

  2. [python] A*算法基于栅格地图的全局路径规划

    # 所有节点的g值并没有初始化为无穷大 # 当两个子节点的f值一样时,程序选择最先搜索到的一个作为父节点加入closed # 对相同数值的不同对待,导致不同版本的A*算法找到等长的不同路径 # 最后c ...

  3. 简单易学的机器学习算法—基于密度的聚类算法DBSCAN

    简单易学的机器学习算法-基于密度的聚类算法DBSCAN 一.基于密度的聚类算法的概述 我想了解下基于密度的聚类算法,熟悉下基于密度的聚类算法与基于距离的聚类算法,如K-Means算法之间的区别.    ...

  4. 简单易学的机器学习算法——基于密度的聚类算法DBSCAN

    一.基于密度的聚类算法的概述     最近在Science上的一篇基于密度的聚类算法<Clustering by fast search and find of density peaks> ...

  5. 算法:基于 RingBuffer 的 Queue 实现

    背景 如果基于数组实现队列,常见的选择是采用 RingBuffer,否则就需要移动数组元素. RingBuffer 很容易看出 RingBuffer 的思想,这里就不赘述了. 您可以思考一个问题:图中 ...

  6. 算法:基于 RingBuffer 的 Queue 实现《续》

    背景 上篇实现了一个简单的队列,内部使用了 _count 计数,本文采用另外一种模式,不用 _count 计数. RingBuffer 不用 _count 计数的话,为了区分队列的满和空,需要在数组中 ...

  7. 【笔记6】用pandas实现条目数据格式的推荐算法 (基于物品的协同)

    ''' 基于物品的协同推荐 矩阵数据 说明: 1.修正的余弦相似度是一种基于模型的协同过滤算法.我们前面提过,这种算法的优势之 一是扩展性好,对于大数据量而言,运算速度快.占用内存少. 2.用户的评价 ...

  8. 【笔记5】用pandas实现矩阵数据格式的推荐算法 (基于物品的协同)

    ''' 基于物品的协同推荐 矩阵数据 说明: 1.修正的余弦相似度是一种基于模型的协同过滤算法.我们前面提过,这种算法的优势之 一是扩展性好,对于大数据量而言,运算速度快.占用内存少. 2.用户的评价 ...

  9. 【笔记3】用pandas实现矩阵数据格式的推荐算法 (基于用户的协同)

    原书作者使用字典dict实现推荐算法,并且惊叹于18行代码实现了向量的余弦夹角公式. 我用pandas实现相同的公式只要3行. 特别说明:本篇笔记是针对矩阵数据,下篇笔记是针对条目数据. ''' 基于 ...

随机推荐

  1. SqlServer性能优化 Sql语句优化(十四)

    一:在较小的结果集上上操作 1.仅返回需要的列 2.分页获取数据 EF实现分页: public object getcp(int skiprows,int currentpagerows) { HRU ...

  2. Registry私有仓库搭建及认证

    本节内容: Registry相关概念 Registry V1和V2 安装Docker 搭建本地registry v2 搭建外部可访问的Registry 添加认证 更高级的认证 registry web ...

  3. hdu 4349 求C(n,0),C(n,1),C(n,2)...C(n,n).当中有多少个奇数 (Lucas定理推广)

    Lucas定理:把n写成p进制a[n]a[n-1]a[n-2]...a[0],把m写成p进制b[n]b[n-1]b[n-2]...b[0],则C(n,m)与C(a[n],b[n])*C(a[n-1], ...

  4. 使用Let’s Encrypt创建nginx免费SSL证书

    资料参考: https://www.freehao123.com/top-8-free-ssl-cert/   八大免费SSL证书-给你的网站免费添加Https安全加密 https://www.fre ...

  5. ASP.NET Web API 2:Action的返回类型

    Web API控制器中的Action方法有如下几种返回类型: void HttpResponseMessage IHttpActionResult 其它类型 基于上面几种不同的返回类型,Web API ...

  6. WangSql 3.0源码共享(WangSql 1.0重大升级到3.0)

    WangSql 1.0博文阅读: http://www.cnblogs.com/deeround/p/6204610.html 基于1.0做了以下重大改动: 1.多数据实现方式调整 2.使用EmitM ...

  7. 【转】 LINUX中IPTABLES和TC对端口的带宽限制 端口限速

    不管是iptables还是tc(traffic control)功能都很强大,都是与网络相关的工具,那么我们就利用这两个工具来对端口进行带宽的限制. 1.使用命令ifconfig查看服务器上的网卡信息 ...

  8. 002 Jupyter-NoteBook工具介绍(网页版编辑器)

    1.Jupyter-NoteBook位置 在安装完anaconda后,这个工具已经被安装完成. 2.打开 3.功能讲解 目录:C:\Users\dell,这个可以看上面控制台上的信息. 4.其余的功能 ...

  9. RabbitMQ路由类型

    关于RabbitMQ的Exchange类型 参考地址:<RabbitMQ学习系列(四): 几种Exchange 模式> github地址:https://github.com/ChenWe ...

  10. 在centOS使用systemctl配置启动多个tomcat

    公司服务器使用的是阿里云CentOS7,CentOS7和CentOS6目前最大区别就是service变成了现在的systemctl,简单的查了一下并结合使用,发现systemctl功能上等同于6上面的 ...