2021-09-23

java 线性数据结构
java 数据结构之 stack

public class Stack {
    private int[] elements;
    public  Stack(){
        elements=new int[0];
    }
    //实现入栈与出栈
    public void add(int value){     //栈中添加元素
        int[] newArr=new int[elements.length+1];
        for (int i = 0; i <elements.length ; i++) {
            newArr[i]=elements[i];

        }
        newArr[elements.length]=value;
        elements=newArr;
    }
    public  void show(){  //展示栈中的元素
        for (int i = 0; i <elements.length ; i++) {
            System.out.println(elements[i]);
        }
    }
    public void pop(){  //取出栈顶元素
        int[] newArr=new int[elements.length-1];
        int stackTop=elements[elements.length-1];
        for (int i = 0; i <elements.length-1 ; i++) {
          newArr[i]=elements[i];
        }
        elements=newArr;
        System.out.println("取出的栈顶元素是:"+stackTop);
    }
    public Boolean isEmpty(){
        return elements.length==0;
    }

java 数据结构之 queue

public class MyQueue {
    private int[]elements;
    public MyQueue(){
        elements= new  int[0];
    }
    public void  add(int value ){
        int[] newArr=new int[elements.length+1];
        for (int i = 0; i <elements.length ; i++) {
            newArr[i]=elements[i];

        }
        newArr[elements.length]=value;
        elements=newArr;
    }
    public  void show(){  //展示栈中的元素
        for (int i = 0; i <elements.length ; i++) {
            System.out.println(elements[i]);
        }
    }
    public void pop(){
        int[] newArr=new int[elements.length-1];
        int target=elements[0];
        for (int i = 0; i <elements.length-1 ; i++) {
            newArr[i]=elements[i+1];
        }
        System.out.println("取出队列的元素是:"+target);
        elements=newArr;
    }
    public Boolean isEmpty(){
        return  elements.length==0;
    }

上一篇:ABAQUS ERROR


下一篇:项目实践1:进行时(一)