队列的基本思想是:先入队列的,先出队列
一、链表实现队列
public class MyLinkedList {
class Node{
public int val;
public Node next;
public Node(int val){
this.val = val;
}
}
public Node head;
public Node last;
public int usedSize;
//入队
public void offer(int val){
Node node = new Node(val);
if(head == null){
head = node;
last = node;
}else{
last.next = node;
last = node;
}
this.usedSize++;
}
//出队
public int poll(){
if(isEmpty()){
throw new RuntimeException("队列为空");
}else {
int val = head.val;
head = head.next;
if(head == null){
last = null;
}
usedSize--;
return val;
}
}
public boolean isEmpty(){
return usedSize == 0;
}
//得到队头元素
public int peek(){
if(isEmpty()){
throw new RuntimeException("队列为空");
}else {
return head.val;
}
}
}
二、数组实现队列
用数组实现队列,需要用到循环的思想。
622. 设计循环队列 - 力扣(LeetCode)https://leetcode.cn/problems/design-circular-queue/submissions/
class MyCircularQueue {
public int[] elem;
public int Front = 0;
public int Rear = 0;
public MyCircularQueue(int k) {
elem = new int[k+1];
}
//入队
//1.判断是否满了
//2.把当前需要存放的元素,放到rear下标的地方
public boolean enQueue(int value) {
if(isFull()){
return false;
}else{
elem[Rear] = value;
Rear = (Rear+1)%elem.length;
}
return true;
}
//出队
public boolean deQueue() {
if(isEmpty()){
return false;
}else{
Front = (Front+1)%elem.length;
return true;
}
}
//得到队头元素
public int Front() {
if(isEmpty()){
return -1;
}else{
return elem[Front];
}
}
//得到队尾元素
public int Rear() {
if(isEmpty()){
return -1;
}else{
int index = (Rear==0) ? (elem.length-1):(Rear-1);
return elem[index];
}
}
public boolean isEmpty() {
return Rear == Front;
}
public boolean isFull() {
if((Rear+1) % elem.length == Front){
return true;
}
return false;
}
}
三、习题
1、用队列实现栈225. 用队列实现栈 - 力扣(LeetCode)https://leetcode.cn/problems/implement-stack-using-queues/submissions/
class MyStack {
private Queue<Integer> q1;
private Queue<Integer> q2;
public MyStack() {
q1 = new LinkedList<>();
q2 = new LinkedList<>();
}
public void push(int x) {
if(!q1.isEmpty()){
q1.offer(x);
}else if(!q2.isEmpty()){
q2.offer(x);
}else{
q1.offer(x);
}
}
public int pop() {
if(empty()){
return -1;
}
if(!q1.isEmpty()){
//出这个不为空的队列,出size-1个
int size = q1.size();
for(int i=0;i< size-1;i++){
int tmp = q1.poll();
q2.offer(tmp);
}
return q1.poll();
}else{
int size = q2.size();
for(int i=0;i < size-1;i++){
int tmp = q2.poll();
q1.offer(tmp);
}
return q2.poll();
}
}
public int top() {
if(empty()){
return -1;
}
if(!q1.isEmpty()){
int size = q1.size();
//将tmp定义到for循环外面,就不是局部变量了,就可以拿到最后一次弹出的元素的值
int tmp = 0;
for(int i=0;i < size;i++){
tmp = q1.poll();
q2.offer(tmp);
}
return tmp;
}else{
int size = q2.size();
int tmp = 0;
for(int i=0;i<size;i++){
tmp = q2.poll();
q1.offer(tmp);
}
return tmp;
}
}
public boolean empty() {
if(q1.isEmpty() && q2.isEmpty()){
return true;
}
return false;
}
}
2、用栈实现队列
232. 用栈实现队列 - 力扣(LeetCode)https://leetcode.cn/problems/implement-queue-using-stacks/submissions/
class MyQueue {
private Stack<Integer> s1;
private Stack<Integer> s2;
public MyQueue() {
s1 = new Stack<>();
s2 = new Stack<>();
}
public void push(int x) {
s1.push(x);
}
//如果第二个栈不为空,则出栈顶元素。否则,将第一个栈的元素全部导入第二个栈
public int pop() {
//两个栈都是空的
if(empty()){
return -1;
}
if(s2.empty()){
while(!s1.empty()){
int tmp = s1.pop();
s2.push(tmp);
}
}
int x = s2.pop();
return x;
}
public int peek() {
if(empty()){
return -1;
}
if(s2.empty()){
while(!s1.empty()){
int tmp = s1.pop();
s2.push(tmp);
}
}
return s2.peek();
}
public boolean empty() {
if(s1.empty() && s2.empty()){
return true;
}
return false;
}
}
3、最小栈
155. 最小栈 - 力扣(LeetCode)https://leetcode.cn/problems/min-stack/submissions/
class MinStack {
private Stack<Integer> s1;
private Stack<Integer> minStack;
public MinStack() {
s1 = new Stack<>();
minStack = new Stack<>();
}
public void push(int val) {
s1.push(val);
if(minStack.empty()){
minStack.push(val);
}else{
//这里一定要取等号
if(minStack.peek()>=val){
minStack.push(val);
}
}
}
public void pop() {
int x = s1.pop();
int x2 = minStack.peek();
if(x == x2){
minStack.pop();
}
}
public int top() {
return s1.peek();
}
public int getMin() {
return minStack.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/