1. 程式人生 > 實用技巧 >[2020HDU多校第一場][HDU 6751][A. Avian Darts]

[2020HDU多校第一場][HDU 6751][A. Avian Darts]

佇列,先來先服務,先進先出

具有一定操作約束的線性表
只能在一端插入,而在另一端刪除

操作集

  • Queue CreatQueue(int MaxSize):生成長度為MaxSize的佇列
  • int IsFullQ(Queue Q,int MaxSize):判斷佇列Q是否已滿
  • void AddQ(Queue Q,ElemenType item):將資料元素item插入佇列
  • int IsEmptyQ(Queue Q):判斷佇列是否為空
  • ElementType DeleteQ(Queue Q):將隊投元素從佇列中刪除並返回

佇列順序儲存實現

佇列的順序儲存結構通常由一個一維陣列和一個記錄佇列頭元素位置的變數front以及一個記錄佇列尾元素的變數rear組成

#define MaxSize <最大元素個數>
struct QNode{
      ElementType Data[MaxSize];
      int rear;
      int front;
};
typedef struct QNode *Queue;

環狀佇列

//入佇列
void AddQ(Queue PtrQ,ElementType item){
      if(PtrQ->rear+1) % MaxSize == PtrQ->front{
            printf("佇列滿");
            return ;
      }
      PtrQ->rear=(PtrQ->rear+1) % MaxSize;
      PtrQ->Data[PtrQ->rear]=item;
//出佇列
ElementType DeleteQ(Queue ptrQ){
      if(PtrQ->front==PtrQ->reat){
            printf("佇列空");
            return ERROR;
      }
      else{
            PtrQ->front=(PtrQ->front+1)%MaxSize;
            return PtrQ->Data[PtrQ->front];
      }
}