Skip to main content

Static Implementation of Circular Queue

Static Implementation of Circular Queue


CODE
๐Ÿ‘‡
  1. #include<stdio.h>
  2. #define MAX 50
  3. typedef struct
  4. {
  5.   int data[MAX];
  6.   int front,rear;
  7. }QUEUE;

  8. void InitQ(QUEUE *pq)
  9. {
  10.   pq->front = pq->rear = MAX-1;
  11. }

  12. void AddQ(QUEUE *pq, int num)
  13. {
  14.   pq->rear=(pq->rear+1)%MAX;
  15. pq->data[pq->rear]=num;
  16. }

  17. int DelQ(QUEUE *pq)
  18. {
  19.   pq->front=(pq->front+1)%MAX;
  20.   return(pq->data[pq->front]);
  21. }

  22. int isempty(QUEUE *pq)
  23. {
  24.   return(pq->front == pq->rear);
  25. }

  26. int isfull(QUEUE *pq)
  27. {
  28.   return((pq->rear+1)%MAX==pq->front);
  29. }

  30. void main()
  31. {
  32.   int num,choice;
  33.   QUEUE q1;
  34.   InitQ(&q1);
  35.   printf(">>Static Implementation of Circular Queue<<\n");
  36.   do
  37.   {
  38.     printf("\n1: Add \n");
  39.     printf("2: Delete \n");
  40.     printf("3: Exit \n");
  41.     printf("Enter your choice: \t");
  42.     scanf("%d",&choice);

  43.     switch(choice)
  44.     {
  45.       case 1: printf("Enter Element To Be Added: \t");
  46.       scanf("%d",&num);
  47.       if(isfull(&q1))
  48.       printf("Queue overflow \n");
  49.       else
  50.       AddQ(&q1,num);
  51.       break;

  52.     case 2: if(isempty(&q1))
  53.       printf("Queue is empty \n");
  54.       else
  55.       printf("The Deleted Element is: %d \n",DelQ(&q1));
  56.       break;

  57.       case 3: printf("\t>>END<<");
  58.       break;
  59.      
  60.       default: printf("\t>>Enter Right Choice<<");
  61.  
  62.     }
  63.   }
  64. while(choice!=3);
  65. }

๐Ÿ‘‰Execute๐Ÿ‘ˆ


//Output
/*
>>Static Implementation of Circular Oueue<<

1: Add
2: Delete
3: Exit
Enter your choice:      1
Enter element to be added:      2

1: Add
2: Delete
3: Exit
Enter your choice:      2
The deleted element is 2

1: Add
2: Delete
3: Exit
Enter your choice:      4
        >>Enter Right Choice<<
1: Add
2: Delete
3: Exit
Enter your choice:      3
>>END<<
*/

Comments