<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          數(shù)據(jù)結(jié)構(gòu)--隊列--范例

          共 1215字,需瀏覽 3分鐘

           ·

          2024-03-22 14:30

           每天都可以成長一點點

          01


          public class ArrayQueue {
          //隊列容量
          public int size;
          //隊列頭部下標
          public int front;
          //隊列尾部下標
          public int rear;
          //數(shù)組
          public int arr[];

          public ArrayQueue(int size){
          this.size = size + 1; //默認留一個空地址
          this.front = 0;
          this.rear = 0;
          this.arr = new int[size+1];
          }

          /**
          * 是否滿隊
          * @return
          */
          public boolean isFull(){
          return (rear+1) % size == front;
          }

          /**
          * 是否空隊
          * @return
          */
          public boolean isEmpty(){
          return rear == front;
          }

          /**
          * 入隊
          * @param item
          */
          public void push(int item){
          if(isFull()){
          throw new RuntimeException("隊列已滿,入隊失敗");
          }
          arr[rear] = item;
          rear = (rear + 1) % size;
          }

          /**
          * 出隊
          * @return
          */
          public int pop(){
          if(isEmpty()){
          throw new RuntimeException("隊列為空");
          }
          int item = arr[front];
          front = (front + 1) % size;
          return item;
          }

          /**
          * 隊列長度
          * @return
          */
          public int length(){
          return (rear + size - front) % size;
          }

          /**
          * 顯示整個隊列
          */
          public void show(){
          if(isEmpty()){
          throw new RuntimeException("隊列為空");
          }
          for (int i = front; i < front+length(); i++) {
          System.out.print(arr[i%size]+"\t");
          }
          System.out.printf("\n");
          }

          public int getHeader(){
          if(isEmpty()){
          throw new RuntimeException("隊列為空");
          }
          return arr[front];
          }
          }




          瀏覽 27
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产 日韩 欧美视频在线 | 成年人精品视频 | 欧洲亚洲韩国在线观看 | 亚洲AV无码专区一级婬片毛片 | 日本不卡免费 |