用固定长度的数组实现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 ...
随机推荐
- struts2添加需要的jar包
转自:https://blog.csdn.net/fance611261/article/details/6790737 以前总是在myeclipse中添加jar包的,由于现在转向了eclipse,原 ...
- 编写hadoop程序并打成jar包上传到hadoop集群运行
准备工作: 1. hadoop集群(我用的是hadoop-2.7.3版本),这里hadoop有两种:1是编译好的hadoop-2.7.3:2是源代码hadoop-2.7.3-src: 2. 自己的机器 ...
- Mac系统下源码编译安装MySQL 5.7.17
1.下载并解压到:/Users/xiechunping/Softwares/mysql-5.7.17下载地址:http://ftp.ntu.edu.tw/pub/MySQL/Downloads/MyS ...
- hdu2476【区间DP,未完待续】
好难搞得东西.... 题意都懒得写了,看题解的巨巨莫怪啊,未完待续未完待续,回去睡觉.
- Java 在线反编译
使用jd-gui反编译java提示 // INTERNAL ERROR // 的类,用在线反编译直接反编译.class http://www.showmycode.com/
- bzoj 2435: [Noi2011]道路修建【树形dp】
dp求size和deep,然后对每条边模拟求代价即可 #include<iostream> #include<cstdio> #include<algorithm> ...
- C#递归拷贝文件夹下文件以及文件夹
public bool CopyOldLabFilesToNewLab(string sourcePath, string savePath) { if (!Directory.Exists(save ...
- 最小公倍数的最小和(Minimum Sum LCM )
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> u ...
- D. Black Hills golden jewels 二分答案 + 二分判定
http://codeforces.com/gym/101064/problem/D 题目是给定一个数组,如果两两组合,有C(n, 2)种结果,(找出第一个大于等于第k大的结果) 思路, 二分答案va ...
- R语言中的并行处理
网上有人说foreach包可以并行,我就去弄了,结果发现一个普通的二重循环什么事都不错都很卡!捣鼓了半天才发现是foreach的问题 为了提速,做了如下事宜: 直接利用矩阵列加减,不是一个个遍历加 把 ...