判断某一数列能否通过另一数列进行栈操作实现(数列按指定顺序入栈,判断其出栈是否合法?)

GDUF 数据结构第七周第二题:

原题描述:“判断某一数列能否通过另一数列进行栈操作实现”,该描述不清楚。

转为下列问题描述:

数列按指定顺序入栈,判断其出栈是否合法?

即:在 [1. 2, 3, 4, 5]的入栈顺序下,检验 [4 ,3, 2, 1, 5] 是否为合法的出栈序列

java实现:

package week7_2;

public class two {
    public static void main(String[] args) {
        //数列按指定顺序入栈,判断其出栈是否合法
        //思路:1.将指定顺序放入order数组,出栈序列放入tag数组;
        //     2.用index标记tag数组下标,
        //       从前到后遍历order数组,每次遍历先将order数组当前元素压入栈中,
        //       然后将用while循环比较 "栈顶元素" 与 "tag[index]" ,若二者相等则将栈顶元素出栈且index++,直到栈空或二者不同;
        //     3.结束循环时,若栈空则tag数组所保存的序列合法

        circleQueue queue=new circleQueue(6);
        int[] order={1,2,3,4,5};//指定以“1 2 3 4 5”的顺序入栈。将其保存在order数组
        int[] tag={3,2,1,4,5};//待检验目标序列
        boolean isLegal=isLegal(tag,order);
        System.out.println(isLegal);
    }
    public static boolean isLegal(int[] tag,int[] order){
        arrayStack stack = new arrayStack(order.length);
        int index=0;//当前tag数组的下标
        for ( int i=0;i<order.length;i++){//遍历order序列的元素
            stack.push(order[i]);//每次遍历都将order序列中第i个元素压栈
            while (!stack.isEmpty()&&stack.peek()==tag[index]){
                //上述语句为什么要用”while“?不用”if“?
                //答:因为接下来要对栈顶元素与tag数组进行多次比较,if只能比较一次,无法实现 ”多次将栈顶与tag数组的元素弹出“的操作
                // 压栈后要判断tag数组中index所指元素与栈顶元素是否相同,
                // 若相同则将当前两个元素同时弹出,
                // 直到栈中元素为空或者栈中元素与tag数组元素不同为止
                stack.pop();
                index++;
            }
        }
        boolean isLegal=false;
        if (stack.isEmpty()){//当栈为空说明指定序列tag合法。
            isLegal=true;
        }
        return isLegal;
    }


}

手写循环队列类:数组实现 

package week7_2;

class circleQueue{
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;
    public circleQueue(int max){
        maxSize=max;
        arr=new int[maxSize];
    }
    public boolean isFull(){
        return (rear+1)%maxSize==front;
    }

    public boolean isEmpty(){
        return rear==front;
    }

    public void addQueue(int n){
        if(isFull()){
            throw new RuntimeException("列满");
        }
        arr[rear]=n;
        rear=(rear+1)%maxSize;
    }
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("列空");
        }
        int temp= arr[front];
        front=(front+1)%maxSize;
        return temp;
    }

    public void showQueue(){
        if (isEmpty()){
            throw new RuntimeException("列空");
        }
        for (int i=front;i<size()+front;i++){
            System.out.printf("arr[%d]=%d\n",i%maxSize,arr[(i%maxSize)]);
        }
    }
    public int size(){
        return (rear+maxSize-front)%maxSize;
    }//??
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("列空");
        }
        return arr[front];
    }

}

手写栈类:数组实现

class arrayStack{
    private  int maxsize;
    private  int[] stack;
    private  int top=-1;
    public arrayStack(int maxsize){
        this.maxsize=maxsize;
        stack =new int[this.maxsize];
    }
    public boolean isFull(){
        return top==maxsize-1;
    }
    public boolean isEmpty(){
        return top==-1;
    }
    public void  push(int n){
        if (isFull()){
            System.out.println("full");
            return;
        }
        else {
            top++;
            stack[top]=n;
        }
    }
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("empty");
        }
        else {
            int temp=stack[top];
            top--;
            return temp;
        }
    }
    public void show(){
        if (isEmpty()){
            System.out.println("empty");
            return;
        }
        for (int i=top;i>-1;i--){
            System.out.print(stack[i]+"  ");
        }
    }
    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("empty");
        }
        else {
            return stack[top];
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值