【知识积累】链表实现栈和队列

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 日 21:30:06
 * @since : 1.0
 */
public class J003_StackAndQueue2Node {

    public static void main(String[] args) {
        int number = 100000;
        int count = 100;
        int value = 100;
        for (int i = 0; i < number; 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 < 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 (!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 (!isEqual(myQueue.pop(), queue.poll())){
                            System.out.println("queue error");
                        }
                    }
                }
            }
        }
        System.out.println("ending");
    }

    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 class MyStack<T>{
        private DoubleEndsQueue<T> queue;

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

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

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

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

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

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

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

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

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

    public static class Node<T>{
        public T value;
        public Node<T> next;
        public Node<T> last;

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

    public static class DoubleEndsQueue<T>{
        public Node<T> head;
        public Node<T> tail;

        /**
         * 从头部添加元素
         * @param value
         */
        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;
            }
        }

        /**
         * 从尾部添加元素
         * @param value
         */
        public void addFromBottom(T value){
            Node<T> cur = new Node<>(value);
            if (head == null){
                head = cur;
                tail = cur;
            }else {
                tail.next = cur;
                cur.last = tail;
                tail = cur;
            }
        }

        /**
         * 从头部弹出元素
         * @return
         */
        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;
        }

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

        public boolean isEmpty(){
            return head == null;
        }
    }

}

猜你喜欢

转载自blog.csdn.net/axin1240101543/article/details/113846165