zoukankan      html  css  js  c++  java
  • 用数组实现的队列

    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)。

  • 相关阅读:
    1755:菲波那契数列
    1788:Pell数列
    3089:爬楼梯
    7832:最接近的分数
    7649:我家的门牌号
    7216:Minecraft
    7213:垃圾炸弹
    2983:谁是你的潜在朋友
    2723:因子问题
    2722:和数
  • 原文地址:https://www.cnblogs.com/yony/p/2877702.html
Copyright © 2011-2022 走看看