public class Stack { int[] data; int maxSize; int top;
public Stack(int maxSize) { this.maxSize = maxSize; data = new int[maxSize]; top = -1; } /** * 依次加入数据 * @param data 要加入的数据 * @return 添加是否成功 */ public boolean push(int data) { if(top+1== maxSize) { System.out.println(“栈已满!”); return false; } this.data[++top] = data; return true; } /** * 从栈中取出数据 * @return 取出的数据 */ public int pop() throws Exception{ if(top==-1) { throw new Exception(“栈已空!”); } return this.data[top--]; } public static void main(String[] args) throws Exception { Stack stack=new Stack(1000); stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.push(5); while(stack.top>=0){ System.out.println(stack.pop()); } } }
如何用java代码写一个堆栈
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:Linux关机和重启命令
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
用Java写一个生日祝福
用Java写一个生日祝福
System Image 抽象类 -
java写一个接收数据的接口 java怎么写接口
随着互联网各岗位精细化分工的普及,出现了很多的系统架构设计,比如常见的前后端分离架构,后端提供接口给前端,前端根据接口的数据进行渲染,大家各执其职,效率也非常的高,但是随着接口的增加,如果不统一的规范就会额外的增加大量的沟通成本以及学习成本,对管理者而言是非常的不利。为此,我在这篇文章分享给大家一个Java生鲜电商平台中如何返回统一格式的API的整体架构设计。一般系统的大致整体架
java写一个接收数据的接口 JAVA接口返回数组怎么写 后端返回json数据给前端 验证手机号格式不能为空和格式