数据结构(java)栈与队列

栈:(先进后出)

入栈:

1.普通栈一定要放、最小栈放的原则是:

*如果最小栈是空的,那么放

*如果最小栈的栈顶元素没有当前的元素小,则放

2.如果要放的的元素小于等于最小栈栈顶元素可以放吗?放

出栈:

需要判断 出栈的元素 和 栈顶元素是否相同,相同则最小栈也要出栈

队列:(先进先出)

单链表实现队列:

java 复制代码
public class MyQueue {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode first = null;
    public ListNode last = null;

    public int usedSize = 0;

    public void offer(int val) {
        ListNode node = new ListNode(val);
        if(isEmpty()) {
            first = last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = last.next;
        }
        usedSize++;
    }

    public int poll() {
        if(isEmpty()) {
            return -1;
        }
        int val = first.val;
        first = first.next;
        if(first != null) {
            first.prev = null;
        }
        usedSize--;
        return val;
    }

    public int peek() {
        if(isEmpty()) {
            return -1;
        }
        return first.val;
    }

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

}

设置循环队列:

java 复制代码
class MyCircularQueue {
    public int front;
    public int rear;
    public int[] elem;

    public MyCircularQueue(int k) {
        elem = new int[k+1];
    }
    
    //入队列 
    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }
        elem[rear] = value;
        rear = (rear+1)%elem.length;
        return true;
    }
    //出队列 
    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        front = (front+1)%elem.length;
        return true;
    }
    //得到队头元素 
    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return elem[front];
    }
    
    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        int index = (rear == 0) ? elem.length-1 : rear-1;
        return elem[index];
    }
    
    public boolean isEmpty() {
        return rear == front;
    }
    
    public boolean isFull() {
        return (rear+1)%elem.length == front;
    }
}

用队列实现栈:

java 复制代码
import java.util.LinkedList;
import java.util.Queue;

    class MyStack {
        private Queue<Integer> queue;

        public MyStack() {
            queue = new LinkedList<>();
        }

        public void push(int x) {
            // 每次push时,将新元素加入队列,然后将前面的元素依次出队再入队
            // 这样新元素就在队列前端,模拟了栈的后进先出特性
            queue.offer(x);
            int size = queue.size();
            for (int i = 0; i < size - 1; i++) {
                queue.offer(queue.poll());
            }
        }
        public int pop() {
            if (empty()) {
                throw new RuntimeException("Stack is empty");
            }
            return queue.poll();
        }

        public int top() {
            if (empty()) {
                throw new RuntimeException("Stack is empty");
            }
            return queue.peek();
        }

        public boolean empty() {
            return queue.isEmpty();
        }
    }

用栈实现队列:、

java 复制代码
import java.util.ArrayDeque;
class MyQueueUseStack {
        public ArrayDeque<Integer> stack1;
        public ArrayDeque<Integer> stack2;
        public MyQueueUseStack() {
            stack1 = new  ArrayDeque<>();
            stack2 = new  ArrayDeque<>();
        }

        public void push(int x) {
            stack1.push(x);
        }

        public int pop() {
            if(empty()) {
                return -1;
            }
            if(stack2.isEmpty()) {
                //第一个栈里面所有的元素 放到第二个栈当中
                while(!stack1.isEmpty()) {
                    stack2.push(stack1.pop());
                }
            }
            return stack2.pop();
        }

        public int peek() {
            if(empty()) {
                return -1;
            }
            if(stack2.isEmpty()) {
                //第一个栈里面所有的元素 放到第二个栈当中
                while(!stack1.isEmpty()) {
                    stack2.push(stack1.pop());
                }
            }
            return stack2.peek();
        }

        public boolean empty() {
            return stack1.isEmpty() && stack2.isEmpty();
        }
    }
相关推荐
考虑考虑3 小时前
JDK27结构化并发
java·后端·java ee
Cosolar5 小时前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
yiangL_7 小时前
课后作业4:基于 Python 的古诗词词云可视化(P205)
开发语言·python
geovindu9 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
skywalk816310 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
敲上瘾10 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
听情歌落俗11 小时前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
小飞在努力12 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
落羽的落羽13 小时前
【C++】现代容器: tuple、optional、variant、any、string_view
linux·开发语言·数据结构·c++·人工智能·计算机网络·算法
会编程的吕洞宾14 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring