资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

javaqueue-创新互联

// Queuequeue = new LinkedList<>();

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、成都网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的五大连池网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

queue.add() 入队列 (满的队列中加入新的元素时)调用 add() 方法就会抛出一个 unchecked 异常

queue.offer() 入队列 (满的队列中加入新的元素时) 调用 offer() 方法会返回 false

int pop() 从队列的开头移除并返回元素

int peek() 返回队列开头的元素

boolean empty() 如果队列为空,返回 true ;否则,返回 false

void push(int x) 将元素 x 压入栈顶。

int top() 返回栈顶元素。

MyCircularQueue(k): 构造器,设置队列长度为 k 。

Front: 从队首获取元素。如果队列为空,返回 -1 。

Rear: 获取队尾元素。如果队列为空,返回 -1 。

enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。

deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。

isEmpty(): 检查循环队列是否为空。

isFull(): 检查循环队列是否已满。

public class MyQueue {
 
    private class Node {
        private int val; //数据域
        private Node next; //指针域名
 
        private Node(int val) {
            this.val = val;
        }
    }
    //队头入,队尾出
    private Node head; //队头引用
    private Node last; //队尾引用
    private int size; //队列有效数据个数
}
// 队尾入队列
public boolean offer(int val) {
    Node newNode = new Node(val);
    // 如果是第一次入队列
    if (this.head == null) {
        this.head = newNode;
        this.last = newNode;
    } else {
        this.last.next = newNode;
        this.last = this.last.next;
    }
    this.size++;
    return true;
}
// 队头出队列
public int poll() {
    Node node = this.head;
    // 如果队列为null
    if (this.head == null) {
        throw new MyQueueIsEmptyException("队列为空!");
    } else {
        this.head = this.head.next;
    }
    this.size--;
    return node.val;
}
// 取队头元素
public int peek() {
    // 如果队列为null
    if (this.head == null) {
        throw new MyQueueIsEmptyException("队列为空!");
    } else {
        return this.head.val;
    }
}
public class MyCircularQueue {
 
    private int array[]; //存放数据的数组
    private int front; // 队头下标
    private int rear; // 队尾下标
 
    public MyCircularQueue(int k) {
        this.array = new int[k + 1]; //因为我们要浪费一个空间,所以需要多开辟一个空间
    }
 
    // 入队列
    public boolean enQueue(int value) {
        // 如果队列满的情况
        if (isFull()) {
            return false;
        }
        // 没有满就往队尾入元素
        this.array[this.rear] = value;
        // rear往前走一步,需要空出一个位置,所以当rear走到length-1时,需要修正下rear
        this.rear = (this.rear + 1) % this.array.length;
        return true;
    }
 
    // 出队列
    public boolean deQueue() {
        // 如果队列为null的情况
        if (isEmpty()) {
            return false;
        }
        // 从队头出队列,需要修正队头的位置
        this.front = (this.front + 1) % this.array.length;
        return true;
    }
    
    // 取队头元素
    public int Front() {
        // 如果队列为null的情况
        if (isEmpty()) {
            return -1;
        }
        return this.array[this.front]; //返回队头元素
    }
 
    // 取队尾元素
    public int Rear() {
        // 如果队列为null的情况
        if (isEmpty()) {
            return -1;
        }
        // 如果rear为0的情况,我们需要特殊处理
        int index = this.rear == 0 ? this.array.length - 1 : this.rear - 1;
        return this.array[index]; //返回队尾元素
    }
 
    // 判断队列是否为空
    public boolean isEmpty() {
        // 当front和rear相遇了,则表示队列中没有元素
        return this.front == this.rear;
    }
 
    // 判断队列是否满了
    public boolean isFull() {
        // 因为我们会浪费一个空间,所以rear+1等于front就满了
        // 但是我们要rear防止越界,所以要进行修正:(this.rear + 1) % this.array.length
        return (this.rear + 1) % this.array.length == this.front;
    }
}
class MyStack {
    private Queuequ1;
    private Queuequ2;
 
    public MyStack() {
        this.qu1 = new LinkedList<>();
        this.qu2 = new LinkedList<>();
    }
    
    public void push(int x) {
        // 两个队列都为null的情况
        if (this.qu1.isEmpty() && this.qu2.isEmpty()) {
            this.qu1.offer(x);
            return;
        }
 
        // 哪个队列不为null往哪个队列中入元素
        if (!this.qu1.isEmpty()) {
            this.qu1.offer(x);
        } else {
            this.qu2.offer(x);
        }
    }
    
    public int pop() {
        // 如果两个队列都为空的情况下,就不能出队操作
        if (empty()) {
            return -1;
        }
        // 先将不为空的队列的size-1个元素全部放到为空的队列中
        if (!this.qu1.isEmpty()) {
            while (qu1.size() - 1 != 0) {
                qu2.offer(qu1.poll());
            }
            return qu1.poll(); //返回最后一个元素
        } else {
            while (qu2.size() - 1 != 0) {
                qu1.offer(qu2.poll());
            }
            return qu2.poll(); //返回最后一个元素
        }
    }
    
    public int top() {
        // 如果队列为null
        if (empty()) {
            return -1;
        }
        int ret = 0; //保留剩余最后一个栈顶元素的变量
        // 先将不为空的队列的size-1个元素全部放到为空的队列中
        if (!this.qu1.isEmpty()) {
            while (qu1.size() - 1 != 0) {
                qu2.offer(qu1.poll());
            }
            ret = qu1.peek();
            qu2.offer(qu1.poll());
        } else {
            while (qu2.size() - 1 != 0) {
                qu1.offer(qu2.poll());
            }
            ret = qu2.peek();
            qu1.offer(qu2.poll()); //取栈顶元素不能删除掉,还得入另一个队列中去
        }
        return ret;
    }
    
    public boolean empty() {
        return this.qu1.isEmpty() && this.qu2.isEmpty(); //两个队列都为空,栈才为空
    }
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站题目:javaqueue-创新互联
标题网址:http://cdkjz.cn/article/ddpjph.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220