• 数据结构之队列


    队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。先进先出(FIFO—first in first out)

    队列分为普通队列和环形队列

    这里说说环形队列

      1 package song.queue;
      2 
      3 /**
      4  * Created by sfz on 2017/8/25.
      5  */
      6 public class MyQueue {
      7 
      8     private int[] data;
      9 
     10     //队列数组的长度
     11     private int size = 0;
     12     //对头的下标
     13     private int head = 0;
     14     //队尾的下标
     15     private int tail = 0;
     16     //队列实际 的个数
     17     private int queuelen = 0;
     18 
     19     //初始化队列
     20     public MyQueue(int size) {
     21         this.size = size;
     22         data = new int[size];
     23         clearQueue();
     24     }
     25 
     26 
     27     /**
     28      * 清空队列
     29      */
     30     public void clearQueue() {
     31         head = 0;
     32         tail = 0;
     33         queuelen = 0;
     34     }
     35 
     36     /**
     37      * 队列判空
     38      *
     39      * @return
     40      */
     41     public boolean isEmpty() {
     42         return queuelen == 0;
     43     }
     44 
     45     /**
     46      * 实际长度
     47      *
     48      * @return
     49      */
     50     public int queueLength() {
     51         return queuelen;
     52     }
     53 
     54     /**
     55      * 判满
     56      *
     57      * @return
     58      */
     59     public boolean isfull() {
     60         return queuelen == size;
     61     }
     62 
     63     /**
     64      * 入队
     65      *
     66      * @param ele
     67      * @return
     68      */
     69     public boolean enQueue(int ele) {
     70         if (isfull()) {
     71             return false;
     72         } else {
     73             data[tail] = ele;
     74             tail++;
     75             tail = tail % size;
     76             queuelen++;
     77             return true;
     78         }
     79     }
     80 
     81 
     82     /**
     83      * 出对
     84      *
     85      * @param ele
     86      * @return
     87      */
     88     public boolean deQqueue(int ele) {
     89         if (isEmpty()) {
     90             return false;
     91         } else {
     92             data[head] = 0;
     93             head++;
     94             head = head % size;
     95             queuelen--;
     96             return true;
     97         }
     98 
     99 
    100     }
    101 
    102     /**
    103      * 遍历
    104      */
    105     public void traverse() {
    106         for (int i = head; i < queuelen+head; i++) {
    107             int val = data[i % size];
    108             System.out.print(val+ " ");
    109         }
    110         System.out.println();
    111     }
    112 
    113 }
    View Code
     1 package song.queue;
     2 
     3 import org.junit.Test;
     4 import song.tree.Tree;
     5 
     6 /**
     7  * Created by sfz on 2017/8/25.
     8  */
     9 public class Test001 {
    10 
    11     @Test
    12     public void test001() throws Exception {
    13 
    14         MyQueue myQueue = new MyQueue(4);
    15 
    16         myQueue.enQueue(10);
    17         myQueue.enQueue(20);
    18         myQueue.enQueue(30);
    19         myQueue.enQueue(40);
    20 
    21         myQueue.traverse();
    22 
    23 
    24 
    25         myQueue.deQqueue(0);
    26         myQueue.deQqueue(0);
    27         myQueue.traverse();
    28 
    29 
    30 
    31 
    32         myQueue.enQueue(50);
    33         myQueue.enQueue(60);
    34         myQueue.traverse();
    35 
    36         myQueue.clearQueue();
    37         myQueue.traverse();
    38 
    39 
    40         myQueue.enQueue(70);
    41         myQueue.enQueue(80);
    42         myQueue.traverse();
    43 
    44 
    45 
    46     }
    47 }
    View Code
  • 相关阅读:
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    C语言I博客作业02
    C语言II博客作业04
    C语言II博客作业03
    C语言II博客作业02
    C语言II博客作业01
    学期总结
    C语言I博客作业08
  • 原文地址:https://www.cnblogs.com/songfahzun/p/7440403.html
Copyright © 2020-2023  润新知