java实现栈功能

文章介绍了使用Java数组和链表实现栈的操作,包括push、top和pop,并提到在牛客网上进行性能测试时,目标是达到390ms左右的最佳运行时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)) {//top
                int topData = stackInner.top();
                if (topData == Integer.MIN_VALUE) {
                    outputSb.append("error").append("\n");
                } else {
                    outputSb.append(topData).append("\n");
                }
            } else if ("pop".equals(inputInfo)) {//pop
                int popData = stackInner.pop();
                if (popData == Integer.MIN_VALUE) {
                    outputSb.append("error").append("\n");
                } else {
                    outputSb.append(popData).append("\n");
                }
            } else {//push
                String[] 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左右

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kenick

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值