• 用数组实现的队列


    public class Queue {
        private int maxSize;//数组大小
        private long[] queArray;
        private int front;//队头
        private int rear;//队尾
        private int nItems;//数据项个数
        
        //create Queue
        public Queue(int s){
            maxSize = s;
            queArray = new long[s];
            front = 0;
            rear = -1;
            nItems = 0;
        }
        
        //insert Queue
        public void insert(long j){
            if(rear == maxSize-1)//循环
                rear = -1;
            queArray[++rear] = j;
            nItems++;
        }
        
        //remove Queue
        public long remove(){
            long temp = queArray[front++];
            if(front == maxSize)
                front = 0;
            nItems--;
            
            return temp;
        }
        
        public long peedFront(){
            return queArray[front];
        }
        
        public boolean isEmpty(){
            return nItems == 0;
        }
        
        public boolean isFull(){
            return nItems == maxSize;
        }
        
        public int ItemSize(){
            return nItems;
        }
    }
    public class QueueApp {
        public static void main(String[] args) {
            Queue theQueue = new Queue(5);
            theQueue.insert(10);
            theQueue.insert(20);
            theQueue.insert(30);
            theQueue.insert(40);// rear 3 nItems 4
            
            theQueue.remove();
            theQueue.remove();
            theQueue.remove();// front 3 nItems 1
            
            theQueue.insert(50);
            theQueue.insert(60);
            theQueue.insert(70);
            theQueue.insert(80);// rear 2 nItems 5
            
            while(!theQueue.isEmpty()){
                System.out.println(theQueue.remove());
            }
        }
    }

    队列

      计算机科学中,队列是一种数据结构,有点类似栈,只是在队列中第一个被插入的数据项也会被最先移除(FIFO),而在栈中,最后插入的数据项最先移除(LIFO)。

  • 相关阅读:
    缓存概念用法理解
    shiro
    Java生成验证码并进行验证(转)
    Java 8 Optional类深度解析(转)
    jdk8 stream可以与list,map等数据结构互相转换
    Java8初体验(二)Stream语法详解(转)
    Java 8 Optional 类
    cors跨域深刻理解
    httpclient连接池
    [C++] [算法] KMP算法
  • 原文地址:https://www.cnblogs.com/yony/p/2877702.html
Copyright © 2020-2023  润新知