Advertisement
HmHimu

queue final

Mar 1st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. struct Node
  4. {
  5. int data;
  6. struct Node *next;
  7. } *temp,*temp2, *node;
  8.  
  9. struct Head
  10. {
  11. int count;
  12. struct Node *front;
  13. struct Node *rear;
  14. } *head;
  15.  
  16. void Head_create()
  17. {
  18. head = (struct Head*) malloc(sizeof(struct Head));
  19. head->count = 0;
  20. head->front = NULL;
  21. head->rear = NULL;
  22. }
  23.  
  24. void Enque(int value)
  25. {
  26. node=(struct Node*)malloc(sizeof(struct Node));
  27. node->data = value;
  28. node->next=NULL;
  29. if(head->count==0)
  30. {
  31. head->front=node;
  32. head->rear=node;
  33. (head->count)++;
  34. }
  35. else
  36. {
  37. head->rear->next=node;
  38. head->rear=node;
  39. (head->count)++;
  40. }
  41. }
  42. void swap_q()
  43. {
  44. int m,v;
  45. temp = head->front;
  46. printf("\t\tEnter value: ");
  47. scanf("%d",&m);
  48. while(temp!= NULL)
  49. {
  50. if(m==temp->data)
  51. {
  52. v=temp->next->data;
  53. temp->data=v;
  54. temp->next->data=m;
  55. break;
  56. }
  57. temp = temp->next;
  58. }
  59. }
  60. void Deque()
  61. {
  62. //temp=head->front;
  63. if(head->count == 0)
  64. printf("\t\tqueue is empty.\n");
  65. else if(head->count == 1)
  66. {
  67. temp=head->front;
  68. temp2=head->rear;
  69. head->rear=NULL;
  70. head->front=NULL;
  71. free(temp);
  72. free(temp2);
  73. (head->count)--;
  74. }
  75. else
  76. {
  77. temp=head->front;
  78. head->front=head->front->next;
  79. (head->count)--;
  80. free(temp);
  81. }
  82. }
  83. void Search_values(int value)
  84. {
  85. int flag = 0,i;
  86. for(temp = head->front,i=1; temp != NULL; temp = temp->next,i++)
  87. {
  88. if(temp->data == value)
  89. {
  90. printf("\t\t%d Found in the queue in %d th position.\n",temp->data,i);
  91. flag=1;
  92. break;
  93. }
  94. }
  95. if(flag == 0)
  96. {
  97. printf("\t\t%d not Found in the queue!\n",value);
  98. }
  99. }
  100.  
  101. void Show_values()
  102. {
  103. temp = head->front;
  104. if(head->count==0)
  105. printf("\t\tNo values in the queue!!\n");
  106. else
  107. {
  108. printf("\t\tCurrent Values in the queue\n");
  109. while(temp!= NULL)
  110. {
  111. printf("\t\t-> %d",temp->data);
  112. temp = temp->next;
  113. }
  114. }
  115. }
  116.  
  117.  
  118. void Rear()
  119. {
  120. if(head->count!=0)
  121. {
  122. printf("\t\tRear is: %d",head->rear->data);
  123. }
  124. else
  125. printf("\t\tqueue is empty.\n");
  126. }
  127. void Front()
  128. {
  129. if(head->count!=0)
  130. {
  131. printf("\t\tFront is: %d",head->front->data);
  132. }
  133. else
  134. printf("\t\tqueue is empty.\n");
  135. }
  136.  
  137. void Menu_function()
  138. {
  139. printf("\n\n\t\t-------------------------------------------------\n");
  140. printf("\t\t: Queue :\n");
  141. printf("\t\t-------------------------------------------------\n");
  142. printf("\t\t: 1. Enqueue :\n");
  143. printf("\t\t-------------------------------------------------\n");
  144. printf("\t\t: 2. Dequeue :\n");
  145. printf("\t\t-------------------------------------------------\n");
  146. printf("\t\t: 3. Search a Data :\n");
  147. printf("\t\t-------------------------------------------------\n");
  148. printf("\t\t: 4. Show All Data :\n");
  149. printf("\t\t-------------------------------------------------\n");
  150. printf("\t\t: 5. Queue empty or not!! :\n");
  151. printf("\t\t-------------------------------------------------\n");
  152. printf("\t\t: 6. Front :\n");
  153. printf("\t\t-------------------------------------------------\n");
  154. printf("\t\t: 7. Rear :\n");
  155. printf("\t\t-------------------------------------------------\n");
  156. printf("\t\t: 8. swap :\n");
  157. printf("\t\t-------------------------------------------------\n");
  158. printf("\t\t: 9. Exit program :\n");
  159. printf("\t\t-------------------------------------------------\n");
  160. printf("\t\tEnter choice :");
  161. }
  162.  
  163. int Take_number()
  164. {
  165. int value;
  166. printf("\t\tEnter a value: ");
  167. scanf("%d", &value);
  168. return value;
  169. }
  170.  
  171. int main()
  172. {
  173. int choice;
  174. Head_create();
  175. for( ; ; )
  176. {
  177. Menu_function();
  178. scanf("%d", &choice);
  179. switch (choice)
  180. {
  181. case 1:
  182. Enque(Take_number());
  183. break;
  184. case 2:
  185. Deque();
  186. break;
  187. case 3:
  188. Search_values(Take_number());
  189. break;
  190. case 4:
  191. Show_values();
  192. break;
  193. case 5:
  194. if(head->count==0)
  195. printf("\t\tqueue is empty\n");
  196. else
  197. printf("\t\tqueue is not empty\n");
  198. break;
  199. case 6:
  200. Front();
  201. break;
  202. case 7:
  203. Rear();
  204. break;
  205. case 8:
  206. swap_q();
  207. break;
  208. case 9:
  209. free(temp);
  210. free(node);
  211. free(head);
  212. return EXIT_SUCCESS;
  213. break;
  214. default:
  215. printf("\t\tplease only press 1 to 8\n");
  216. break;
  217. }
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement