使用C语言来解决循环队列问题的方法

2020-01-06 13:42:44王旭
  • 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("yesn");   }else 
  • {   printf("non");  
  • }   }else if (strcmp(command,"Isfull") == 0)  
  • {   flag = (Q -> count == m)? 1 : 0;  
  • if(flag)   {  
  • printf("yesn");   }else 
  • {   printf("non");  
  • }   }  
  • }   }  
  • 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