• 【数据结构与算法之美】4.基于数组实现队列


    package com.ncst.queue;
    
    /**
     * @author i
     * @create 2019/12/20 15:14
     * @Description 利用数组实现队列
     *
     */
    public class ArrayQueue {
        private String [] data;//存储数据
        private Integer size = 0;//大小
        private Integer head = 0;//队列头
        private Integer tail = 0;//队尾
    
        //初始化
        public ArrayQueue(Integer capacity){
            data = new String[capacity];
            size = capacity;
        }
    
        //入队
        public String requene(String value){
            //如果队列为满
            if (tail == size){
                throw new RuntimeException("quene is full!");
            }
            data[tail] = value;
            tail++;
            return value;
        }
    
        //出队
        public String dequene(){
            //如果队列为空
            checkQueneIsEmpty();
            String value = data[head];
            head++;
            return value;
        }
    
        //打印所有
        public void printfAll(){
            checkQueneIsEmpty();
            for (int i = 0; i < tail ; i++) {
                System.out.print(data[i]+"	");
            }
            System.out.println();
        }
    
        private void checkQueneIsEmpty(){
            if (head == tail){
                throw  new RuntimeException("quene is empty!");
            }
        }
    
        public static void main(String[] args) {
            ArrayQueue arrayQueue = new ArrayQueue(5);
            arrayQueue.requene("1");
            arrayQueue.requene("2");
            arrayQueue.requene("3");
            arrayQueue.requene("4");
            arrayQueue.requene("5");
    
            arrayQueue.printfAll();
    
            System.out.println(arrayQueue.dequene());
            System.out.println(arrayQueue.dequene());
            System.out.println(arrayQueue.dequene());
    
        }
    
    }
    
  • 相关阅读:
    TH-Union教学机 指令总结
    Manjaro 显卡驱动安装
    grub学习内容
    manjaro 折腾
    链栈的实现
    汇编综合实验
    二叉树
    Oracle表空间基本操作
    Windows7/10实现ICMP(ping命令)
    WireShark——IP协议包分析(Ping分析IP协议包)
  • 原文地址:https://www.cnblogs.com/qxlxi/p/12860747.html
Copyright © 2020-2023  润新知