猿辅导面试算法题(7:一个数组实现两个栈)

本文介绍了一种使用单个数组实现两个栈的数据结构方法,详细解释了其实现原理,并提供了完整的Java代码示例,展示了如何进行元素的压入、弹出及打印栈内元素。

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

一、原理

定义一个数组,由两个指针分别指向数组的首部和尾部,其分别代表两个栈的栈顶
在这里插入图片描述

二、代码实现

package com.jp.yuanfudao.prepare.mianshi.test7;

/**
 * @program: mianjing
 * @description: 一个数组实现两个栈
 * @author: CoderPengJiang
 * @create: 2020-06-26 18:58
 **/
public class Main {
    public static void main(String[] args) {
        DoubleStack doubleStack=new DoubleStack(8);
        doubleStack.push1(1);
        doubleStack.push1(3);
        doubleStack.push1(4);
        doubleStack.push1(5);
        doubleStack.push2(9);
        doubleStack.push2(8);
        doubleStack.push2(7);
        doubleStack.push2(6);
        doubleStack.print_stack1();
        doubleStack.print_stack2();
        System.out.println(doubleStack.getLength());
    }
}

class DoubleStack {
    //两个栈的长度
    private int length;
    private int top1,top2;
    private int[] array;

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getTop1() {
        return top1;
    }

    public void setTop1(int top1) {
        this.top1 = top1;
    }

    public int getTop2() {
        return top2;
    }

    public void setTop2(int top2) {
        this.top2 = top2;
    }

    public int[] getArray() {
        return array;
    }

    public void setArray(int[] array) {
        this.array = array;
    }

    public DoubleStack(int length) {
        this.length = length;
        array=new int[length];
        top1=-1;
        top2=length;
    }
    //定义两个栈的push和pop
    public boolean push1(int a){
        if (top1+1==top2){
            return false;
        }else{
            array[++top1]=a;
            return true;
        }
    }

    public boolean push2(int a){
        if (top2-1==top1){
            return false;
        }else {
            array[--top2]=a;
            return true;
        }
    }

    public boolean pop1(){
        if (top1==-1){
            return false;
        }else {
            top1--;
            return true;
        }
    }

    public boolean pop2(){
        if (top2==length){
            return false;
        }else {
            top2--;
            return true;
        }
    }

    public void print_stack1(){
        if (top1==-1){
            System.out.println(
                    "stack1 is empty!!!"
            );
        }else {
            for (int i=0;i<=top1;i++){
                System.out.print(array[i]+" ");
            }
            System.out.println();
        }
    }

    public void print_stack2(){
        if (top2==length){
            System.out.println(
                    "stack2 is empty!!!"
            );
        }else {
            for (int i=length - 1;i>=top2;i--){
                System.out.print(array[i]+" ");
            }
            System.out.println();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值