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

  • 相关阅读:
    前端
    wampserver无法进入到phpmyadmin
    wordpress搭载wampserver上的问题
    电脑不显示网络信号,却能连上网
    读书TODO
    秒杀活动,怎么设计全套技术方案
    淘宝返利知识普及
    如何给变量命名
    一段SQL代码的压缩:从974行到96行,十倍压缩
    nginx一致性hash及应用场景。
  • 原文地址:https://www.cnblogs.com/yony/p/2877702.html
Copyright © 2011-2022 走看看