zoukankan      html  css  js  c++  java
  • java数据结构和算法------队列

     1 // 普通队列
     2 package iYou.neugle.list;
     3 
     4 public class MySeqQueue<T> {
     5     private SeqQueue queue = new SeqQueue();
     6 
     7     class SeqQueue {
     8         public int maxSize = 10;
     9         public T[] data = (T[]) new Object[this.maxSize];
    10         public int head = 0;// 头指针
    11         public int tail = 0;// 尾指针
    12     }
    13 
    14     // 初始化队列
    15     public void SeqQueueInit() {
    16         this.queue.head = 0;
    17         this.queue.tail = 0;
    18         System.out.println("队列初始化成功!");
    19     }
    20 
    21     // 出队
    22     public T SeqQueueOut() {
    23         if (this.queue.head == this.queue.tail) {
    24             System.out.println("队列已空,无法出队!");
    25             return null;
    26         }
    27         T data = this.queue.data[this.queue.head];
    28         this.queue.data[this.queue.head] = null;
    29         this.queue.head++;
    30         return data;
    31     }
    32 
    33     // 入队
    34     public void SeqQueueIn(T data) {
    35         if (this.queue.tail == this.queue.maxSize) {
    36             System.out.println("队列已满,无法入队!");
    37             return;
    38         }
    39         this.queue.data[this.queue.tail++] = data;
    40     }
    41 
    42     // 获取队头元素
    43     public T SeqQueuePeek() {
    44         if (this.queue.head == this.queue.tail) {
    45             System.out.println("队列已空,无法获取列头元素!");
    46             return null;
    47         }
    48 
    49         return this.queue.data[this.queue.head];
    50     }
    51 
    52     // 获取队列长度
    53     public int SeqQueueLen() {
    54         return this.queue.tail - this.queue.head;
    55     }
    56 }
     1 // 循环队列
     2 package iYou.neugle.list;
     3 
     4 public class MySeqLoopQueue<T> {
     5     private SeqQueue queue = new SeqQueue();
     6 
     7     class SeqQueue {
     8         public int maxSize = 10;
     9         public T[] data = (T[]) new Object[this.maxSize];
    10         public int head = 0;// 头指针
    11         public int tail = 0;// 尾指针
    12         public int size = 0;// 实际数
    13     }
    14 
    15     // 初始化队列
    16     public void SeqQueueInit() {
    17         this.queue.head = 0;
    18         this.queue.tail = 0;
    19         this.queue.size = 0;
    20         System.out.println("循环队列初始化成功!");
    21     }
    22 
    23     // 出队
    24     public T SeqQueueOut() {
    25         if (this.queue.size == 0) {
    26             System.out.println("循环队列已空,无法出队!");
    27             return null;
    28         }
    29         T data = this.queue.data[this.queue.head];
    30         this.queue.data[this.queue.head] = null;
    31         this.queue.size--;
    32         this.queue.head = (this.queue.head + 1) % this.queue.maxSize;
    33         return data;
    34     }
    35 
    36     // 入队
    37     public void SeqQueueIn(T data) {
    38         if (this.queue.size == this.queue.maxSize) {
    39             System.out.println("循环队列已满,无法入队!");
    40             return;
    41         }
    42         this.queue.data[this.queue.tail] = data;
    43         this.queue.size++;
    44         this.queue.tail = (this.queue.tail + 1) % this.queue.maxSize;
    45     }
    46 
    47     // 获取队头元素
    48     public T SeqQueuePeek() {
    49         if (this.queue.size == 0) {
    50             System.out.println("循环队列已空,无法获取列头元素!");
    51             return null;
    52         }
    53 
    54         return this.queue.data[this.queue.head];
    55     }
    56 
    57     // 获取队列长度
    58     public int SeqQueueLen() {
    59         return this.queue.size;
    60     }
    61 }
  • 相关阅读:
    Vue 异步组件按需加载
    Net core 连接 Minio 出现 Access Denied 错误
    vue 脚手架打包 生成时间戳版本号
    vue tab 切换动画
    想尽办法不出错的语言太可怕了
    .NET Core:处理全局异常
    C#获取当前路径的方法
    [C#]解决生成的缩略图模糊的问题
    C# 正则表达式 —— 中文/英文空格(全角/半角空格)处理
    用C# 7.0的switch...case模式匹配取代一堆if语句
  • 原文地址:https://www.cnblogs.com/niuxiaoha/p/4635976.html
Copyright © 2011-2022 走看看