JavaSE_Queue/Stack模拟实现,C++/Java实现环形队列

JavaSE Queue/Stack

Stack底层是一个数组

import java.util.Arrays;

class MyStack<E>{
    private Object[] array;
    private int useSize;
    MyStack(){
        this.array=new Object[1];
        this.useSize=0;
    }
    private void dilatation(){
        this.array= Arrays.copyOf(this.array,this.array.length*2);
    }
    public void push(E num){
        if(array.length==this.useSize){
            dilatation();
            System.out.println("INFO:扩容成功");
        }
        array[useSize++]=num;
    }
    public Object pop(){
        if(isEmpty()){
            throw new RuntimeException("栈为空");
        }
        Object num=array[useSize-1];
        this.array[useSize-1]=null;
        this.useSize--;
        return num;
    }
    public boolean isEmpty(){
        return useSize==0;
    }
}

Queue底层是一个链表

class Node{
    public int val;
    public Node next;
    public Node(int val){
        this.val=val;
        next=null;
    }
}

class MyQueue{//实现普通队列
    private Node head;
    private Node last;
    public MyQueue(){
        this.head=null;this.last=null;
    }
    public void offer(int val){
        if(head==null){
            this.head=new Node(val);
            this.last=this.head;
        }
        else{
            Node newNode=new Node(val);
            this.last.next=newNode;
            this.last=newNode;
        }
    }
    public int poll(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        int oldval=head.val;
        if(head==last){
            this.last=null;
        }
        this.head=this.head.next;
        return oldval;
    }
    public boolean isEmpty(){
        return head==null;
    }
}

public class TestQueue {
    public static void main(String[] args) {
        MyQueue queue=new MyQueue();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
//        System.out.println(queue.head);
//        System.out.println(queue.last);
    }
}

环形队列

环形队列首尾指针移动规则:

front=(front+1)%array.length 来达到将数组抽象为环形的方法

判断空和满有三种策略

  1. 添加useSize判断满和空
  2. 添加标志位判断满和空
  3. 空开一个空间来判断空还是满

注意:方法2,3 获取环形队列尾部的方法时 last指向的是环形队列尾部的下一个位置。所以需要公式 (last+array.length-1)%array.length来计算last指向的上一个节点

C++环形队列

struct QueueNode{
    int val;
    bool isUsed;//标记位
    QueueNode():val(-1),isUsed(false){}
};

class MyCircularQueue {
    vector<QueueNode>vet;
    int front;
    int last;
public:
    MyCircularQueue(int k) {
        vet.resize(k);
        front=0;
        last=0;
    }
    
    bool enQueue(int value) {
        if(isFull()){
            return false;
        }
        vet[last].val=value;
        vet[last].isUsed=true;
        last=(last+1)%vet.size();
        return true;
    }
    
    bool deQueue() {
        if(isEmpty()){
            return false;
        }
        vet[front].isUsed=false;
        front=(front+1)%vet.size();
        return true;
    }
    
    int Front() {
        if(isEmpty()){
            return -1;
        }
        return vet[front].val;
    }
    
    int Rear() {
        if(isEmpty()){
            return -1;
        }
        return vet[(last+vet.size()-1)%vet.size()].val;
    }
    
    bool isEmpty() {
        return front==last&&vet[front].isUsed==false;
    }
    
    bool isFull() {
        return front==last&&vet[front].isUsed==true;
    }
};

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue* obj = new MyCircularQueue(k);
 * bool param_1 = obj->enQueue(value);
 * bool param_2 = obj->deQueue();
 * int param_3 = obj->Front();
 * int param_4 = obj->Rear();
 * bool param_5 = obj->isEmpty();
 * bool param_6 = obj->isFull();
 */

Java环形队列

class MyCircularQueue {
    private int[]arrry;
    private int front;
    private int last;
    public MyCircularQueue(int k) {
        this.arrry=new int[k+1];
        this.front=0;
        this.last=0;
    }
    
    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        this.arrry[this.last]=value;
        this.last=(this.last+1)%this.arrry.length;
        return true;
    }
    
    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        this.front=(this.front+1)%this.arrry.length;
        return true;
    }
    
    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return this.arrry[this.front];
    }
    
    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        return this.arrry[(this.last+this.arrry.length-1)%this.arrry.length];
    }
    
    public boolean isEmpty() {
        return this.last==this.front;
    }
    
    public boolean isFull() {
        return (this.last+1)%this.arrry.length==this.front;
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NUC_Dodamce

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值