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. JS实现系统时间(自动)

    转自:https://blog.csdn.net/qq_35607510/article/details/54600563

  2. Linux学习—退出vi编辑模式

    转载自:http://blog.csdn.net/u010648555/article/details/50676647 初学Linux的时候,在使用vi 操作时候,有时候可能进入的是一个文件夹,这样 ...

  3. vector学习

    #include <iostream> #include <vector> #include <string.h> #include <algorithm&g ...

  4. asp.net MVC 单选按钮的使用

    单选按钮的标准的html 语法 <form><input type="radio" name="sex" value="male&q ...

  5. E20170405-gg

    repository n. 存储库,知识库 Permanent adj 永久的 host      n 主机,住处人,主人 authenticate v 使生效,验证 shell n 外壳 commi ...

  6. (十)SpringBoot的文件上传

    一:添加commons-fileupload依赖 打开pom文件添加 <dependency> <groupId>commons-fileupload</groupId& ...

  7. 突然所有命令都不能用了, /usr/bin不在path里 .bashrc文件

    export PATH=/usr/bin:/bin .bashrc文件中多一个空格都不行啊所有的问题都是因为复制的时候多了空格,简直无语,用了两个小时

  8. vs2010中的ADO控件及绑定控件

    要在项目中添加某一个ActiveX控件,则该ActiveX控件必须要注册.由于VS2010中,并没有自动注册ADO及ADO数据绑定控件(Microsoft ADO Data Control,Micro ...

  9. 跟我一起玩Win32开发(15):ListView控件

    这个控件其实不用阿拉来介绍,因为它太常见了,就好像我们一出门就会看到妹子一样常见.当然也可以说,它是对ListBox的扩充. 在使用该控件之前,我先介绍VS的一个相当好玩的功能. 在代码文件的#inc ...

  10. 洛谷 P1892 团伙

    P1892 团伙 并查集 #include<cstdio> int fa[2500];//fa[i]表示i的朋友所在集合,fa[i+n]表示i的敌人所在集合 bool boo[2500]; ...