Java栈与队列怎么实现

发布时间:2022-04-02 10:43:40 作者:iii
来源:亿速云 阅读:173

Java栈与队列怎么实现

目录

  1. 引言
  2. 栈的基本概念
  3. 栈的实现
  4. 队列的基本概念
  5. 队列的实现
  6. 栈与队列的应用场景
  7. 栈与队列的对比
  8. 总结

引言

在计算机科学中,栈(Stack)和队列(Queue)是两种非常重要的数据结构。它们在算法设计、系统开发、数据处理等领域中有着广泛的应用。本文将详细介绍栈和队列的基本概念、实现方式以及它们在Java中的具体实现。

栈的基本概念

栈的定义

栈是一种遵循后进先出(LIFO, Last In First Out)原则的线性数据结构。这意味着最后进入栈的元素将最先被移除。栈通常用于处理具有递归性质的问题,如函数调用、表达式求值等。

栈的操作

栈的基本操作包括:

栈的实现

使用数组实现栈

在Java中,可以使用数组来实现栈。数组实现的栈具有固定的容量,因此在实现时需要考虑栈的容量限制。

public class ArrayStack {
    private int maxSize;
    private int[] stackArray;
    private int top;

    public ArrayStack(int size) {
        this.maxSize = size;
        this.stackArray = new int[maxSize];
        this.top = -1;
    }

    public void push(int value) {
        if (isFull()) {
            throw new IllegalStateException("Stack is full");
        }
        stackArray[++top] = value;
    }

    public int pop() {
        if (isEmpty()) {
            throw new IllegalStateException("Stack is empty");
        }
        return stackArray[top--];
    }

    public int peek() {
        if (isEmpty()) {
            throw new IllegalStateException("Stack is empty");
        }
        return stackArray[top];
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

    public int size() {
        return top + 1;
    }
}

使用链表实现栈

链表实现的栈具有动态扩容的特性,因此在实现时不需要考虑容量限制。

public class LinkedListStack {
    private Node top;

    private static class Node {
        int data;
        Node next;

        Node(int data) {
            this.data = data;
        }
    }

    public void push(int value) {
        Node newNode = new Node(value);
        newNode.next = top;
        top = newNode;
    }

    public int pop() {
        if (isEmpty()) {
            throw new IllegalStateException("Stack is empty");
        }
        int value = top.data;
        top = top.next;
        return value;
    }

    public int peek() {
        if (isEmpty()) {
            throw new IllegalStateException("Stack is empty");
        }
        return top.data;
    }

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

    public int size() {
        int count = 0;
        Node current = top;
        while (current != null) {
            count++;
            current = current.next;
        }
        return count;
    }
}

队列的基本概念

队列的定义

队列是一种遵循先进先出(FIFO, First In First Out)原则的线性数据结构。这意味着最先进入队列的元素将最先被移除。队列通常用于处理需要按顺序处理的任务,如任务调度、消息队列等。

队列的操作

队列的基本操作包括:

队列的实现

使用数组实现队列

在Java中,可以使用数组来实现队列。数组实现的队列具有固定的容量,因此在实现时需要考虑队列的容量限制。

public class ArrayQueue {
    private int maxSize;
    private int[] queueArray;
    private int front;
    private int rear;
    private int size;

    public ArrayQueue(int size) {
        this.maxSize = size;
        this.queueArray = new int[maxSize];
        this.front = 0;
        this.rear = -1;
        this.size = 0;
    }

    public void enqueue(int value) {
        if (isFull()) {
            throw new IllegalStateException("Queue is full");
        }
        rear = (rear + 1) % maxSize;
        queueArray[rear] = value;
        size++;
    }

    public int dequeue() {
        if (isEmpty()) {
            throw new IllegalStateException("Queue is empty");
        }
        int value = queueArray[front];
        front = (front + 1) % maxSize;
        size--;
        return value;
    }

    public int peek() {
        if (isEmpty()) {
            throw new IllegalStateException("Queue is empty");
        }
        return queueArray[front];
    }

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

    public boolean isFull() {
        return size == maxSize;
    }

    public int size() {
        return size;
    }
}

使用链表实现队列

链表实现的队列具有动态扩容的特性,因此在实现时不需要考虑容量限制。

public class LinkedListQueue {
    private Node front;
    private Node rear;

    private static class Node {
        int data;
        Node next;

        Node(int data) {
            this.data = data;
        }
    }

    public void enqueue(int value) {
        Node newNode = new Node(value);
        if (isEmpty()) {
            front = newNode;
        } else {
            rear.next = newNode;
        }
        rear = newNode;
    }

    public int dequeue() {
        if (isEmpty()) {
            throw new IllegalStateException("Queue is empty");
        }
        int value = front.data;
        front = front.next;
        if (front == null) {
            rear = null;
        }
        return value;
    }

    public int peek() {
        if (isEmpty()) {
            throw new IllegalStateException("Queue is empty");
        }
        return front.data;
    }

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

    public int size() {
        int count = 0;
        Node current = front;
        while (current != null) {
            count++;
            current = current.next;
        }
        return count;
    }
}

栈与队列的应用场景

栈的应用场景

队列的应用场景

栈与队列的对比

特性 栈(Stack) 队列(Queue)
操作原则 后进先出(LIFO) 先进先出(FIFO)
主要操作 push, pop, peek enqueue, dequeue, peek
应用场景 函数调用、表达式求值、括号匹配 任务调度、消息队列、BFS
实现方式 数组、链表 数组、链表

总结

栈和队列是计算机科学中两种基本且重要的数据结构。栈遵循后进先出的原则,适用于需要按相反顺序处理数据的场景;队列遵循先进先出的原则,适用于需要按顺序处理数据的场景。在Java中,栈和队列可以通过数组或链表来实现,具体选择哪种实现方式取决于应用场景和性能需求。

通过本文的介绍,相信读者已经对栈和队列的基本概念、实现方式以及应用场景有了更深入的理解。在实际开发中,合理选择和使用栈和队列将有助于提高程序的效率和可维护性。

推荐阅读:
  1. 数据结构--栈与队列
  2. c++中栈与队列的实现

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

java

上一篇:Linux下怎么使用Jenkins自动化构建.NET Core应用

下一篇:.NET项目在k8s中运行的Dapr持续集成方法

相关阅读

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

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