• 队列、环形队列(用数组实现)


      1 package com.aixuexi.contact;
      2 
      3 public class CirArrQunue {
      4     public static void main(String[] args) {
      5         /*
      6          * 2019/7/15 15点58分
      7          * 2个问题   几个算法公式
      8          * 
      9          * 
     10          *     (rear + 1) % maxSize == front 判断队列是否满
     11          *     rear = 4      屁股
     12          *     front = 0     头
     13          *     maxSize = 5    最大数
     14          *  (4 + 1) % 5 = 0
     15          * 
     16          * 
     17          *  rear == front
     18          *     默认 都为0
     19          * 
     20          * 
     21          *     (rear + 1) % maxSize
     22          *     添加元素 尾指针+1
     23          *     默认  rear = 0 
     24          *     maxSize = 5    最大数
     25          *     rear = (rear + 1) % 5
     26          * 
     27          * 
     28          *     (front + 1) % maxSize
     29          *     front = (front + 1) % maxSize; //将 front 后移, 考虑取模
     30          *     默认front = 0     头
     31          *     front = (0 + 1) % 5         front == 1
     32          * 
     33          * 
     34          *     (rear + maxSize - front) % maxSize    算个数
     35          *     rear = 3
     36          *     maxSize = 5
     37          *     front  = 0 没弹出数据
     38          *     value = (3 + 5 - 0)%5   valiue == 3
     39          * 
     40          * 
     41          *     显示队列
     42          *     for(int i = front; i <front + Size(); i++) { //front + Size()     去掉为什么不行
     43          * 
     44          * 42行   75 行
     45          */
     46         
     47         CircleArray queue = new CircleArray(5);   //队列有四个元素
     48         
     49         System.out.println(queue.isFull());
     50         System.out.println(queue.isEmpty());
     51         
     52         System.out.println(queue.addQueue(7));
     53         System.out.println(queue.addQueue(8));
     54         System.out.println(queue.addQueue(1));
     55         System.out.println(queue.addQueue(9));
     56         queue.Arrshow();
     57         
     58         System.out.println(queue.getQueue());
     59         queue.Arrshow();
     60         
     61         System.out.println(queue.getQueue());
     62         queue.Arrshow();
     63         //System.out.println(queue.headQunue());
     64         
     65     }
     66 }
     67 
     68 class CircleArray{
     69     
     70     private int maxSize;
     71     private int front;
     72     private int rear;
     73     private int[] arr;
     74     //设置队列有多少个元素
     75     public CircleArray(int arrmaxSize) {
     76         // 注意: 不要重复定义了 int maxSize 不对 会重新产生变量
     77         maxSize = arrmaxSize;
     78         arr = new int[maxSize];
     79     }
     80     
     81     //判断队列是否满
     82     public boolean isFull() {
     83         return (rear + 1) % maxSize == front;
     84     }
     85     //判断队列是否空
     86     public boolean isEmpty() {
     87         return rear == front;
     88     }
     89     //添加元素
     90     public int addQueue(int n) {
     91         if(isFull()) {
     92             return -1;
     93         } 
     94         arr[rear] = n;
     95         rear = (rear + 1) % maxSize;//
     96         return 1;        
     97     }
     98     //弹出队列的第一个元素
     99     public int getQueue() {
    100         if(isEmpty()) {
    101             return -1;
    102         } else {
    103             int value = arr[front];
    104             front = (front + 1) % maxSize; //将 front 后移, 考虑取模
    105             return value;
    106         }
    107     }
    108     //显示队列
    109     public void Arrshow() {
    110         for(int i = front; i <front + Size(); i++) { //front + Size()     去掉为什么不行
    111             System.out.printf("arr[%d]=%d
    ", i % maxSize, arr[i % maxSize]);
    112         }
    113     }
    114     //大小 队列
    115     public int Size() {
    116         return (rear + maxSize - front) % maxSize;//
    117     }
    118     //返回队列头的数值
    119     public int headQunue() {
    120         if(isEmpty()) {
    121             return -1;
    122         }
    123         return arr[front];
    124     }
    125 }
  • 相关阅读:
    string基本字符系列容器(一)
    HDU 1541 star (树状数组)
    NKOI 1321--数列操作问题(裸BIT)
    树状数组(BIT)初学
    vector向量容器
    C++ STL概述
    2015年,为ACM奋斗的一年
    kuangbin带你飞,矩阵(简单数学推导题)
    hust 1009 Sum the K-th
    hust 1223 Friends
  • 原文地址:https://www.cnblogs.com/yaozhenhua/p/11190966.html
Copyright © 2020-2023  润新知