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

单链表实现队列和栈

作者:互联网

package class04;

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

/**
 * 单链表实现队列和栈
 */
public class Code02_LinkedListToQueueAndStack {
    public static class Node<V> {
        public V value;
        public Node<V> next;

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

    //实现队列
    public static class MyQueue<V> {
        private Node<V> head;
        private Node<V> tail;
        private int size;

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

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

        public int size() {
            return size;
        }

        //队列添加元素的方法
        public void offer(V value) {
            Node<V> cur = new Node<>(value);
//            if (head == null) {
            if (tail == null) {
                head = cur;
                tail = cur;
            } else {
                tail.next = cur;
                tail = cur;
            }
            size++;
        }

        //队列弹出元素的方法
        public V poll() {
            V ans = null;
            if (head != null) {
                System.out.println("node1");
                ans = head.value;
                head = head.next;
                size--;
            }
            //为什么方式1会报空指针异常?方式2就不会报空指针异常?
            //因为上方的if执行完后,head有可能变成了null,所以head == null的情况,要单独判断。而不是使用else。
//            else {//head == null//方式1
//                System.out.println("node2");
//                tail = null;
//            }
//            printMyQueue(head);
            if (head == null) {//方式2
                System.out.println("node2");
                tail = null;
            }
            return ans;
        }

        //队列返回将要弹出,但是实际并不弹出的元素
        public V peek() {
            V ans = null;
            if (head != null) {
                ans = head.value;

            }
            return ans;
        }

    }

    //实现栈
    public static class MyStack<V> {
        private Node<V> head;
        private int size;

        public MyStack() {
            head = null;
            size = 0;
        }

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

        public int size() {
            return size;
        }

        //栈添加元素
        public void push(V value) {
            Node<V> cur = new Node<>(value);
            if (head == null) {
                head = cur;
            } else {
                cur.next = head;
                head = cur;
            }
            size++;
        }

        //栈弹出元素
        public V pop() {
            V ans = null;
            if (head != null) {
                ans = head.value;
                head = head.next;
                size--;
            }
            return ans;
        }

        //栈将要弹出,但是实际并不弹出的元素
        public V peek() {
            return head == null ? null : head.value;
        }

    }

    //测试队列
    public static void testQueue() {
        try {
            MyQueue<Integer> myQueue = new MyQueue<>();
            Queue<Integer> test = new LinkedList<>();
            int testTimes = 1000;
            int maxValue = 1280;
            System.out.println("test begin!");
            for (int i = 0; i < testTimes; i++) {
                if (myQueue.isEmpty() != test.isEmpty()) {
                    System.out.println("Oops1!");
                }
                if (myQueue.size != test.size()) {
                    System.out.println("Oops2!");
                }
                double decide = Math.random();
                if (decide < 0.33) {
                    int num = (int) (Math.random() * maxValue);
                    myQueue.offer(num);
                    test.offer(num);
                } else if (decide < 0.66) {
                    if (!myQueue.isEmpty()) {
                        Integer num = myQueue.poll();
                        Integer num2 = test.poll();
                        System.out.println("num2 = " + num2);
                        System.out.println("num = " + num);
                        //如果不使用equals(),而是使用!=。那么当num大于等于128时,结果就会是false。所以改为使用equals()。
                        //if (num != num2) {
                        if (!num.equals(num2)) {
                            System.out.println("Oops3!");
                        }
                    }
                } else {
                    if (!myQueue.isEmpty()) {
//                        int num = myQueue.peek();
//                        int num2 = test.peek();
                        Integer num = myQueue.peek();
                        Integer num2 = test.peek();
                        if (!num.equals(num2)) {
                            System.out.println("Oops4!");
                        }
                    }
                }
            }
            if (myQueue.size() != test.size()) {
                System.out.println("Oops5!");
            }
            while (!myQueue.isEmpty()) {
                Integer num = myQueue.poll();
                Integer num2 = test.poll();
                if (!num.equals(num2)) {
                    System.out.println("Oops6!");

                }
            }
            System.out.println("test finished!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //测试队列
    public static void testStack() {
        MyStack<Integer> myStack = new MyStack<>();
        Stack<Integer> testStack = new Stack<>();
        int testTimes = 1000;
        int maxValue = 1000;
        System.out.println("test MyStack begin!");
        for (int i = 0; i < testTimes; i++) {
            if (myStack.isEmpty() != testStack.isEmpty()) {
                System.out.println("MyStack_Oops1");
            }
            int size = myStack.size();
            int size1 = testStack.size();
            if (size != size1) {
                System.out.println("MyStack_Oops2!");
            }
            double random = Math.random();
            if (random < 0.33) {
                System.out.println("node1");
                int num = (int) (Math.random() * maxValue);
                myStack.push(num);
                testStack.push(num);
            } else if (random < 0.66) {
                System.out.println("node2");
                if (!myStack.isEmpty()) {
//                    if (!myStack.pop().equals(testStack.pop())) {
                    Integer pop = myStack.pop();
                    Integer pop1 = testStack.pop();
                    System.out.println("pop = " + pop);
                    System.out.println("pop1 = " + pop1);
                    if (!pop1.equals(pop)) {
                        System.out.println("MyStack_Oops3!");
                    }
                }
            } else {
                System.out.println("node3");
                if (!myStack.isEmpty()) {
                    Integer peek = testStack.peek();
                    Integer peek1 = myStack.peek();
                    System.out.println("peek = " + peek);
                    System.out.println("peek1 = " + peek1);
                    if (!peek.equals(peek1)) {
                        System.out.println("MyStack_Oops4!");
                    }
                }
            }
        }
        System.out.println("test MyStack finish!");
    }

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

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

 

标签:head,单链,队列,System,实现,println,size,public,out
来源: https://www.cnblogs.com/TheFloorIsNotTooHot/p/16535998.html