Java栈和队列

本文深入探讨了栈和队列这两种基本数据结构的实现方式与应用,包括数组和链表实现的优劣对比,以及如何利用两种数据结构相互转换。详细介绍了栈的后进先出(LIFO)原则和队列的先进先出(FIFO)原则,并提供了具体代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的
代价比较小。

数组实现一个栈

public class Stack {
    private int top = 0;
    private int[] array = new int[100];

    public Stack() {
        this.top = top;
        this.array = array;
    }


    //入栈
    void push(int value) {
        array[top++] = value;
    }

    //出栈
    int pop() {
        return array[top--];
    }

    //查看栈顶元素
    int peek() {
        return array[top - 1];
    }

    //栈是否为空
    boolean isEmpty() {
        return this.top == 0;
    }
}

两个队列实现一个栈

class TwoQueueToStack {
    LinkedList<Integer> queueA = new LinkedList<>();
    LinkedList<Integer> queueB = new LinkedList<>();

    //入栈
    void push(int value) {
        while (queueB != null) {
            queueA.push(queueB.poll());
        }
        queueA.push(value);
    }

    //出栈  栈顶元素移除
    int pop() {
        int head = 0;
        int last = queueA.getLast();
        while (head != last) {
            head = queueA.getFirst();
            queueB.push(queueA.poll());
        }
        queueA.pop();
        return last;
    }
}

队列

只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出
FIFO(First In First Out) 入队列

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数
组头上出数据,效率会比较低。

链表实现队列

class ListQueue {
    static class Node {
        int value;
        Node next;

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

    Node head = null;
    Node last = null;

    public ListQueue() {
        this.head = head;
        this.last = last;
    }

    //入队尾
    void push(int val) {
        Node node = new Node(val);
        node.next = null;
        if (head == null) {
            head = node;
            last = head;
        } else {
            last.next = node;
            last = node;
        }
    }

    //出队头,返回删除的元素值
    int pop() {
        int val = head.value;
        head = head.next;
        if (head == null) {
            last = null;
        }
        return val;
    }

    //返回队首元素
    int frount() {
        return this.head.value;
    }

    //判断是否为空
    boolean isEmpty() {
        return head == null;
    }
}

两个栈实现一个队列

class TwoStackToQueue {
    Stack<Integer> stackA = new Stack();
    Stack<Integer> stackB = new Stack();

    //入队尾
    void push(int value) {
        while (stackB != null) {
            stackA.push(stackB.pop());
        }
        stackA.push(value);
    }

    //出队头
    int pop() {
        while (stackA != null) {
            stackB.push(stackA.pop());
        }
        return stackB.pop();
    }

    //判断是否为空
    boolean isEmpty() {
        return stackB.empty() && stackA.empty();
    }

    //返回队首元素
    int peek() {
        while (stackA != null) {
            stackB.push(stackA.pop());
        }
        return stackB.peek();
    }

}

实现一个循环队列

class ReQueue {
    Object[] queue;
    int head;
    int last;
    int lenth;

    ReQueue(int lenth) {
        this.lenth = lenth;
        queue = new Object[lenth + 1];
        head = last = 0;
    }

    //判断是否为满
    boolean isFull() {
        if (head == (last + 1)%lenth) {
            return true;
        } else {
            return false;
        }
    }

    //判断是否为空
    boolean isEmpty() {
        if (head == last) {
            return true;
        } else {
            return false;
        }
    }

    //插入元素 成功则返回true
    boolean enQueque(int value) {
        if (isFull()) {
            return false;
        } else {
            queue[last] = value;
            last = (last + 1) % lenth;
            return true;
        }
    }

    //删除头元素 删除成功返回true 删除失败返回false
    boolean delQueuehead(){
        if (isEmpty()) {
            return false;
        }else {
            head=(head+1)%lenth;
            return true;
        }
    }
    //获取队首元素
    Object Front(){
        if(isEmpty()){
            return null;
        }else {
            return head;
        }
    }

实现一个最小栈

class MinStackDemo {
    Stack<Integer> stackMin = new Stack<>();
    Stack<Integer> stackAll = new Stack<>();

    //入栈 和最小栈里面的元素比较,如果是小的就进小的和全部。如果不是,只进去全部
    void push(int value) {
        stackAll.push(value);
        if (stackMin.empty()) {
            stackMin.push(value);
        } else {
            if (stackMin.peek() > value) {
                stackMin.pop();
                stackMin.push(value);
            }else if(stackMin.peek()==value){
                stackMin.push(value);
            }
        }
    }

    //出栈
    void pop(){
          if(stackAll.peek()==stackMin.peek()){
              stackMin.pop();
              stackAll.pop();
          }else {
              stackAll.pop();
          }
    }
    //最顶上元素
    int peek(){
        return stackAll.peek();
    }
    //最小值
    int getMIn(){
        return stackMin.peek();
    }

}

判断一个字符串中的括号是否匹配

 static  boolean isRight(String str) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            switch (ch) {
                case '(':
                case '{':
                case '[':
                    stack.push(ch);
                    break;
                case ')':
                    if (stack.empty()) {
                        return false;
                    } else {
                        if (stack.peek() == '(') {
                            stack.pop();
                            break;
                        } else {
                            return false;
                        }
                    }
                case '}':
                    if (stack.empty()) {
                        return false;
                    } else {
                        if (stack.peek() == '{') {
                            stack.pop();
                            break;
                        } else {
                            return false;
                        }
                    }
                case ']':
                    if (stack.empty()) {
                        return false;
                    } else {
                        if (stack.peek() == '[') {
                            stack.pop();
                            break;
                        } else {
                            return false;
                        }
                    }
                default:
                    break;
            }
        }
        if (stack.empty()) {
            return true;
        } else {
            return false;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值