背景

前两篇文章介绍了 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. 查看本地安装的sql server是什么版本

    方法一:查询语句 SELECT @@VERSION返回当前安装的日期.版本和处理器类型.例如:结果为: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 ( ...

  2. PHP随机浮点数

    function randomFloat($min = 0, $max = 1) { $rand = mt_rand(); $lmax = mt_getrandmax(); return $min + ...

  3. 【LOJ】#2562. 「SDOI2018」战略游戏

    题解 圆方树建好之后点是原来的两倍,而st表求lca也要开到点的两倍,所以是四倍 我并没有开小,然而= =,我的预处理log2,写成了200000,而不是400000 我是不是折翼啊= = 很可写,我 ...

  4. 【AtCoder】ARC095 C-F题解

    我居然每道题都能想出来 虽然不是每道题都能写对,debug了很久/facepalm C - Many Medians 排序后前N/2个数的中位数时排序后第N/2 + 1的数 其余的中位数都是排序后第N ...

  5. 【AtCoder】AGC023 A-F题解

    可以说是第一场AGC了,做了三道题之后还有30min,杠了一下D题发现杠不出来,三题滚粗了 rating起步1300+,感觉还是很菜... 只有三题水平显然以后还会疯狂--啊(CF的惨痛经历) 改题的 ...

  6. 用 Java 实现一个冒泡排序算法

    冒泡排序(BubbleSort)的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面.即首先比较第1个和第2个数,将小数放前,大数放后.然后比较第2个数和第3个数,将小数放前,大数放后,如 ...

  7. Codeforces Round #323 (Div. 2) E - Superior Periodic Subarrays

    E - Superior Periodic Subarrays 好难的一题啊... 这个博客讲的很好,搬运一下. https://blog.csdn.net/thy_asdf/article/deta ...

  8. logstash部署及基本语法(二)

    一.logstash介绍 Logstash是一个开源的数据收集引擎,可以水平伸缩,而且logstash是整个ELK当中拥有最多插件的一个组件,其可以接收来自不同源的数据并统一输入到指定的且可以是不同目 ...

  9. Bootstrap--响应式表格布局

    <div class="row"> <div class="col-sm-2 col-md-2" style="min-height ...

  10. maven设计思想

    20171108 maven设计思想? archetype  插件  学习插件.