两个队列实现一个栈

出队原理

这里写图片描述

代码实现

class QueueLink{
    int elem[];
    /**
     * 队头
     */
    int front;
    /**
     * 队尾
     */
    int rear;
    /**
     * 当前循环内部有效循环个数
     */
    int usedSize = 0;
    int allSize = 0;
    /**
     * 无参构造函数
     */
    public QueueLink(){
        this(10);
    }
    /**
     * 有参构造函数
     * @param size
     */
    public QueueLink(int size){
        this.elem = new int[size+1];
        this.front = 0;
        this.rear = 0;
        this.allSize= size+1;
    }
    /**
     * 判断是否为满
     */
    public boolean isFull(){
        if((this.rear+1)%this.allSize == this.front){
            return true;
        }
        return false;
    }
    /**
     * 入队
     */
    public void push(int val){
        if(isFull()){
            return;
        }
        this.elem[this.rear] = val;
        this.rear = (this.rear+1)%this.allSize;
        this.usedSize++;
    }
    /**
     * 判断是否为空
     */
    public boolean isEmpty(){
        return this.front == this.rear;
    }
    /**
     * 出队
     */
    public int pop(){
        if(isEmpty()){
            return -1;
        }
        int a = this.elem[this.front];
        this.elem[this.front] = -1;
        this.front = (this.front+1)%this.allSize;
        this.usedSize--;
        return a;
    }
    /**
     * 得到队头元素
     */
    public int getTop(){
        if(isEmpty()){
            return -1;
        }
        return this.elem[this.front];
    }
    /**
     * 打印输出
     */
    public void show(){
        for(int i = this.front;i < this.rear;i = (i+1)%this.allSize){
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }
}
public class Test3 {
    public static void enter(QueueLink q1,QueueLink q2,int val){
        //先定义一个空
        QueueLink p=null;
        //永远让p指向有数据的队列
        if(!q1.isEmpty()){
            p=q1;

        }else{
            p=q2;
        }    
        p.push(val);
        }
    public static int pop(QueueLink q1,QueueLink q2){
        //指向出队的队列
        QueueLink p1=null;
        //指向出队元素需要放置的地方
        QueueLink p2=null;
        if(!q1.isEmpty()){
            p1=q1;
            p2=q2;
        }else{
            p1=q2;
            p2=q1;
        }
        if(p1.isEmpty()){

        }else{
            //知道出队到一个元素跳出,并返回此元素值
            while(p1.usedSize>1){
                //入队p1出队元素
                p2.push(p1.pop());
                }
            return p1.pop();
            }
        return -1;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        QueueLink q1=new QueueLink ();
        QueueLink q2=new QueueLink ();
        q1.push(10);
        q1.push(20);
        q1.push(30);
        enter(q1,q2,40);
        q1.show();
        System.out.println("=====出栈后======");
        System.out.println(pop(q1,q2));
        q1.show();
        System.out.println("=====出栈后=====");
        System.out.println(pop(q1,q2));
        System.out.println("=====剩余=====");
        q1.show();
    }
}

运行结果

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值