背景

前两篇文章介绍了 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. php读取xml中cdata部分方法

    本例使用php的simplexml:XML(eventtrackdata.xml'): <eventdata> <event> <date>2012.05.11&l ...

  2. 【BZOJ】1152: [CTSC2006]歌唱王国Singleland

    题解 读错题了,是最后留下一个牛人首长歌颂他,和其他人没有关系,t就相当于数据组数 结论题,具体可看 https://www.zhihu.com/question/59895916/answer/19 ...

  3. Python数据分析之pandas

    Python中的pandas模块进行数据分析. 接下来pandas介绍中将学习到如下8块内容:1.数据结构简介:DataFrame和Series2.数据索引index3.利用pandas查询数据4.利 ...

  4. php中的PDO函数库详解

    PHP中的PDO函数库详解 PDO是一个“数据库访问抽象层”,作用是统一各种数据库的访问接口,与mysql和mysqli的函数库相比,PDO让跨数据库的使用更具有亲和力:与ADODB和MDB2相比,P ...

  5. thinkphp数据查询方法总结select ,find,getField,query

    thinkphp已经封装好了常用的查询方法,且都比较实用,对于不常用的查询框架也保留了原始查询方法query. 1 2 $Model = new Model() // 实例化一个model对象 没有对 ...

  6. 20169211《Linux内核原理与分析》第五周作业

    1.在自己的linux系统中搭建实验环境: 2.使用GDB调试内核跟踪启动过程: 3.分析start_kernel的代码. 1.在自己的linux系统中搭建实验环境 1.1 下载linux-3.18. ...

  7. ACM训练计划建议(转)

    ACM训练计划建议 From:freecode#  Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...

  8. 【基础知识】JavaScript基础

    [学习日记]JavaScript基础 1,一般写在<head></head>中(其实可以放到任意位置); 2,弹出对话框 <scripttype="text/j ...

  9. Xamarin 2017.11.1更新

     Xamarin 2017.11.1更新 本次更新主要解决了一些bug.Visual Studio 2017升级到15.4.2获得新功能.Visual Studio 2015需要工具-选项-Xamar ...

  10. 机器学习之路:python 字典特征提取器 DictVectorizer

    python3 学习使用api 将字典类型数据结构的样本,抽取特征,转化成向量形式 源码git: https://github.com/linyi0604/MachineLearning 代码: fr ...