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. case 1: 
  39. printf("\n Enter string : ");
  40.     fflush(stdin);
  41.     scanf("%s",x);
  42.     if(!full(&q))
  43. {
  44. y=(char*)malloc(strlen(x)+1);
  45. strcpy(y,x);
  46. enqueue(&q,y);
  47. }
  48.     else
  49. printf("\n Queue is full !!!!");
  50.         break;
  51.     case 2:
  52.     if(!empty(&q))
  53.     {
  54. y=dequeue(&q);
  55. printf("\n Deleted Data = %s",y);
  56.     }
  57.     else
  58. printf("\n Queue is empty !!!!");
  59.     break;
  60.     case 3: 
  61.     print(&q);a
  62. break;
  63. }
  64.       }while(op!=4);
  65. }

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

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

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

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

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

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

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