java实现栈功能

1.使用数组方式

    public static void main(String[] args) throws Exception {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int operateNum = Integer.parseInt(br.readLine());//操作次数String inputInfo;//输入信息StringBuilder outputSb = new StringBuilder();//输出信息StackInner stackInner = new StackInner();stackInner.initCap(operateNum);for (int i = 0; i < operateNum; i++) {inputInfo = br.readLine();if ("top".equals(inputInfo)) {Integer topData = stackInner.top();if (topData == null) {outputSb.append("error").append("\n");} else {outputSb.append(topData).append("\n");}} else if ("pop".equals(inputInfo)) {Integer popData = stackInner.pop();if (popData == null) {outputSb.append("error").append("\n");} else {outputSb.append(popData).append("\n");}} else {stackInner.push(Integer.parseInt(inputInfo.split(" ")[1]));}}System.out.println(outputSb);}public static class StackInner {private int topIndex = 0;private Integer[] dataArray = null;public void initCap(int size) {dataArray = new Integer[size];}public void push(int data) {dataArray[topIndex] = data;topIndex++;}public Integer top() {if (topIndex > 0) {return dataArray[topIndex - 1];} else {return dataArray[topIndex];}}public Integer pop() {Integer topData = top();if (topData == null) {return null;}if (topIndex > 0) {dataArray[topIndex - 1] = null;topIndex--;} else {dataArray[topIndex] = null;}return topData;}}

2.使用链表方式

    public static void main(String[] args) throws Exception {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int operateNum = Integer.parseInt(br.readLine());//操作次数String inputInfo;//输入信息StringBuilder outputSb = new StringBuilder();//输出信息StackInner stackInner = new StackInner();for (int i = 0; i < operateNum; i++) {inputInfo = br.readLine();if ("top".equals(inputInfo)) {//topint topData = stackInner.top();if (topData == Integer.MIN_VALUE) {outputSb.append("error").append("\n");} else {outputSb.append(topData).append("\n");}} else if ("pop".equals(inputInfo)) {//popint popData = stackInner.pop();if (popData == Integer.MIN_VALUE) {outputSb.append("error").append("\n");} else {outputSb.append(popData).append("\n");}} else {//pushString[] inputInfoArray = inputInfo.split(" ");stackInner.push(Integer.parseInt(inputInfoArray[1]));}}System.out.println(outputSb);}//节点,使用链实现栈功能private static class Node {public Node(int num) {this.data = num;}public int data;//数据public Node nextNode;//下一个节点}//内部栈public static class StackInner {private Node headNode;//头节点public void push(Integer num) {if (headNode == null) {headNode = new Node(num);} else {Node newHead = new Node(num);newHead.nextNode = headNode;headNode = newHead;}}public int top() {if (headNode == null) {return Integer.MIN_VALUE;} else {return headNode.data;}}public int pop() {if (headNode == null) {return Integer.MIN_VALUE;} else {int popNum = headNode.data;Node newHeadNode = headNode.nextNode;headNode.nextNode = null;headNode = newHeadNode;return popNum;}}}

        在牛客网上提交测试,最好运行时间在390ms左右

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/463375.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

WWW 2024 | 时间序列(Time Series)和时空数据(Spatial-Temporal)论文总结

WWW 2024已经放榜&#xff0c;本次会议共提交了2008篇文章&#xff0c;research tracks共录用约400多篇论文&#xff0c;录用率为20.2%。本次会议将于2024年5月13日-17日在新加坡举办。 本文总结了WWW 2024有关时间序列&#xff08;Time Series&#xff09;和时空数据&#xf…

【OrangePi Zero2 智能家居】阿里云人脸识别方案

一、接入阿里云 二、C语言调用阿里云人脸识别接口 三、System V消息队列和POSIX 消息队列 一、接入阿里云 在之前树莓派的人脸识别方案采用了翔云平台的方案去1V1上传比对两张人脸比对&#xff0c;这种方案是可行&#xff0c;可 以继续采用。但为了接触更多了云平台方案&…

ios设备解锁 --Apeaksoft iOS Unlocker

Apeaksoft iOS Unlocker是一款针对iOS系统的密码解锁工具。其主要功能包括解锁多种锁屏类型&#xff0c;包括数字密码、Touch ID、Face ID和自定义密码。此外&#xff0c;它还可以帮助用户删除iPhone密码以进入锁屏设备&#xff0c;忘记的Apple ID并将iPhone激活为新的&#xf…

蓝桥杯每日一题------背包问题(一)

背包问题 阅读小提示&#xff1a;这篇文章稍微有点长&#xff0c;希望可以对背包问题进行系统详细的讲解&#xff0c;在看的过程中如果有任何疑问请在评论区里指出。因为篇幅过长也可以进行选择性阅读&#xff0c;读取自己想要的那一部分即可。 前言 背包问题可以看作动态规…

再识C语言 DAY17 【什么是原码、反码和补码】

文章目录 前言本文总结于此文章 一、知识补充二、原码三、反码四&#xff0c;补码 总结如果您发现文章有错误请与我留言&#xff0c;感谢 前言 本文总结于此文章 一、知识补充 通常&#xff0c;1字节包含8位。C语言用字节&#xff08;byte&#xff09;表示储存系统字符集所需…

跟着pink老师前端入门教程-day23

苏宁网首页案例制作 设置视口标签以及引入初始化样式 <meta name"viewport" content"widthdevice-width, user-scalableno, initial-scale1.0, maximum-scale1.0, minimum-scale1.0"> <link rel"stylesheet" href"css/normaliz…

Tkinter教程22:DataFrame数据加入到treeview树视图(含横纵滚动条+正反排序)

------------★Tkinter系列教程★------------ Tkinter教程21&#xff1a;Listbox列表框OptionMenu选项菜单Combobox下拉列表框控件的使用绑定事件 Tkinter教程20&#xff1a;treeview树视图组件&#xff0c;表格数据的插入与表头排序 Python教程57&#xff1a;tkinter中如何…

刘谦魔术我用代码还原了,魔术尽头是数学,数学尽头是神学!

刘谦在春晚让两个半张扑克牌合在一起的时候&#xff0c;我就知道其中必然有数学的奥妙。 假设我们初始卡牌为1&#xff0c;2&#xff0c;3&#xff0c;4。对半撕开后我们定义扑克牌为&#xff1a; 1(1) 2(1) 3(1) 4(1) 1(2) 2(2) 3(2) 4(2)按照刘谦的魔术&#xff0c;你需要…

C语言之:编译和链接

目录 1. 翻译环境和运行环境翻译环境 2. 翻译环境&#xff1a;预编译编译汇编链接预处理&#xff08;预编译&#xff09;编译词法分析语法分析语义分析汇编链接运行环境 1. 翻译环境和运行环境 在ANSI C的任何一种实现中&#xff0c;存在两个不同的环境。 第一种是翻译环境&a…

常用的前端模块化标准总结

1、模块化标准出现以前使用的模块化方案&#xff1a; 1&#xff09;文件划分&#xff1a; 将不同的模块定义在不同的文件中&#xff0c;然后使用时通过script标签引入这些文件 缺点&#xff1a; 模块变量相当于是定义在全局的&#xff0c;容易造成变量名冲突&#xff08;即不…

分享76个表单按钮JS特效,总有一款适合您

分享76个表单按钮JS特效&#xff0c;总有一款适合您 76个表单按钮JS特效下载链接&#xff1a;https://pan.baidu.com/s/1CW9aoh23UIwj9zdJGNVb5w?pwd8888 提取码&#xff1a;8888 Python采集代码下载链接&#xff1a;采集代码.zip - 蓝奏云 学习知识费力气&#xff0c;收集…

[office] excel如何计算毛重和皮重的时间间隔 excel计算毛重和皮重时间间隔方法 #笔记#学习方法

excel如何计算毛重和皮重的时间间隔 excel计算毛重和皮重时间间隔方法 在日常工作中经常会到用excel&#xff0c;有时需要计算毛重和皮重的时间间隔&#xff0c;具体的计算方式是什么&#xff0c;一起来了解一下吧 在日常工作中经常会到用excel&#xff0c;在整理编辑过磅数据…