使用C語言來解決循環(huán)隊列問題的方法
題目描述:
大家都知道數(shù)據(jù)結(jié)構(gòu)里面有一個結(jié)構(gòu)叫做循環(huán)隊列。顧名思義,這是一個隊列,并且是循環(huán)的。但是現(xiàn)在,淘氣的囧哥給這個循環(huán)隊列加上了一些規(guī)矩,其中有5條指令:
(1) Push K, 讓元素K進(jìn)隊列。
(2) Pop,對頭元素出隊列。
(3) Query K,查找隊列中第K個元素,注意K的合法性。
(4) Isempty,判斷隊列是否為空。
(5) Isfull,判斷隊列是否已滿。
現(xiàn)在有N行指令,并且告訴你隊列大小是M。
輸入:
第一行包含兩個整數(shù)N和M。1<=N,M<=100000。
接下來有N行,表示指令,指令格式見題目描述。
其中元素均在int范圍。
輸出:
對于指令(1),若隊列已滿,輸出failed,否則不做輸出。
對于指令(2),若隊列已空,輸出failed,否則不做輸出。
對于指令(3),輸出隊列中第K個元素,若不存在,輸出failed。
對于指令(4)和(5),則用yes或者no回答。
詳情見樣例。
樣例輸入:
12 2Push 1Push 2Push 3Query 2Query 3IsemptyIsfullPopPopPopIsemptyIsfull
樣例輸出:
failed2failednoyesfailedyesno
AC代碼:
#include <stdio.h> 
  #include <stdlib.h> 
  #include <string.h> 
   
  #define queuesize 100001  //最大隊列長度 
   
  struct queue 
  { 
    int front; 
    int rear; 
    int data[queuesize]; 
    int count; //記錄隊列中的元素 
  }; 
   
  void InitQueue(struct queue *Q); 
  void EnQueue(struct queue *Q, int element, int m); 
  void Dequeue(struct queue *Q, int m); 
  void QueueSearch(struct queue *Q, int k, int m); 
   
  int main() 
  { 
    int n, m, i, element, k, flag; 
    char command[10]; 
   
    while(scanf("%d%d",&n, &m) != EOF) 
    { 
      if(n < 1 || m > 100000) 
        return 0; 
      struct queue *Q; 
      Q = malloc(sizeof(struct queue)); 
      InitQueue(Q); 
      for(i = 0; i < n; i ++) 
      { 
        scanf("%s",command); 
        if (strcmp(command,"Push") == 0) 
        { 
          scanf("%d",&element); 
          EnQueue(Q, element, m); 
        }else if (strcmp(command,"Pop") == 0) 
        { 
          Dequeue(Q, m); 
        }else if (strcmp(command,"Query") == 0) 
        { 
          scanf("%d",&k); 
          QueueSearch(Q, k, m); 
        }else if (strcmp(command,"Isempty") == 0) 
        { 
          flag = (Q -> count == 0)? 1 : 0; 
          if(flag) 
          { 
            printf("yes\n"); 
          }else 
          { 
            printf("no\n"); 
          } 
        }else if (strcmp(command,"Isfull") == 0) 
        { 
          flag = (Q -> count == m)? 1 : 0; 
          if(flag) 
          { 
            printf("yes\n"); 
          }else 
          { 
            printf("no\n"); 
          } 
        } 
      } 
    }   
    return 0; 
  } 
   
  /** 
   * Description:隊列初始化 
   */ 
  void InitQueue(struct queue *Q) 
  { 
    Q -> front = Q -> rear = 0; 
    Q -> count = 0; 
  } 
   
  /** 
   * Description:入隊操作 
   */ 
  void EnQueue(struct queue *Q, int element, int m) 
  { 
    int flag; 
    flag = (Q -> count == m)? 1 : 0;  
   
    if(!flag) 
    { 
      Q -> data[Q -> rear] = element; 
      Q -> count ++; 
      Q -> rear = (Q -> rear + 1) % m; 
    }else 
    { 
      printf("failed\n"); 
    } 
  } 
   
  /** 
   * Description:出隊操作 
   */ 
  void Dequeue(struct queue *Q, int m) 
  { 
    int flag; 
    int element; 
   
    flag = (Q -> count == 0)? 1 : 0; 
   
    if(!flag) 
    { 
      element = Q -> data[Q -> front]; 
      Q -> front = (Q -> front + 1) % m; 
      Q -> count --; 
    }else 
    { 
      printf("failed\n"); 
    } 
  } 
   
  /** 
   * Description:查找隊列中的指定元素 
   */ 
  void QueueSearch(struct queue *Q, int k, int m) 
  { 
    int flag, temp; 
    flag = (Q -> count == 0)? 1: 0; 
    temp = Q -> front + k - 1; 
    if((!flag) && (k <= m && k >= 1)) 
    { 
      if((Q -> front < Q -> rear) && ( Q-> front <= temp && Q -> rear > temp)) 
        printf("%d\n",Q -> data[temp]); 
      else if((Q -> front > Q -> rear) && (temp >= Q -> front || temp < Q->rear)) 
        printf("%d\n",Q -> data[temp]); 
      else if(Q -> front == Q -> rear) 
        printf("%d\n",Q -> data[temp]); 
      else 
        printf("failed\n"); 
    }else 
    { 
      printf("failed\n"); 
    } 
  } 
上一篇:C語言中查找字符在字符串中出現(xiàn)的位置的方法
欄 目:C語言
下一篇:C語言中strspn()函數(shù)和strcspn()函數(shù)的對比使用
本文標(biāo)題:使用C語言來解決循環(huán)隊列問題的方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2869.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘
 


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 01-10C#中split用法實(shí)例總結(jié)
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10delphi制作wav文件的方法
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 04-02jquery與jsp,用jquery
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 


