Skip to main content

Dynamic Implementation of Circular Queue

Dynamic Implementation of Circular Queue 


CODE
👇
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef struct node
  4. {
  5. int info;
  6.   struct node *next;
  7. }NODE;

  8. NODE *front,*rear;

  9. void InitQ()
  10. {
  11.   rear = NULL;
  12. }

  13. void AddQ(int num)
  14. {
  15.   NODE *newnode;
  16. newnode=(NODE*)malloc(sizeof(NODE));
  17.   newnode->info=num;
  18.   if(rear==NULL)
  19.   {
  20.     rear=newnode;
  21.     rear->next=rear;
  22.   }
  23.   else
  24.   {
  25.     newnode->next=rear->next;
  26.     rear->next=newnode;
  27.     rear=newnode;
  28.   }
  29. }

  30. int DelQ()
  31. {
  32. NODE *front=rear->next;
  33.   int num=front->info;
  34.   if(rear->next==rear)
  35.   free(rear);
  36.   else
  37.   {
  38.     rear->next=front->next;
  39.     free(front);
  40.   }
  41.   return(num);
  42. }

  43. int isempty()
  44. {
  45.   return(rear == NULL);
  46. }

  47. void main()
  48. {
  49.   int choice,num;
  50.   InitQ();
  51.   printf(">>Dynamic Implementation of Circular Queue<<\n");
  52.   do
  53.   {
  54.     printf("\n1: Add \n");
  55.     printf("2: Delete \n");
  56.     printf("3: Exit \n");
  57.     printf("Enter your choice:\t");
  58.     scanf("%d",&choice);

  59.     switch(choice)
  60.     {
  61.       case 1: printf("Enter Element To Be Added:\t");
  62.       scanf("%d",&num);
  63.       AddQ(num);
  64.       break;

  65.       case 2: if(isempty())
  66.       printf(">>Queue Is Empty<< \n");
  67.       else
  68.       printf("The Deleted Element Is:\t%d \n",DelQ());
  69.       break;

  70.       case 3: printf("\t>>END<<");
  71.       break;
  72.      
  73.       default: printf("\t>>Enter Right Choice<<");
  74.     }
  75.   }
  76. while(choice!=3);
  77. }


👉Execute👈

//Output
/*
>>Dynamic Implementation of Circular Queue<<

1: Add
2: Delete
3: Exit
Enter your choice:      1
Enter Element To Be Added:      5

1: Add
2: Delete
3: Exit
Enter your choice:      2
The Deleted Element Is: 5

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

                                                    //ThE ProFessoR

Comments