当前位置:网站首页>手动实现一个简单的栈
手动实现一个简单的栈
2022-07-01 04:41:00 【小白学习日记】
仿写一下Stack,还有些问题,之后再改进
栈是用来存储逻辑关系为 “一对一” 数据的线性存储结构。
栈的特性:先进后出,只能从栈顶弹出元素,添加元素称为压栈
需求分析:
所需属性:栈顶指针,栈最大容量,用什么存储?【记得之前在哪看过一句,大多数据结构可以用数组加链表实现,这里使用数组模拟】
所需功能:出栈,入栈,创建栈,查看栈顶元素,栈空,栈满
public class MyStack {
/** * 栈的最大容量 */
private int maxSize;
/** * 数组模拟栈 */
private long[] stackArr;
/** * 栈顶 */
private int top;
//创建对象时初始化栈容量
public MyStack(int initCapacity){
maxSize = initCapacity; //设置栈容量
stackArr = new long[maxSize]; //创建数组
top = -1; //没有元素,栈顶指向-1
}
/** * 添加元素到栈顶 * @param newValue */
public void push(long newValue) throws StackException {
boolean full = this.isFull();
if (full){
throw new StackException("栈已满");
}else {
try {
//将新的元素添加push至栈顶,栈顶指针指向新元素
//这里一定是++top,如果top++就会导致原栈顶数据被新元素覆写的情况
stackArr[++top] = newValue;
}catch (ArrayIndexOutOfBoundsException e){
throw new StackException("栈已满");
}
}
}
/** * 从栈顶弹出一个元素 * @return */
public long pop(){
if (isEmpty()){
try {
throw new StackException("栈中没有任何元素");
} catch (StackException e) {
e.printStackTrace();
}
return -1;
}else {
return stackArr[top--];
}
}
/** * 获得栈顶元素 * @return */
public long peek() throws StackException {
if (isEmpty()){
throw new StackException("栈中没有任何元素");
}
return stackArr[top];
}
/** * 判断栈空 * @return */
public boolean isEmpty(){
return (top == -1);
}
/** * 判断栈满 * @return */
public boolean isFull(){
return (top == maxSize);
}
}
简单定义一个异常类
public class StackException extends Exception{
public StackException(String message) {
super(message);
}
}
测试
public class TestStack {
public static void main(String[] args) throws StackException{
MyStack myStack = new MyStack(5);
myStack.push(20);
myStack.push(30);
myStack.push(40);
myStack.push(50);
myStack.push(60);
// myStack.push(70);
while (!myStack.isEmpty()){
long peek = myStack.peek();
System.out.println("栈顶元素为:"+peek);
myStack.pop();
}
myStack.pop();
System.out.println("栈已空");
}
}
下次试着使用链表实现栈
边栏推荐
- 如何看待智慧城市建设中的改变和机遇?
- Extension fragment
- 2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
- STM32扩展版 按键扫描
- [difficult] sqlserver2008r2, can you recover only some files when recovering the database?
- 2022危险化学品生产单位安全生产管理人员题库及答案
- Shell之一键自动部署Redis任意版本
- 【硬十宝典目录】——转载自“硬件十万个为什么”(持续更新中~~)
- JS rotation chart
- 测量三相永磁同步电机的交轴直轴电感
猜你喜欢
LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
Grey correlation cases and codes
2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
Pytorch(四) —— 可视化工具 Visdom
Applications and features of VR online exhibition
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
技术分享| 融合调度中的广播功能设计
STM32扩展板 温度传感器和温湿度传感器的使用
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
随机推荐
最长递增子序列及最优解、动物总重量问题
STM32扩展版 按键扫描
Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
Software testing needs more and more talents. Why do you still not want to take this path?
VR线上展览所具备应用及特色
How to do the performance pressure test of "Health Code"
RDF query language SPARQL
Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
JVM栈和堆简介
Day 52 - tree problem
VIM easy to use tutorial
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
数据加载及预处理
LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
Why is Hong Kong server most suitable for overseas website construction
科研狗可能需要的一些工具
Shell之一键自动部署Redis任意版本
Maixll-Dock 使用方法
Matters behind the construction of paint testing laboratory
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers