ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

单链表实现队列和栈

2022-07-30 22:31:07  阅读:105  来源: 互联网

标签:head 单链 队列 System 实现 println size public out


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有