双链表实现双端队列

package class04;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 双链表实现双端队列
 */
public class Code03_DoubleLinkedListToDeque {
    public static class Node<V> {
        public V value;
        public Node<V> pre;
        public Node<V> next;

        public Node(V v) {
            value = v;
            pre = null;
            next = null;
        }
    }

    public static class MyDeque<V> {
        private Node<V> head;
        private Node<V> tail;
        private int size;

        public MyDeque() {
            head = null;
            tail = null;
            size = 0;
        }

        public int size() {
            return size;
        }

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

        //头部添加元素
        public void leftPush(V value) {
            Node<V> cur = new Node<>(value);
            if (head == null) {
                head = cur;
                tail = cur;
            } else {
                cur.next = head;
                head.pre = cur;
                head = cur;//zuo的写法
//                head = head.pre;//我之前的写法
            }
            size++;
        }

        //尾部添加元素
        public void rightPush(V value) {
            Node<V> cur = new Node<>(value);
            if (head == null) {
                head = cur;
                tail = cur;
            } else {
                cur.pre = tail;
                tail.next = cur;
                tail = cur;//zuo的写法
//                tail = tail.next;//我之前的写法
            }
            size++;
        }

        //头部弹出元素
        public V leftPop() {
            V ans = null;
            if (head == null) {
                return ans;
            }
            size--;
            ans = head.value;
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                head = head.next;
                head.pre = null;
            }
            return ans;
        }

        //尾部弹出元素
        public V rightPop() {
            V ans = null;
            if (tail == null) {//head == null
                return ans;
            }
            size--;
            ans = tail.value;
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                tail = tail.pre;
                tail.next = null;
            }
            return ans;
        }

        //返回头部将要弹出,但是实际并不弹出的元素
        public V leftPeek() {
            V ans = null;
            if (head != null) {
                ans = head.value;
            }
            return ans;
        }

        //返回尾部将要弹出,但是实际并不弹出的元素
        public V rightPeek() {
            V ans = null;
            if (tail != null) {
                ans = tail.value;
            }
            return ans;
        }

    }

    //测试双端队列
    public static void testMyDeque() {
        int testTimes = 10000;
        int maxValue = 1000;
        MyDeque<Integer> myDeque = new MyDeque<>();
        Deque<Integer> test = new LinkedList<>();

        System.out.println("test begin!");
        for (int i = 0; i < testTimes; i++) {
//            System.out.println("i = " + i);
//            printMyDeque(myDeque.head);
            if (myDeque.isEmpty() != test.isEmpty()) {
                System.out.println("DequeOops1!");
            }
            if (myDeque.size() != test.size()) {
                System.out.println("DequeOops2!");
            }
            double random = Math.random();
            if (random < 0.33) {
                int num = (int) (Math.random() * maxValue);
                double random1 = Math.random();
                if (random1 < 0.5) {
                    myDeque.leftPush(num);
                    test.addFirst(num);
                } else {
                    myDeque.rightPush(num);
                    test.addLast(num);

                }
            } else if (random < 0.66) {
                if (!myDeque.isEmpty()) {
                    Integer num1;
                    Integer num2;
                    double random1 = Math.random();
                    if (random1 < 0.5) {
                        num1 = myDeque.leftPop();
                        num2 = test.pollFirst();
                    } else {
                        num1 = myDeque.rightPop();
                        num2 = test.pollLast();
                    }
                    if (!num1.equals(num2)) {
                        System.out.println("DequeOops3!");
                    }
                }
            } else {
                if (!myDeque.isEmpty()) {
                    double random1 = Math.random();
                    Integer num1;
                    Integer num2;
                    if (random1 < 0.5) {
                        num1 = myDeque.leftPeek();
                        num2 = test.peekFirst();
                    } else {
                        num1 = myDeque.rightPeek();
                        num2 = test.peekLast();
                    }
                    if (!num1.equals(num2)) {
                        System.out.println("DequeOops4!");

                    }
                }
            }
        }
        if (myDeque.size() != test.size()) {
            System.out.println("DequeOops5!");
        }
        while (!myDeque.isEmpty()) {
            Integer num1 = myDeque.leftPop();
            Integer num2 = test.pollFirst();
            if (!num1.equals(num2)) {
                System.out.println("DequeOops6!");
            }
        }
        System.out.println("test finished!");
    }

    private static void printMyDeque(Node head) {
        while (head != null) {
            System.out.print(head.value + " ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        testMyDeque();
    }
}

 

posted @ 2022-07-30 22:33  TheFloorIsNotTooHot  阅读(29)  评论(0)    收藏  举报