drop-out栈
1、drop-out栈能够用来做什么?
在许多提供编辑功能的软件,如word、ps、画图,都会提供“撤销”和“恢复”功能,使用drop-out能够实现这些功能。
2、drop-out栈特性
drop-out栈是一种特殊的栈,具有如下特征:
1、栈的大小固定
2、如果在栈满的情况下希望往栈中压入一个数据,栈底的数据会被清除以腾出空间容纳要新的数据。
3、drop-out栈实现
在此提供一种drop-out栈的实现方法:循环队列
package learnspring.learnspring.normalJava; import java.awt.datatransfer.StringSelection; /**
* @author 肖政宇
* @date 2019-09-28 21:21
* 说明:drop-out栈具有这样的特性:栈满以后,如果试图向栈中压入数据,
* 栈底的数据会自动被弹出。
*/
public class DropOutStack {
/**
* size - 栈大小
* stack - 栈
* top - 栈顶(当前能够插入数据的位置)
* bottom - 栈底
* total - 栈内数据总数
*/
private final int size;
private Object[] stack;
private int top;
private int bottom;
private int total; /**
* constructor
*
* @param stackSize - the size of the stack
* @throws Exception - wrong size
*/
public DropOutStack(int stackSize) throws Exception {
if (stackSize <= 1) {
throw new Exception("wrong value!" + stackSize);
}
this.size = stackSize;
this.stack = new Object[stackSize];
this.top = 0;
this.bottom = 0;
this.total = 0;
} /**
* get the size of the stack
*
* @return - the size of the stack
*/
public int size() {
return size;
} /**
* push a data into the stack
*
* @param object = data that you want to push onto the stack
* @return - true or false
*/
public Boolean push(Object object) {
try {
stack[top] = object;
top = (top + 1) % size;
//stack is full
if (total == size) {
//the bottom element have been drop
bottom = (bottom + 1) % size;
} else {//stack is not full yet
total++;
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} /**
* get the data from the top of the stack
*
* @return - a data from the top of the stack
* @throws Exception - the stack is empty
*/
public Object peek() throws Exception {
if (total == 0) {
throw new Exception("the stack is empty!");
}
//get the data from the top of the stack
top = top == 0 ? size - 1 : top - 1;
return stack[top];
} /**
* drop a data from the top of the stack
*
* @return - a data from the top of the stack
* @throws Exception - the stack is empty
*/
public Object pop() throws Exception { if (total == 0) {
throw new Exception("the stack is empty!");
}
//get the data from the top of the stack
top = top == 0 ? size - 1 : top - 1;
Object object = stack[top];
//delete the data from the top of the stack
stack[top] = null;
//change the number of elements in the stack
total--;
return object;
} /**
* Is the stack empty?
*
* @return - true or false
*/
public Boolean isEmpty() {
return total == 0;
} @Override
public String toString() {
StringBuilder stringStack = new StringBuilder();
stringStack.append('[');
for (int i = 0; i < size; i++) {
if (stack[i] != null) {
stringStack.append(stack[i]);
} else {
stringStack.append("null");
}
if (i + 1 < size) {
stringStack.append(',');
}
}
stringStack.append(']');
return stringStack.toString();
}
}
drop-out栈的更多相关文章
- Git实战指南----跟着haibiscuit学Git(第三篇)
笔名: haibiscuit 博客园: https://www.cnblogs.com/haibiscuit/ Git地址: https://github.com/haibiscuit?tab=re ...
- 2021 从零开始学Git【新版本Git - 8000字详细介绍】
我写的这篇文章,主要是记录自己的学习过程,也希望帮助读者少踩坑(比如不同版本可能命令不兼容等).本文面向git零基础初学者,建议读者按照文中命令自己全部操作一遍(注意运行环境). 我的运行环境:win ...
- Linux内核--网络栈实现分析(二)--数据包的传递过程(上)
本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7492423 更多请看专栏,地址 ...
- online ddl 使用、测试及关键函数栈
[MySQL 5.6] MySQL 5.6 online ddl 使用.测试及关键函数栈 http://mysqllover.com/?p=547 本文主要分为三个部分,第一部分是看文档时的笔记:第 ...
- Linux内核--网络栈实现分析(二)--数据包的传递过程--转
转载地址http://blog.csdn.net/yming0221/article/details/7492423 作者:闫明 本文分析基于Linux Kernel 1.2.13 注:标题中的”(上 ...
- SQL Server 堆表与栈表的对比(大表)
环境准备 使用1个表,生成1000万行来进行性能对比(勉强也算比较大了),对比性能差别. 为了简化过程,不提供生成随机数据的过程.该表初始为非聚集索引(堆表),测试过程中会改为聚集索引(栈表). CR ...
- 全栈必备 JavaScript基础
1995年,诞生了JavaScript语言,那一年,我刚刚从大学毕业.在今年RedMonk 推出的2017 年第一季度编程语言排行榜中,JavaScript 排第一,Java 第二,Python 反超 ...
- Elastic 技术栈之 Logstash 基础
title: Elastic 技术栈之 Logstash 基础 date: 2017-12-26 categories: javatool tags: java javatool log elasti ...
- Android开发 ---SQLite数据库,lock文件,结果集游标,适配器,安全退出,给连接设置下划线,编辑器,投影,ContentValues存储,DbHelper,activity栈
目录截图: 1.activity_main.xml 主界面效果: <?xml version="1.0" encoding="utf-8"?> &l ...
- 急速JavaScript全栈教程
3 天前 · 3k 次阅读 急速JavaScript全栈教程 javascript node.js mongodb 140 自从一年前发布了Vuejs小书的电子书,也有些日子没有碰过它们了,现在 ...
随机推荐
- 《C语言深度解剖》学习笔记之关键字
第一章 关键字 C语言共有32个关键字. 关键字 auto 声明自动变量 int 声明整型变量 long 声明长整型变量 char 声明字符型变量 float 声明浮点型变量 short 声明短整 ...
- 报错No module named IPython的解决方法
没有按照 ipython 或者 ide 没有选择编译器
- uda 2.C++ 向量
向量与矩阵代数 学习得不错!你已经学习了大量 C++ 句法.你也许注意到了,使用 C++ 编程无疑比使用 Python 困难.C++ 专为快速执行而设计,使用这门语言,你可以采用许多不同方式达到同一结 ...
- 如何使用jmeter调用soap协议
- "?:"在正则表达式中什么意思
“?:”非获取匹配,匹配冒号后的内容但不获取匹配结果,不进行存储供以后使用. 单独的“?”:匹配前面的子表达式零次或一次. 当“?”紧跟在任何一个其他限制符(*,+,?,{n},{n,},{n,m}) ...
- PHP实现微信小程序人脸识别刷脸登录功能
首先我们先确认我们的百度云人脸库里已经上传了我们的个人信息照片 然后我们在后台写刷脸登陆的接口login我们要把拍照获取的照片存储到服务器 public function login(){ // ...
- Vue中computed与method的区别
转载于:https://segmentfault.com/a/1190000014478664?utm_source=tag-newest 1.computed区别于method的两个核心 在官方文档 ...
- Linux下的实用工具——计算器bc
Linux下的实用工具——计算器 1. bc指令算加法,如图: 4. bc指令算除法(进阶),如图示,10/3之所以为3,是因为我们没有指定小数点后取几位,默认取到整数部分:而10/100之所以为 ...
- layer/layui弹出层插件bug
<button class="layui-btn" lay-submit lay-filter="formDemo" id="layui-btn ...
- 给websocket加入心跳包防止自动断开连接
var userId=$("#userId").val(); var lockReconnect = false; //避免ws重复连接 var ws = null; // 判断当 ...