栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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 

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

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 

手写栈类:数组实现

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];
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/332521.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号