用固定长度的数组实现stack queue
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的更多相关文章
- Queue插入的时候报错:源数组长度不足。请检查 srcIndex 和长度以及数组的下限。
异常问题记录: 本想自己手动实现一个日志记录功能.使用Queue队列集合来实现多线程的日志记录. 测试 一个线程写入数据Enqueue和一个线程读取数据Dequeue ,直接用的无休眠死循环. 终于抛 ...
- 数据结构设计 Stack Queue
之前在简书上初步总结过几个有关栈和队列的数据结构设计的题目.http://www.jianshu.com/p/d43f93661631 1.线性数据结构 Array Stack Queue Hash ...
- programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation
编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...
- 死磕以太坊源码分析之EVM固定长度数据类型表示
死磕以太坊源码分析之EVM固定长度数据类型表示 配合以下代码进行阅读:https://github.com/blockchainGuide/ 写文不易,给个小关注,有什么问题可以指出,便于大家交流学习 ...
- System.ArgumentException: 目标数组的长度不够。请检查 destIndex 和长度以及数组的下限
扫码支付接口将要上线,近几天在优化系统性能.昨天把日志Helper类的日志记录改成了使用Queue<T>对象来实现异步处理.做了单元测试,并模拟多线程来测试后,发现正常.今天将站点部署到准 ...
- PHP固定长度字符串
/** * 获取固定长度随机字符串 * @param $n * @return string * @throws Exception */ function gf_rand_str($n) { if ...
- 小记:目标数组的长度不够。请检查 destIndex 和长度以及数组的下限。
异常:System.ArgumentException: 目标数组的长度不够.请检查 destIndex 和长度以及数组的下限.(不好意思忘记截图了) 发生异常的代码如下: var list = ne ...
- STL容器适配器 stack, queue
stack是一种后进先出(last in first out)的数据结构.它只有一个出口,如图所示.stack允许新增元素,删除元素,取得最顶端元素.但除了最顶端外,没有其他任何地方可以存储stack ...
- 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 ...
随机推荐
- Hibernate中两种获取Session的方式
转自:https://www.jb51.net/article/130309.htm Session:是应用程序与数据库之间的一个会话,是hibernate运作的中心,持久层操作的基础.对象的生命周期 ...
- ndoejs后台查询数据库返回的值-进行解析
JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json对象转换成json对符串
- View Controller Programming Guide for iOS---(三)---Using View Controllers in Your App
Using View Controllers in Your App Whether you are working with view controllers provided by iOS, or ...
- 深度解密Go语言之 map
目录 什么是 map 为什么要用 map map 的底层如何实现 map 内存模型 创建 map 哈希函数 key 定位过程 map 的两种 get 操作 如何进行扩容 map 的遍历 map 的赋值 ...
- hdoj5003【wa水】
蜜汁wa,蜜汁wa,少了个\n------ #include<bits/stdc++.h> using namespace std; typedef long long LL; typed ...
- AGC031 A~C
A题意:给定字符串s,求无重复字符子序列个数(子序列相同位置不同算不同) 在最后加一串a~z表示选了这些就是不选这个字符了,然后答案就是每次选每个字符位置的方案数的积 #include<iost ...
- 如何正确访问Redis中的海量数据?服务才不会挂掉!
一.前言 有时候我们需要知道线上的Redis的使用情况,尤其需要知道一些前缀的key值,让我们怎么去查看呢?并且通常情况下Redis里的数据都是海量的,那么我们访问Redis中的海量数据?如何避免事故 ...
- mac的日常使用总结
目录 有一个github的仓库:(强烈推荐) 不推荐的但是可以试试的一些链接: # 关于mac book的使用教程 github简直是一个宝藏,发现好多各种好玩的东西, 爱了爱了, 开源一定是未来, ...
- S.O.L.I.D: PHP 面向对象设计的五个基准原则
S.O.L.I.D 是首个 5 个面向对象设计 (OOD) 准则的首字母缩写,这些准则是由 Robert C. Martin 提出的,他更为人所熟知的名字是 Uncle Bob. 这些准则使得开发出易 ...
- jQuery 第九章 工具方法
$.type() $.isArray() $.isFunction() $.isWindow()... $.trim() $.proxy() $.noConflict() $.each() $.map ...