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小书的电子书,也有些日子没有碰过它们了,现在 ...
随机推荐
- 从零学React Native之05混合开发
本篇文章,我们主要讨论如何实现Android平台的混合开发. RN给Android端发送消息 首先打开Android Studio, Open工程, 在React Native项目目录下选择andro ...
- @bzoj - 4378@ [POI2015] Pustynia
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个长度为 n 的正整数序列 a,每个数都在 1 到 10^ ...
- php解压缩
1.zip文件 2.rar文件 3.php调用linux指令进行解压缩 解压7z文件: 注:Windows下的文件编码和LINUX不一样,中文系统为GB,LINUX为UTF-8编码,这种情况下,中文名 ...
- Bootstrap之Form表单验证神器: BootstrapValidator(转)
前言:做Web开发的我们,表单验证是再常见不过的需求了.友好的错误提示能增加用户体验.博主搜索bootstrap表单验证,搜到的结果大部分都是文中的主题:bootstrapvalidator.今天就来 ...
- poj 1271 && uva 10117 Nice Milk (半平面交)
uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= ...
- JavaScript 拖曳和居中问题
今天遇到了一个问题,是这样的,有一个div盒子,实现盒子居中,居中的样式是这样的见下 #box{ width:300px; height:150px; position:absolute; left: ...
- H3C 配置NAT Server
- Delphi的不足
Delphi拥有C#那样的开发速度,同时运行速度也很快,而且不需要.net运行时(可以免安装直接运行).为什么还是衰落了呢? 既不是单根体系,又缺少泛型支持.导致delphi没法做map.list.v ...
- java 使用反射调用可变参数方法
使用反射操作对象-调用可变参数方法 要把可变参数都当做是其对应的数组类型参数; 如 show(XX... is)作为show(XX[] is)调用; 若可变参数元素类型是引用类型: JDK内部接收到参 ...
- vue基于 element-ui 实现菜单动画效果,任意添加 li 个数均匀撑满 ul 宽度
%)%)%%%))) .) .) .) .) .) .) .) .) .) .) .) .) .) .) .) .% %% %deg);}