背景

前两篇文章介绍了 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. 大家来探讨下,IRepository 应该怎么定义?

    ORM已EF为例子:我见的最多的是泛型的IRepository, public partial interface IRepository<T> where T : BaseEntity{ ...

  2. Android Studio编译慢、卡死和狂占内存怎么破?

    https://www.zhihu.com/question/27953288 作者:知乎用户链接:https://www.zhihu.com/question/27953288/answer/118 ...

  3. 何时调用getView?——从源码的角度给出解答

    先来看ListView类中的makeAndAddView方法: 没有数据变化:从mRecycler中取得可视的view 数据有变化:obtainView /** * 获取视图填充到列表的item中去, ...

  4. Kafka(一)Kafka的简介与架构

    一.简介 1.1 概述 Kafka是最初由Linkedin公司开发,是一个分布式.分区的.多副本的.多订阅者,基于zookeeper协调的分布式日志系统(也可以当做MQ系统),常见可以用于web/ng ...

  5. C语言:九九乘法表打印

    题目: 要求:用“,”分隔算式,用“:”做一行的结尾. 另外1*1=1:这个算式是程序的第一行,前面没有空行. 文字版如下: 输入格式: 无 输出格式: 1*1=1; 2*1=2,2*2=4; 3*1 ...

  6. Jersey入门一:从Maven Archetype创建jersey项目

    1.用Ctrl+空格调出Spotlight搜索,输入ter调出终端窗口  2.在终端窗口进入将创建jersey项目的目录:  3.输入如下命令,创建一个名为的simple-service项目: m ...

  7. A - Superset CodeForces - 97B(人生第一个分治法,感觉,像二分啊。。)

    /* 分治法,第一次做不是很懂,借鉴了神犇代码,但实操之后感觉像二分,,可能做得少了或者就是.... */ 题目大意: 一个集合里有若干点,要求你添加某些点后保证这个集合里的任意两点满足以下三个条件中 ...

  8. 《Android源码设计模式》--享元模式

    No1: 享元模式是对象池的一种实现.享元模式用来尽可能减少内存使用量,它适合用于可能存在大量重复对象的场景,来缓存可共享的对象,达到对象共享.避免创建过多对象的效果,这样一来就可以提升性能.避免内存 ...

  9. Block的基本用法

    NSString* (^myBlock)(NSString*, int); myBlock = ^(NSString *name, int age){ return [NSString stringW ...

  10. java设计模式(六)策略模式

    适用于同一操作的不同行为,策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们可以相互替换,让算法独立于使用它的客户而独立变化,具体应用场景如第三方支付对接不同银行的算法. 要点:1)抽象策 ...