首页 > 其他分享 >双链表实现栈,和队列

双链表实现栈,和队列

时间:2022-11-03 00:13:04浏览次数:89  
标签:head cur 队列 queue 实现 tail 双链 null public

package class03;

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

/**
 * 双链表实现栈,和队列
 */
public class Code03_DoubleEndsQueueToStackAndQueue {
    /**
     * 双链表
     *
     * @param <T>
     */
    public static class Node<T> {
        public T value;
        public Node<T> last;
        public Node<T> next;

        public Node(T v) {
            value = v;
        }
    }

    /**
     * 双端队列
     *
     * @param <T>
     */
    public static class DoubleEndsQueue<T> {
        public Node<T> head;
        public Node<T> tail;

        //从头部插入节点
        public void addFromHead(T value) {
            Node<T> cur = new Node<T>(value);
            if (head == null) {
                head = cur;
                tail = cur;
            } else {
                cur.next = head;
                head.last = cur;
                head = cur;
            }
        }

        //从尾部插入节点
        public void addFromBottom(T value) {
            Node<T> cur = new Node<T>(value);
            if (head == null) {
                head = cur;
                tail = cur;
            } else {
                cur.last = tail;
                tail.next = cur;
                tail = cur;
            }
        }

        //从头部弹出节点
        public T popFromHead() {
            if (head == null) {
                return null;
            }
            Node<T> cur = head;
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                head = head.next;
                cur.next = null;
                head.last = null;
            }
            return cur.value;
        }

        //从尾部弹出节点
        public T popFromBottom() {
            if (head == null) {
                return null;
            }
            Node<T> cur = tail;
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                tail = tail.last;
                tail.next = null;
                cur.last = null;
            }
            return cur.value;
        }

        //是否是空链表
        public boolean isEmpty() {
            return head == null;
        }
    }

    /**
     * 自定义栈MyStack
     *
     * @param <T>
     */
    public static class MyStack<T> {
        private DoubleEndsQueue<T> queue;

        public MyStack() {
            queue = new DoubleEndsQueue<>();
        }

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

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

        public boolean isEmpty() {
            return queue.isEmpty();
        }
    }

    /**
     * 自定义队列MyQueue
     *
     * @param <T>
     */
    public static class MyQueue<T> {
        private DoubleEndsQueue<T> queue;

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

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

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

        public boolean isEmpty() {
            return queue.isEmpty();
        }
    }

    //for test 两个数字是否相等
    public static boolean isEqual(Integer a, Integer b) {
        if (a == null && b != null) {
            return false;
        }
        if (a != null && b == null) {
            return false;
        }
        if (a == null && b == null) {
            return true;
        }
        return a.equals(b);
    }

    public static void main(String[] args) {
        int testTimes2 = 100;
        int maxValue = 10000;
        int testTimes = 100000;
        System.out.println("test start!");
        for (int i = 0; i < testTimes; i++) {
            MyStack<Integer> myStack = new MyStack<>();
            Stack<Integer> stack = new Stack<>();
            MyQueue<Integer> myQueue = new MyQueue<>();
            Queue<Integer> queue = new LinkedList<>();
            for (int j = 0; j < testTimes2; j++) {
                int numS = (int) (Math.random() * maxValue);
                if (myStack.isEmpty()) {
                    myStack.push(numS);
                    stack.add(numS);
                } else {
                    if (Math.random() < 0.5) {
                        myStack.push(numS);
                        stack.add(numS);
                    } else {
                        if (!isEqual(myStack.pop(), stack.pop())) {
                            System.out.println("oops1!");
                            break;
                        }
                    }
                }
                int numQ = (int) (Math.random() * maxValue);
                if (myQueue.isEmpty()) {
                    myQueue.push(numQ);
                    queue.add(numQ);
                } else {
                    if (Math.random() < 0.5) {
                        myQueue.push(numQ);
                        queue.add(numQ);
                    } else {
                        if (!isEqual(myQueue.poll(), queue.poll())) {
                            System.out.println("oops2!");
                            break;
                        }
                    }
                }
            }
        }
        System.out.println("test end!");
    }

}

 

标签:head,cur,队列,queue,实现,tail,双链,null,public
From: https://www.cnblogs.com/TheFloorIsNotTooHot/p/16853009.html

相关文章