【链表算法基础】基于双向链表实现双向队列

package ListText;

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

// 双链表实现双端队列
public class DoubleLinkedListToDeque {

    /**
     * 双端队列为什么要用双链表,主要是因为
     * 如新节点为node;头插时,可以使用 node - > head;头出时,可以找到head,直接返回node
     * 尾插时,可以使用 tail - > node,但是尾出的时候,需要出的是tail,需要找到指向tail的节点,但是此时是无法找上节点的,所以就需要有个last指针指向上节点才能实现双端队列
     */
    public static class Node<V> {
        public Node<V> last;
        public Node<V> next;
        public V value;

        public Node(V v) {
            this.value = v;
        }

    }

    /**
     * 双端队列
     */
    public static class MyDeque<V> {
        public Node<V> head;
        public Node<V> tail;
        public int size;

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

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

        public int size() {
            return size;
        }

        // 头插法,新node - > head,然后新node作为head
        // head的last指向新node
        public void pushHead(V value) {
            Node<V> node = new Node<>(value);
            if (head == null) {
                tail = node;
            } else {
                node.next = head;
                head.last = node;
            }
            head = node;
            size++;
        }

        // 尾插法,tail -> 新node,tail作为新node
        // 新node的last指向tail
        public void pushTail(V value) {
            Node<V> node = new Node<>(value);
            if (head == null) {
                head = node;
            } else {
                tail.next = node;
                node.last = tail;
            }
            tail = node;
            size++;
        }

        // 头出法,抛出head,然后将head.next的last指针指向为空,防止内存泄露
        public V pollHead() {
            V ans = null;
            if (head == null) {
                tail = null;
                return null;
            }
            ans = head.value;
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                head = head.next;
                head.last = null;
            }
            size--;
            return ans;

        }

        // 尾出法,抛出tail,然后tail.last作为tail,并且要将tail.next为空防止内存泄露
        public V pollTail() {
            V ans = null;
            if (head == null) {
                tail = null;
                return null;
            }
            ans = tail.value;
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                tail = tail.last;
                tail.next = null;
            }
            size--;
            return ans;
        }

        public V peekHead() {
            V ans = null;
            if (head != null) {
                ans = head.value;
            }
            return ans;
        }

        public V peekTail() {
            V ans = null;
            if (tail != null) {
                ans = tail.value;
            }
            return ans;
        }
    }

    public static void testMyDeque() {
        MyDeque<Integer> myDeque = new MyDeque<>();
        Deque<Integer> test = new LinkedList<>();
        int testTime = 5000000;
        int maxValue = 200000000;
        System.out.println("测试开始!");
        for (int i = 0; i < testTime; i++) {
            if (myDeque.isEmpty() != test.isEmpty()) {
                System.out.println("Oops!");
            }
            if (myDeque.size() != test.size()) {
                System.out.println("Oops!");
            }
            double decide = Math.random();
            if (decide < 0.33) {
                int num = (int) (Math.random() * maxValue);
                if (Math.random() < 0.5) {
                    myDeque.pushHead(num);
                    test.addFirst(num);
                } else {
                    myDeque.pushTail(num);
                    test.addLast(num);
                }
            } else if (decide < 0.66) {
                if (!myDeque.isEmpty()) {
                    int num1 = 0;
                    int num2 = 0;
                    if (Math.random() < 0.5) {
                        num1 = myDeque.pollHead();
                        num2 = test.pollFirst();
                    } else {
                        num1 = myDeque.pollTail();
                        num2 = test.pollLast();
                    }
                    if (num1 != num2) {
                        System.out.println("Oops!");
                    }
                }
            } else {
                if (!myDeque.isEmpty()) {
                    int num1 = 0;
                    int num2 = 0;
                    if (Math.random() < 0.5) {
                        num1 = myDeque.peekHead();
                        num2 = test.peekFirst();
                    } else {
                        num1 = myDeque.peekTail();
                        num2 = test.peekLast();
                    }
                    if (num1 != num2) {
                        System.out.println("Oops!");
                    }
                }
            }
        }
        if (myDeque.size() != test.size()) {
            System.out.println("Oops!");
        }
        while (!myDeque.isEmpty()) {
            int num1 = myDeque.pollHead();
            int num2 = test.pollFirst();
            if (num1 != num2) {
                System.out.println("Oops!");
            }
        }
        System.out.println("测试结束!");
    }

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


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值