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栈的更多相关文章

  1. Git实战指南----跟着haibiscuit学Git(第三篇)

    笔名:  haibiscuit 博客园: https://www.cnblogs.com/haibiscuit/ Git地址: https://github.com/haibiscuit?tab=re ...

  2. 2021 从零开始学Git【新版本Git - 8000字详细介绍】

    我写的这篇文章,主要是记录自己的学习过程,也希望帮助读者少踩坑(比如不同版本可能命令不兼容等).本文面向git零基础初学者,建议读者按照文中命令自己全部操作一遍(注意运行环境). 我的运行环境:win ...

  3. Linux内核--网络栈实现分析(二)--数据包的传递过程(上)

    本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7492423 更多请看专栏,地址 ...

  4. online ddl 使用、测试及关键函数栈

    [MySQL 5.6] MySQL 5.6 online ddl 使用.测试及关键函数栈  http://mysqllover.com/?p=547 本文主要分为三个部分,第一部分是看文档时的笔记:第 ...

  5. Linux内核--网络栈实现分析(二)--数据包的传递过程--转

    转载地址http://blog.csdn.net/yming0221/article/details/7492423 作者:闫明 本文分析基于Linux Kernel 1.2.13 注:标题中的”(上 ...

  6. SQL Server 堆表与栈表的对比(大表)

    环境准备 使用1个表,生成1000万行来进行性能对比(勉强也算比较大了),对比性能差别. 为了简化过程,不提供生成随机数据的过程.该表初始为非聚集索引(堆表),测试过程中会改为聚集索引(栈表). CR ...

  7. 全栈必备 JavaScript基础

    1995年,诞生了JavaScript语言,那一年,我刚刚从大学毕业.在今年RedMonk 推出的2017 年第一季度编程语言排行榜中,JavaScript 排第一,Java 第二,Python 反超 ...

  8. Elastic 技术栈之 Logstash 基础

    title: Elastic 技术栈之 Logstash 基础 date: 2017-12-26 categories: javatool tags: java javatool log elasti ...

  9. Android开发 ---SQLite数据库,lock文件,结果集游标,适配器,安全退出,给连接设置下划线,编辑器,投影,ContentValues存储,DbHelper,activity栈

    目录截图: 1.activity_main.xml 主界面效果: <?xml version="1.0" encoding="utf-8"?> &l ...

  10. 急速JavaScript全栈教程

    3 天前  ·  3k 次阅读 急速JavaScript全栈教程 javascript node.js  mongodb 140 自从一年前发布了Vuejs小书的电子书,也有些日子没有碰过它们了,现在 ...

随机推荐

  1. mysql数据库之linux版本

    http://repo.mysql.com/yum/mysql-5.6-community/ 安装                                                    ...

  2. PyTorch 学习笔记(四):权值初始化的十种方法

    pytorch在torch.nn.init中提供了常用的初始化方法函数,这里简单介绍,方便查询使用. 介绍分两部分: 1. Xavier,kaiming系列: 2. 其他方法分布 Xavier初始化方 ...

  3. VSCode配置启动Vue项目

    下载安装并配置VSCode 随便百度上搜个最新的VSCode安装好后,点击Ctrl + Shit + X打开插件扩展窗口进行插件扩展,这里要安装两个插件. 1.vetur插件的安装 该插件是vue文件 ...

  4. Mybatis/Ibatis,数据库操作的返回值

    该问题,我百度了下,根本没发现什么有价值的文章:还是看源代码(详见最后附录)中的注释,最有效了!insert,返回值是:新插入行的主键(primary key):需要包含<selectKey&g ...

  5. 解决 vs 出现Error MC3000 给定编码中的字符无效

    在 xaml 写中文注释,发现编译失败 Error MC3000 给定编码中的字符无效 我的 xaml 写了一句代码 <Grid> <!--林德熙--> </Grid&g ...

  6. 一文告诉你Adam、AdamW、Amsgrad区别和联系 重点

    **序言:**Adam自2014年出现之后,一直是受人追捧的参数训练神器,但最近越来越多的文章指出:Adam存在很多问题,效果甚至没有简单的SGD + Momentum好.因此,出现了很多改进的版本, ...

  7. hadoop2.6.0 + hbase-1.0.0 伪分布配置

    1 基本配置 主机名: 192.168.145.154 hadoop2 ======= 2 etc/hadoop下文件配置 1)core-site.xml <configuration> ...

  8. 洛谷P3366 【模板】最小生成树 题解

    题目链接:https://www.luogu.org/problem/P3366 最小生成树模板题. Kruskal算法 算法思想:给边按边权从小到大排序,然后遍历每一条边,如果边上的两个点不在同一个 ...

  9. JOISC2014 挂饰("01"背包)

    传送门: [1]:洛谷 [2]:BZOJ 参考资料: [1]:追忆:往昔 •题解 上述参考资料的讲解清晰易懂,下面谈谈我的理解: 关键语句: 将此题转化为 "01背包" 类问题,关 ...

  10. Java 5,6,7,8,9,10,11新特性吐血总结

    作者:拔剑少年 简书地址:https://www.jianshu.com/u/dad4d9675892 博客地址:https://it18monkey.github.io 转载请注明出处 java5 ...