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小书的电子书,也有些日子没有碰过它们了,现在 ...
随机推荐
- 阿里云应用实时监控 ARMS 再升级,支持 Prometheus 开源生态
摘要: 应用实时监控服务 (ARMS) 是一款APM类的监控产品. 用户可基于 ARMS 的前端.应用.自定义监控,快速构建实时的应用性能和业务监控能力.ARMS 让所有性能问题“一屏了然”,不遗余力 ...
- @loj - 3022@ 「CQOI2017」老 C 的方块
目录 @description@ @solution@ @accepted code@ @details@ @description@ 老 C 是个程序员. 作为一个懒惰的程序员,老 C 经常在电脑上 ...
- 【HAOI2015】树上染色
[HAOI2015]树上染色 这题思路好神仙啊,首先显然是树形dp,f[i][j]表示在以i为根的子树中选j个黑点对答案的贡献(并不是当前子树最大值),dp时只考虑i与儿子连边的贡献.此时(i,son ...
- tinyhttpd简介
一:简介: tinyhttpd是由J. DavidBlackstone在1999年编写的,实现了一个很简单的web服务器.支持GET和POST方法,总代码量也就在500行左右,可以用来学习HTTP协议 ...
- 为 Ubuntu 18.04 添加开机自动加载 ntfs分区 功能
注意:Ubuntu终端命令是区分大小写的 1,准备的: ntfs-3g -- 提供ntfs读写支持(一般说来是自带的,若没有,可是使用 sudo apt-get isntall ntfs-3g ...
- Laravel 5.5 将会要求 PHP 7.0+
Laravel 5.5 都要用 PHP7 了呢!你还在用 PHP 5 吗? Laravel 一直是一个精 (sheng) 进 (ji) 不 (hen) 休 (kuai) 的框架.就在前几天,下图这位 ...
- [转]移动APP安全测试
1 移动App安全风险分析 1.1 安全威胁分析 安全威胁从三个不同环节进行划分,主要分为客户端威胁.数据传输端威胁和服务端的威胁. 1.2 面临的主要风险 1.3 Android测试思维 ...
- call,apply,bind详解
为什么要改变this指向? 我们知道bind,call,apply的作用都是用来改变this指向的,那为什么要改变this指向呢?请看下面的例子: var name="lucy"; ...
- rsa加密(非对称加密)
rsa加密 是非对称加密 需要公钥 与 私钥 这个公钥私钥的具体值需要与后端协商定下 rsa js代码如下 代码太多不插入了 html代码如下 <!DOCTYPE html> <ht ...
- HDU 2602Bone Collector 01背包问题
题意:给出一个t代表有t组数据,然后给出n,n代表有n种石头,v代表旅行者的背包容量,然后给出n种石头的价值和容量大小,求能带走的最大价值 思路:01背包问题,每种石头只有拿与不拿两种状态.(其实我是 ...