zoukankan      html  css  js  c++  java
  • java实现队列的练习

    java用数组实现队列

     1 package com.fanmzdj.tools;
     2 
     3 public class Test17 {
     4     
     5     public static void main(String[] args) {    
     6         ArrayQueue queue = new ArrayQueue(4);
     7         queue.insert(10);
     8         queue.insert(20);
     9         queue.insert(30);
    10         queue.insert(40);
    11         queue.insert(50);// 队列已满,再插入时,不会插入没有提示
    12         queue.remove();// 删除,并返回删除的数据
    13         queue.insert(60);
    14         while(!queue.isEmpty()) {
    15             print(queue.remove());
    16         }
    17     }
    18     
    19     public static void print(Object o) {
    20         System.out.println(o);
    21     }
    22 }
    23 // 用数组实现队列
    24 class ArrayQueue {
    25     private int front;// 取数据的下标
    26     private int rear;// 存数据的下标
    27     private int nItems;// 记录队列存的数据个数
    28     private int maxSize;// 表示队列的大小
    29     private int[] queArr;// 用数组来存放队列数据
    30     
    31     public ArrayQueue(int size) {// 队列为空时,front、rear、nItems都是0,
    32         maxSize = size;
    33         queArr = new int[maxSize];
    34         front = 0;
    35         rear = 0;
    36         nItems = 0;
    37     }
    38     
    39     public void insert(int j) {
    40         if(isFull()) {
    41             return;
    42         }
    43         if(rear == maxSize) {
    44             rear = 0;
    45         }
    46         queArr[rear++] = j;
    47         nItems++;
    48     }
    49     
    50     public int remove() {
    51         int temp = queArr[front++];
    52         if(front == maxSize) {
    53             front = 0;
    54         }
    55         nItems--;
    56         return temp;        
    57     }
    58     
    59     public int peekFront() {
    60         return queArr[front];
    61     }
    62 
    63     private boolean isFull() {
    64         return (nItems == maxSize);
    65     }
    66 
    67     public boolean isEmpty() {
    68         return (nItems == 0);
    69     }
    70 }
    View Code
  • 相关阅读:
    ajax发送cookies
    关于几个入口
    关于.net的概念
    codeproject
    Chrome extension
    modern web application
    JavaScript类型转换
    js方法在iframe父子窗口
    javascript book
    MVC Controller
  • 原文地址:https://www.cnblogs.com/fanmzdj/p/3391207.html
Copyright © 2011-2022 走看看