广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java使用跳转结构实现队列和栈流程详解
  • 264
分享到

Java使用跳转结构实现队列和栈流程详解

Java跳转结构实现队列Java跳转结构实现栈 2023-05-15 14:05:02 264人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录导读队列跳转结构结点实现队列测试队列栈实现栈测试代码导读 在数据结构当中所有的数据结构都是由 连续数据结构或者跳转数据结构 单独或者拼接做成。 连续结构和跳转结构是数据结构中常见

导读

数据结构当中所有的数据结构都是由 连续数据结构或者跳转数据结构 单独或者拼接做成。

连续结构和跳转结构是数据结构中常见的两种基本数据结构,而我们本次的主角栈和队列都 既可以使用使用跳转结构实现也可以使用连续结构实现。

本文主要是介绍了如何通过跳转结构实现栈和队列,在实现栈和队列之后并使用 对数器 对写出的栈和队列进行测试。

队列

跳转结构结点

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使用跳转结构实现队列和栈流程详解的文章就介绍到这了,更多相关Java跳转结构实现队列和栈内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java使用跳转结构实现队列和栈流程详解

本文链接: https://www.lsjlt.com/news/209115.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java使用跳转结构实现队列和栈流程详解
    目录导读队列跳转结构结点实现队列测试队列栈实现栈测试代码导读 在数据结构当中所有的数据结构都是由 连续数据结构或者跳转数据结构 单独或者拼接做成。 连续结构和跳转结构是数据结构中常见...
    99+
    2023-05-15
    Java跳转结构实现队列 Java跳转结构实现栈
  • Java怎么使用跳转结构实现队列和栈
    本篇内容介绍了“Java怎么使用跳转结构实现队列和栈”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!队列跳转结构结点public s...
    99+
    2023-07-06
  • C语言分别实现栈和队列详解流程
    目录什么是栈栈的结构图示栈的实现创建栈的结构体初始化栈入栈出栈获取栈顶元素获取栈中有效元素个数检测栈是否为空栈的销毁什么是队列?队列的实现创建队列结构体初始化队列队尾入队列队头出队列...
    99+
    2022-11-13
  • Java数据结构专题解析之栈和队列的实现
    目录1. 栈1.1 概念1.2 助解图题1.3 栈的数组实现1.4 问题1.5 栈的单链表实现2. 队列2.1 概念2.2 问题2.3 队列的单链表实现2.4 数组实现队列2.5 循...
    99+
    2022-11-12
  • Golang使用gob实现结构体的序列化过程详解
    目录Gob简介单个对象序列化列表数据序列化简单编码示例编码在TCP连接中使用Golang有自己的序列化格式,称为gob。使用gob可以对结构进行编码和解码。你可以使用其他格式,如JS...
    99+
    2023-03-08
    Golang序列化结构体 Golang gob序列化
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作