数据结构与算法总结(二)

164 阅读1分钟

使用双链表实现栈和队列


    public static class DoubleLinked<T>{
        DoubleLinked<T> preNode;
        DoubleLinked<T> nextNode;
        T value;

        public DoubleLinked(T value) {
            this.value = value;
        }
    }

    static class DoubleEndsQueue<T>{
        DoubleLinked<T> head;
        DoubleLinked<T> tail;

        public void addFromHead(T value){
            DoubleLinked<T> node = new DoubleLinked<>(value);
            if(head == null){
                head = node;
                tail = node;
            }else {
                node.nextNode = head;
                head.preNode = node;
                head = node;
            }
        }

        public void addFromBottom(T value){
            DoubleLinked<T> node = new DoubleLinked<>(value);
            if(tail == null){
                tail = node;
                head = node;
            }else {
                tail.nextNode = node;
                node.preNode = tail;
                tail = node;
            }

        }



        public T popFromHead(){
            if(head ==null){
                return null;
            }
            DoubleLinked<T> value = this.head;
            if(this.head == tail){
                this.head = null;
                tail = null;
            }else {
                this.head = this.head.nextNode;
                this.head.preNode = null;
                value.nextNode = null;
            }

            return value.value;
        }


        public T popFromBottom(){
            if(tail == null){
                return null;
            }
            DoubleLinked<T> node = this.tail;
            if(this.tail == head){
                tail = null;
                head = null;
            }else {
                tail = tail.preNode;
                tail.nextNode = null;
                node.preNode = null;
            }

            return node.value;
        }


    }

    public static class MyStack<T>{
        private DoubleEndsQueue<T> linked;

        public MyStack() {
            this.linked = new DoubleEndsQueue<T>();
        }

        public void add(T value){
            linked.addFromHead(value);
        }

        public T pop(){
            return linked.popFromHead();
        }
    }

    public static class MyQueue<T>{
        private DoubleEndsQueue<T> queue;

        public MyQueue() {
            this.queue = new DoubleEndsQueue<>();
        }

        public void add(T value){
            queue.addFromHead(value);
        }

        public T pop(){
            return queue.popFromBottom();
        }
    }

    public static boolean isEqual(Integer o1, Integer o2) {
        if (o1 == null && o2 != null) {
            return false;
        }
        if (o1 != null && o2 == null) {
            return false;
        }
        if (o1 == null && o2 == null) {
            return true;
        }
        return o1.equals(o2);
    }

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


    }

}

数组实现栈和队列


    class MyStack {
        private int[] array;

        private int size = 0;
        private int index = 0;

        public MyStack(int size) {
            this.array = new int[size];
            this.size = size;
        }

        public void push(int i) {
            if (size < index) {
                array[index++] = i;
            } else if (size == index) {

                throw new RuntimeException("队列满了,不能再加了");
            }
        }

        public int pop() {
            if (index <= 0) {
                throw new RuntimeException("数据为空");
            }
            index--;
            int value = array[index];
            return value;
        }
    }


    class MyQueue {
        private int[] array;
        private int addI = 0;
        private int popI = 0;
        private int size = 0;
        private int limit = 0;

        public MyQueue(int size) {
            array = new int[size];
            this.limit = size;

        }

        public void push(int value) {
            if (size == limit) {
                return;
            }
            size++;
            array[addI] = value;
            addI = nextIndex(addI);
        }

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

        private int nextIndex(int addI) {
            return addI == (limit - 1) ? 0 : ++addI;
        }


    }

}