【Java数据结构】实现栈和队列

ced485cbb11e458d81a746890b32cf3f.gif 

作者:渴望力量的土狗

博客主页:渴望力量的土狗的博客主页

专栏:数据结构与算法

工欲善其事必先利其器,给大家介绍一款超牛的斩获大厂offer利器——牛客网

点击免费注册和我一起刷题吧

 

【Java数据结构】实现栈和队列_第1张图片

栈:

MyStack.java

public class MyStack {
    //栈的底层我们用数组存储数据
    int[] elements;

    public MyStack() {
        elements = new int[0];
    }
    //压入元素
    public void push(int element){
        //创建一个新数组
        int[] newArr = new int[elements.length + 1];
        //把原数组的值复制到新数组中
        for (int i = 0; i < elements.length; i++) {
            newArr[i] = elements[i];
        }
        //把添加的元素放入新数组中
        newArr[elements.length] = element;
        //使用新数组替换旧数组
        elements = newArr;
    }
    //取出栈顶元素
    public int pop(){
        if (elements.length ==0){
            throw new RuntimeException("stack is Empty");
        }
        //取出数组中最后一个元素
        int element = elements[elements.length-1];
        //创建一个新数组
        int[] newArr = new int[elements.length-1];
        for (int i = 0; i < elements.length-1; i++) {
            newArr[i] = elements[i];
        }
        elements = newArr;
        return element;
    }
    //查看栈顶元素
    public int peek(){
        return elements[elements.length-1];
    }
    //判断栈顶是否为空
    public boolean isEmpty(){
        return elements.length == 0;
    }
}

Test.java

public class Test {
    public static void main(String[] args) {
        MyStack ms = new MyStack();
        ms.push(3);
        ms.push(6);
        ms.push(4);
        ms.push(45);
        System.out.println("栈顶元素是"+ms.peek());
        ms.pop();
        System.out.println("栈顶元素是"+ms.peek());
        System.out.println(ms.isEmpty());
    }

}

测试结果:

【Java数据结构】实现栈和队列_第2张图片

【Java数据结构】实现栈和队列_第3张图片 

 队列:

MyQueue.java

public class MyQueue {
    int[] elements;
    public MyQueue() {
        elements = new int[0];
    }
    public void add(int element){
        int[] newArr = new int[elements.length+1];
        for (int i = 0; i < elements.length; i++) {
            newArr[i] = elements[i];
        }
        newArr[elements.length] = element;
        elements = newArr;
    }
    public int poll(){
        if (elements.length == 0){
            throw new RuntimeException("queue is Empty");
        }
        int element = elements[0];
        int[] newArr = new int[elements.length - 1];
        for (int i = 0; i < elements.length - 1; i++) {
            newArr[i] = elements[i+1];
        }
        elements = newArr;
        return element;
    }
    public boolean isEmpty(){
        return elements.length == 0;
    }

}

Test.java

public class Test {
    public static void main(String[] args) {
        MyQueue mq = new MyQueue();
        mq.add(1);
        mq.add(4);
        mq.add(5);
        mq.add(45);
        System.out.println(mq.poll());
        System.out.println(mq.poll());
        System.out.println(mq.poll());
        System.out.println(mq.poll());
        System.out.println(mq.isEmpty());

    }


}

测试结果:

【Java数据结构】实现栈和队列_第4张图片

 

你可能感兴趣的:(数据结构与算法,java,数据结构,开发语言,栈,队列)