C language to solve the circular queue problem

  • 2020-04-02 03:13:45
  • OfStack

Title description:

      You know there's a structure inside the data structure called a circular queue. As the name implies, this is a queue and is circular. But now, the naughty boy has added some rules to the circular queue, including five instructions:

      (1) Push K, let K into the queue.

      (2) Pop, the head element out of the queue.

      (3) Query K, find the KTH element in the queue, and pay attention to the validity of K.

      (4) Isempty, which determines whether the queue Isempty.

      (5) Isfull, judge whether the queue Isfull.

      Now I have N lines of instructions, and I'm telling you that the queue size is M.

Input:

      The first row contains two integers N and M. 1 < = N, M < = 100000.

      Next, there are N lines representing the instruction, and the instruction format is described in the title.

      Where the elements are in the int range.

Output:

      For instruction (1), if the queue is full, the output failed, otherwise the output is not done.

      For instruction (2), if the queue is empty, the output failed, otherwise the output is not done.

      For instruction (3), if the KTH element in the output queue does not exist, the output fails.

      For commands (4) and (5), answer with yes or no.

      See the sample for details.

Sample input:

      12 push push push 3 query 2 2 1 2 3 isemptyisfullpoppoppopisemptyisfull query

Sample output:

      failed2failednoyesfailedyesno

AC code:

     


#include <stdio.h> 
  #include <stdlib.h> 
  #include <string.h> 
   
  #define queuesize 100001  //Maximum queue length
   
  struct queue 
  { 
    int front; 
    int rear; 
    int data[queuesize]; 
    int count; //Record the elements in the queue
  }; 
   
  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("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; 
  } 
   
   
  void InitQueue(struct queue *Q) 
  { 
    Q -> front = Q -> rear = 0; 
    Q -> count = 0; 
  } 
   
   
  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("failedn"); 
    } 
  } 
   
   
  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("failedn"); 
    } 
  } 
   
   
  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("%dn",Q -> data[temp]); 
      else if((Q -> front > Q -> rear) && (temp >= Q -> front || temp < Q->rear)) 
        printf("%dn",Q -> data[temp]); 
      else if(Q -> front == Q -> rear) 
        printf("%dn",Q -> data[temp]); 
      else 
        printf("failedn"); 
    }else 
    { 
      printf("failedn"); 
    } 
  } 


Related articles: