package my_basic.class_3;

 /**
* 用数组结构实现大小固定的队列和栈
*/
public class Code_01_Array_stack_queue { public static class ArrayStack{
private Integer[] arr;
private Integer size; public ArrayStack(int initSize) {
if (initSize < 0) {
throw new IllegalArgumentException("The init size is less than 0");
}
arr = new Integer[initSize];
size=0; } public Integer peek() {
if (size == 0) {
return null;
}
return arr[size-1]; }
public void push(int num) {
if (size == arr.length) {
throw new ArrayIndexOutOfBoundsException("the stack is full");
}
arr[size++] = num;
}
public Integer pop() {
if (size == 0) {
throw new ArrayIndexOutOfBoundsException("the stack is empty");
}
size--;
return arr[size];
} } public static class ArrayQueue{
private Integer[] arr;
private int size;
private int first;
private int last; public ArrayQueue(int initSize) {
if (initSize < 0 ) {
throw new IllegalArgumentException("The init size is less than 0");
}
arr = new Integer[initSize];
size=0;
first=0;
last=0;
}
public Integer peek() {
if (size == 0) {
return null;
}
return arr[first];
}
public void push(int obj) {
if (size == arr.length) {
throw new ArrayIndexOutOfBoundsException("the queue is full");
}
size++;
arr[last] = obj;
last = (last == arr.length-1) ? 0 : last++;
}
public Integer poll() {
if (size == 0) {
throw new ArrayIndexOutOfBoundsException("the queue is empty");
}
size--;
int res = first;
first = (first == arr.length-1)? 0 : first++;
return arr[res];
} } public static void main(String[] args) { } }

用固定长度的数组实现stack queue的更多相关文章

  1. Queue插入的时候报错:源数组长度不足。请检查 srcIndex 和长度以及数组的下限。

    异常问题记录: 本想自己手动实现一个日志记录功能.使用Queue队列集合来实现多线程的日志记录. 测试 一个线程写入数据Enqueue和一个线程读取数据Dequeue ,直接用的无休眠死循环. 终于抛 ...

  2. 数据结构设计 Stack Queue

    之前在简书上初步总结过几个有关栈和队列的数据结构设计的题目.http://www.jianshu.com/p/d43f93661631 1.线性数据结构 Array Stack Queue Hash ...

  3. programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation

    编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...

  4. 死磕以太坊源码分析之EVM固定长度数据类型表示

    死磕以太坊源码分析之EVM固定长度数据类型表示 配合以下代码进行阅读:https://github.com/blockchainGuide/ 写文不易,给个小关注,有什么问题可以指出,便于大家交流学习 ...

  5. System.ArgumentException: 目标数组的长度不够。请检查 destIndex 和长度以及数组的下限

    扫码支付接口将要上线,近几天在优化系统性能.昨天把日志Helper类的日志记录改成了使用Queue<T>对象来实现异步处理.做了单元测试,并模拟多线程来测试后,发现正常.今天将站点部署到准 ...

  6. PHP固定长度字符串

    /** * 获取固定长度随机字符串 * @param $n * @return string * @throws Exception */ function gf_rand_str($n) { if ...

  7. 小记:目标数组的长度不够。请检查 destIndex 和长度以及数组的下限。

    异常:System.ArgumentException: 目标数组的长度不够.请检查 destIndex 和长度以及数组的下限.(不好意思忘记截图了) 发生异常的代码如下: var list = ne ...

  8. STL容器适配器 stack, queue

    stack是一种后进先出(last in first out)的数据结构.它只有一个出口,如图所示.stack允许新增元素,删除元素,取得最顶端元素.但除了最顶端外,没有其他任何地方可以存储stack ...

  9. STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map

      list vector deque stack queue priority_queue set [unordered_set] map [unordered_map] multimap [uno ...

随机推荐

  1. 如何在XCode中更改iPhone或iPad模拟器类型

    如何在XCode中更改iPhone或iPad模拟器类型 参考方法一(永久,一旦设置后,每次运行指定的模拟器):(1)选择顶层菜单Project 中的 Set Active Executable(2)根 ...

  2. E20180427-hm

    创建: 2018/04/27 dissolve vi. 溶解; 融化,液化; 解散,散去; 分裂,分解;    vt. 使溶解; 使(固态物)溶解为液体,使液化; 使消失,使消逝,消除; 使终止; c ...

  3. HDU 6092:Rikka with Subset(dp)

    分析 很多个较小的数字可以随机组合成较大的数字,所以B数组从小到大开始遍历,除了空集,最小的那个存在的个数对应的数字必然是a数组中的数字. 每求出这一部分之后,更新后续的B序列. 分析完后,主要的难点 ...

  4. python 之 random 模块、 shutil 模块、shelve模块、 xml模块

    6.12 random 模块 print(random.random()) (0,1)----float 大于0且小于1之间的小数 print(random.randint(1,3)) [1,3] 大 ...

  5. 揭开Python科学计算的面纱

    春牛春杖.无限春风来海上.便与春工.染得桃红似肉红. 春幡春胜.一阵春风吹酒醒.不似天涯.卷起杨花似雪花. 标准的Python中用列表保存一组值,可以当做数组使用,但是由于其值类型任意,所以列表中保存 ...

  6. iOS UITableView设置tableHeaderView时发生约束错误 UIView-Encapsulated-Layout-Height UIView-Encapsulated-Layout-Width

    在将UITableView的tableHeaderView设置为我自己创建的View的时候, 当我为这个自定义View添加约束之后启动调试, 然后符号断点UIViewAlertForUnsatisfi ...

  7. Qt基本应用

    1 使用方式 在qt designer中直接设计图形界面,然后使用pyGUI转换成py文件. 可以发现,转换的文件为一个class.并不是一个完整的程序(运行时无法出现窗口).这个类名字是Ui_Mai ...

  8. Hdu 5446 Unknown Treasure (2015 ACM/ICPC Asia Regional Changchun Online Lucas定理 + 中国剩余定理)

    题目链接: Hdu 5446 Unknown Treasure 题目描述: 就是有n个苹果,要选出来m个,问有多少种选法?还有k个素数,p1,p2,p3,...pk,结果对lcm(p1,p2,p3.. ...

  9. 第03课 在VMwave 14.0 上配置企业级CentOS 6.6操作系统

    第一部分:配置虚拟硬件 1.1 启动VMware,选择文件-->新建虚拟机(Ctrl + N),创建一个虚拟机. (VMware的安装过程较为简单,可自行百度.) 1.2 此时,出现新建虚拟机向 ...

  10. 深入理解spark streaming

    spark streaming是建立在spark core之上的,也就说spark streaming任务最终执行还是依赖于RDD模型.在转化成最终的RDD模型执行前,spark streaming主 ...