队列-java代码

public class QueueDemo { private int maxSize; long[] queueArray; // 队列的头,实际是数组的尾 header; 队列的尾,实际是数组的头 footer; nElems; public QueueDemo( size){ maxSize = size; queueArray = new [maxSize]; header = 0; footer = -1; n

public class QueueDemo {

    private int maxSize;
    long[] queueArray;
    // 队列的头,实际是数组的尾
     header;
     队列的尾,实际是数组的头
     footer;
     nElems;
    
    public QueueDemo( size){
        
        maxSize = size;
        queueArray = new [maxSize];
        header = 0;
        footer = -1;
        nElems = 0;
    }
     插入队列,从队尾插入
    void insert( element){
        
        if(footer == (maxSize - 1)){
            footer = -1;
        }
        queueArray[++footer] = element;
        nElems++ 出队列,即取出数组尾
     remove(){
        
        long temp = queueArray[header];
        queueArray[header] = 0;
        if(++header == maxSize){
            header = 0;
        }
        nElems--return temp;
    }
     取出队列头元素
     getHeader(){
        
         queueArray[header];
    }
     取出队列数组
    [] getArray(){
        
         queueArray;
    }
}

队列是先进先出,可以想象成火车进隧道

?

优先级队列:

import java.util.Arrays;

/**
 * 优先级数组
 * 
 * @author Orlion
 * @create 2015-09-12
 */
 PriorityQDemo {

    [] priorityqArray;
     footer;
    
    public PriorityQDemo( size;
        priorityqArray =  插入优先级数组
    int i = footer;
        while(i > 0 && priorityqArray[i] > element){
            priorityqArray[i + 1] = priorityqArray[i];
            i--;
        }
        priorityqArray[i+1] = element;
        footer++ 出队列
     priorityqArray[header];
        priorityqArray[header] = 0;
        }
         打印队列
    void getPriorityq(){
        System.out.print(Arrays.toString(priorityqArray));
    }
}

可以理解成有一定顺序的队列,也是先进先出

关于作者: dawei

【声明】:石家庄站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐