Java怎么使用跳转结构实现队列和栈

发布时间:2023-04-18 17:12:46 作者:iii
来源:亿速云 阅读:69

本篇内容介绍了“Java怎么使用跳转结构实现队列和栈”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

队列

跳转结构结点

public static class Node<T> {
    public T value;
    public Node<T> next;
    public Node(T value) {
        this.value = value;
    }
    @Override
    public String toString() {
        ArrayList<T> nums = new ArrayList<>();
        Node<T> node = this;
        while (node != null) {
            nums.add(node.value);
            node = node.next;
        }
        return nums.toString();
    }
}
实现队列
public static class MyQueue<T> {
    private Node<T> head;
    private Node<T> tail;
    private int size;
    public MyQueue() {
        head = null;
        tail = null;
        size = 0;
    }
    // 插入一个元素
    public void offer(T t) {
        Node<T> node = new Node<>(t);
        if (head == null) {
            head = node;
        } else {
            tail.next = node;
        }
        tail = node;
        size++;
    }
    // 弹出一个元素
    public T poll() {
        T ans = null;
        if (head != null) {
            ans = head.value;
            head = head.next;
            size--;
        }
        if (head == null) {
            tail = null;
        }
        return ans;
    }
//  查看队首元素
    public T peek() {
        T ans = null;
        if (head != null) {
            ans = head.value;
        }
        return ans;
    }
    //检查 队列是否为空
    public Boolean isEmpty() {
        return size == 0;
    }
    // 查看队列的长度
    public int size() {
        return size;
    }
}
测试队列
public static void main(String[] args) {
    MyQueue<Integer> myQueue = new MyQueue<>();
    Queue<Integer> test = new LinkedList<>();
    int testTime = 5000000;
    int maxValue = 200000000;
    System.out.println("测试开始!");
    for (int i = 0; i < testTime; i++) {
        if (myQueue.isEmpty() != test.isEmpty()) {
            System.out.println("Oops!");
        }
        if (myQueue.size() != test.size()) {
            System.out.println("Oops!");
        }
        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 num1 = myQueue.poll();
                Integer num2 = test.poll();
                if (!num1.equals(num2)) {
                    System.out.println("Oops!");
                }
            }
        } else {
            if (!myQueue.isEmpty()) {
                Integer num1 = myQueue.peek();
                Integer num2 = test.peek();
                if (!num1.equals(num2)) {
                    System.out.println("Oops!");
                }
            }
        }
    }
    if (myQueue.size() != test.size()) {
        System.out.println("Oops!");
    }
    while (!myQueue.isEmpty()) {
        Integer num1 = myQueue.poll();
        Integer num2 = test.poll();
        if (!num1.equals(num2)) {
            System.out.println("Oops!");
        }
    }
    System.out.println("测试结束!");
}

实现栈
public static class MyStack<T> {
    private Node<T> head;
    private int size;
    public MyStack() {
        head = null;
        size = 0;
    }
    //检查 栈是否为空
    public Boolean isEmpty() {
        return size == 0;
    }
    // 查看栈的长度
    public int size() {
        return size;
    }
    // 插入一个元素
    public void push(T t) {
        Node<T> node = new Node<>(t);
        if (head != null) {
            node.next = head;
        }
        head = node;
        size++;
    }
    public T pop() {
        T ans = null;
        if (head != null) {
            ans = head.value;
            head = head.next;
            size--;
        }
        return ans;
    }
    //  查看栈顶元素
    public T peek() {
        T ans = null;
        if (head != null) {
            ans = head.value;
        }
        return ans;
    }
}
测试代码
public static void main(String[] args) {
    MyStack<Integer> myStack = new MyStack<>();
    Stack<Integer> test = new Stack<>();
    int testTime = 5000000;
    int maxValue = 200000000;
    System.out.println("测试开始!");
    for (int i = 0; i < testTime; i++) {
        if (myStack.isEmpty() != test.isEmpty()) {
            System.out.println("Oops!");
        }
        if (myStack.size() != test.size()) {
            System.out.println("Oops!");
        }
        double decide = Math.random();
        if (decide < 0.33) {
            int num = (int) (Math.random() * maxValue);
            myStack.push(num);
            test.push(num);
        } else if (decide < 0.66) {
            if (!myStack.isEmpty()) {
                int num1 = myStack.pop();
                int num2 = test.pop();
                if (num1 != num2) {
                    System.out.println("Oops!");
                }
            }
        } else {
            if (!myStack.isEmpty()) {
                int num1 = myStack.peek();
                int num2 = test.peek();
                if (num1 != num2) {
                    System.out.println("Oops!");
                }
            }
        }
    }
    if (myStack.size() != test.size()) {
        System.out.println("Oops!");
    }
    while (!myStack.isEmpty()) {
        int num1 = myStack.pop();
        int num2 = test.pop();
        if (num1 != num2) {
            System.out.println("Oops!");
        }
    }
    System.out.println("测试结束!");
}

“Java怎么使用跳转结构实现队列和栈”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. Java8中日期比较方法有哪些
  2. java如何使用Calender.before()、Calender.after()和Calender.equals()方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:vue部署后静态文件加载404怎么解决

下一篇:怎么使用Python实现生成对角矩阵和对角块矩阵

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》