Problems:  Write a C program to implement a circular queue that contains a list of names.  

Data Structure Definition:

Node: A node is a basic building block of a linked list. A node may contain different segments, but basically it contains two parts which are as followed,

 

                                    Data or information part : It stores the data in the node

                                       Link part : It points to the next node or holds the address to the next node.

Source Code:

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #define MAX 10

  5. //defining the basic element of the queue
  6. typedef struct Q
  7. {
  8. int R,F;
  9. char *data[MAX];
  10. }Q;

  11. //declaration of functions
  12. void initialise(Q *P);
  13. int empty(Q *P);
  14. int full(Q *P);
  15. void enqueue(Q *P,char *x);
  16. char * dequeue(Q *P);
  17. void print(Q *P);

  18. //main function
  19. int main()
  20. {
  21. //intializing a node named queue
  22. Q q;
  23. //declaration and initialization fof variables
  24.     int op;
  25.     char x[40],*y;
  26.     
  27.     //initializ the queue
  28.     initialise(&q);
  29.         
  30. do{
  31. //displaying the menu
  32. printf("\n\n 1) Enqueue\n 2) Dequeue\n 3) Print\n 4) Quit");
  33. //inputting the input choice 
  34. printf("\n Enter Your Choice : ");
  35. scanf("%d",&op);
  36. //calling functions and passing arguments through the functions as per th choice of the user
  37. switch(op)
  38. { 
  39. case 1: 
  40. printf("\n Enter string : ");
  41.     fflush(stdin);
  42.     scanf("%s",x);
  43.     if(!full(&q))
  44. {
  45. y=(char*)malloc(strlen(x)+1);
  46. strcpy(y,x);
  47. enqueue(&q,y);
  48. }
  49.     else
  50. printf("\n Queue is full !!!!");
  51.         break;
  52.     case 2:
  53.     if(!empty(&q))
  54.     {
  55. y=dequeue(&q);
  56. printf("\n Deleted Data = %s",y);
  57.     }
  58.     else
  59. printf("\n Queue is empty !!!!");
  60.     break;
  61.     case 3: 
  62.     print(&q);a
  63. break;
  64. }
  65.       }while(op!=4);
  66. }

  67. //function to initialize a circular queue
  68. void initialise(Q *P)
  69. {
  70. //declaration of variable
  71. int i;
  72. //initializing the queue
  73. for(i=0;i<MAX;i++)
  74. P->data[i]=NULL;
  75. P->R=-1;
  76. P->F=-1;
  77. }

  78. //function to check if the queue is empty
  79. int empty(Q *P)
  80. {
  81. //if the queue is empty, return 1 else, return 0
  82. if(P->R==-1)
  83. return(1);
  84. return(0);
  85. }

  86. //function to check if the queue is full
  87. int full(Q *P)
  88. {
  89. //return 1 if the queue is full, else return 0
  90. if((P->R+1)%MAX==P->F)
  91. return(1);
  92. return(0);
  93. }

  94. //function to enqueue and element in the queue
  95. void enqueue(Q *P,char *x)
  96. {
  97. //enqueueing the element
  98. if(P->R==-1)
  99. {
  100. P->R=P->F=0;
  101. if(P->data[P->R]!=NULL)
  102. free(P->data[P->R]);
  103. P->data[P->R]=x;
  104. }
  105. else
  106. {
  107. P->R=(P->R+1)%MAX;
  108. if(P->data[P->R]!=NULL)
  109. free(P->data[P->R]);
  110. P->data[P->R]=x;
  111. }
  112. }

  113. //function to dequeue 
  114. char * dequeue(Q *P)
  115. {
  116. //declaring a character type pointer
  117. char *x;
  118. //dequeing the element from the queue
  119. x=P->data[P->F];
  120. if(P->R==P->F)
  121. {
  122. P->R=-1;
  123. P->F=-1;
  124. }
  125. else
  126. P->F=(P->F+1)%MAX;
  127. return(x);
  128. }

  129. //function to display the elements of the queue
  130. void print(Q *P)
  131. {
  132. int i;
  133. if(!empty(P))
  134. {
  135. printf("\n");
  136. for(i=P->F;i!=P->R;i=(i+1)%MAX)
  137. printf("%s\t",P->data[i]);
  138. }
  139.        printf("%s\t",P->data[i]);
  140. }

Output:

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 1

 

 Enter string : computer

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 1

 

 Enter string : science

 

1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 1

 

Enter string : honours

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 3

 

computer        science   honours

 

1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 2

 

 Deleted Data = computer

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 2

 

 Deleted Data = science

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 2

 

 Deleted Data = honours

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

Enter Your Choice : 2

 

 Queue is empty !!!!


0 comments:

Post a Comment