【知识积累】数组实现栈和队列

package com.example.demo.algorithm.D002;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @Description :
 * 数组实现栈和队列
 *
 * @Author : Darren
 * @Date : 2021 年 02 月 09 日 20:50:06
 * @since : 1.0
 */
public class J004_StackAndQueue2Array {

    public static void main(String[] args) {
        int number = 100000;
        int count = 100;
        int value = 100;
        for (int i = 0; i < number; i++) {
            MyStack myStack = new MyStack(100);
            MyQueue myQueue = new MyQueue(100);
            Stack<Integer> stack = new Stack<>();
            Queue<Integer> queue = new LinkedList<>();
            for (int j = 0; j < count; j++) {
                int nums = (int) (Math.random() * value);
                if (stack.isEmpty()){
                    myStack.push(nums);
                    stack.push(nums);
                }else{
                    if (Math.random() < 0.5){
                        myStack.push(nums);
                        stack.push(nums);
                    }else {
                        if (!J003_StackAndQueue2Node.isEqual(myStack.pop(), stack.pop())){
                            System.out.println("stack error");
                        }
                    }
                }

                int numq = (int) (Math.random() * value);
                if (queue.isEmpty()){
                    myQueue.push(numq);
                    queue.offer(numq);
                }else{
                    if (Math.random() < 0.5){
                        myQueue.push(numq);
                        queue.offer(numq);
                    }else {
                        if (!J003_StackAndQueue2Node.isEqual(myQueue.pop(), queue.poll())){
                            System.out.println("queue error");
                        }
                    }
                }
            }
        }
        System.out.println("ending");
    }

    public static class MyStack{
        int[] arrays;
        int limit;
        /**
         * 下一个放元素的位置
         */
        int index;

        public MyStack(int limit) {
            this.arrays = new int[limit];
            this.limit = limit;
            this.index = 0;
        }

        public void push(int value){
            //如果index等于limit 说明数组满了 放完最后一个元素后 index++ == limit
            if (index == limit){
                throw new RuntimeException("栈满了,不能再加了");
            }
            arrays[index] = value;
            index++;
        }

        public int pop(){
            //如果index小于1 说明数组空了 弹出最后一个元素后  index-- == 0
            if (index < 1){
                throw new RuntimeException("栈空了,不能再拿了");
            }
            int result = arrays[index - 1];
            index--;
            return result;
        }

        public boolean isEmpty() {
            return index < 1;
        }

    }

    public static class MyQueue{
        int[] arrays;
        int limit;
        /**
         * 当前有多少个元素
         * 通过这个变量判断还有多少空间可以放元素
         */
        int size;
        /**
         * 放元素的下一个位置
         */
        int pushIndex;
        /**
         * 弹元素的下一个位置
         */
        int popIndex;

        public MyQueue(int limit) {
            this.arrays = new int[limit];
            this.limit = limit;
            this.size = 0;
            this.pushIndex = 0;
            this.popIndex = 0;
        }

        public void push(int value){
            if (size == limit){
                throw new RuntimeException("栈满了,不能再加了");
            }
            size++;
            arrays[pushIndex] = value;
            //去计算下一个pushIndex的位置
            pushIndex = nextIndex(pushIndex);
        }

        public int pop(){
            if (size == 0){
                throw new RuntimeException("栈空了,不能再拿了");
            }
            size--;
            int result = arrays[popIndex];
            popIndex = nextIndex(popIndex);
            return result;
        }

        public boolean isEmpty() {
            return size == 0;
        }

        private int nextIndex(int index) {
            //如果index来到了limit-1的位置,说明数组的最大下标放了数据,直接来到0下标的位置
            return index < limit - 1 ? index + 1 : 0;
        }
    }

}

 

Guess you like

Origin blog.csdn.net/axin1240101543/article/details/113846186