fork(3) download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define Max 5 //so phan tu toi da cua Queue
  5. typedef int item; //kieu du lieu
  6.  
  7. struct Queue
  8. {
  9. int Front, Rear; //front: phan tu dau hang, rear: phan tu cuoi hang
  10. item Data[Max]; //Mang cac phan tu
  11. int count; //dem so phan tu cua Queue
  12. };
  13.  
  14. void Init (Queue &Q); //khoi tao Queue rong
  15. int Isempty(Queue Q); //kiem tra Queue rong
  16. int Isfull(Queue Q); //kiem tra Queue day
  17. void Push(Queue &Q, item x); //them phan tu vao cuoi hang doi
  18. int Pop(Queue &Q); //Loai bo phan tu khoi dau hang doi
  19. int Qfront (Queue Q); //xem thong tin phan tu dau hang doi
  20. void Push_Circular(Queue &Q, item x); //them phan tu vao cuoi hang doi vong
  21. int Pop_Circular(Queue &Q); //Loai bo phan tu khoi dau hang doi vong
  22. void Input (Queue &Q); //Nhap
  23. void Output(Queue Q); //Xuat
  24.  
  25. void Init (Queue &Q) //khoi tao Queue rong
  26. {
  27. Q.Front = 0; //phan tu dau
  28. Q.Rear = -1; // phan tu cuoi o -1 (khong co phan tu trong Q)
  29. Q.count = 0; //so phan tu bang 0
  30. }
  31.  
  32. int Isempty (Queue Q) //kiem tra Queue rong
  33. {
  34. if (Q.count == 0) //so phan tu = 0 => rong
  35. return 1;
  36. return 0;
  37. }
  38.  
  39. int Isfull (Queue Q) //kiem tra Queue day
  40. {
  41. if (Q.count == Max) //so phan tu = Max => day
  42. return 1;
  43. return 0;
  44. }
  45.  
  46. void Push(Queue &Q, item x) //them phan tu vao cuoi Queue
  47. {
  48. if (Isfull(Q)) printf("Hang doi day !");
  49. else
  50. {
  51. Q.Data[++Q.Rear] = x; //tang Rear len va gan phan tu vao
  52. Q.count++; //tang so phan tu len
  53. }
  54. }
  55.  
  56. int Pop(Queue &Q) //Loai bo phan tu khoi dau hang doi
  57. {
  58. if (Isempty(Q)) printf("Hang doi rong !");
  59. else
  60. {
  61. item x = Q.Data[Q.Front];
  62. for (int i=Q.Front; i<Q.Rear; i++) //di chuyen cac phan tu ve dau hang
  63. Q.Data[i] = Q.Data[i+1];
  64. Q.Rear--; // giam vi tri phan tu cuoi xuong
  65. Q.count--;//giam so phan tu xuong
  66. return x; //tra ve phan tu lay ra
  67. }
  68. }
  69.  
  70. item Qfront (Queue Q) //xem thong tin phan tu dau hang
  71. {
  72. if (Isempty(Q)) printf("Hang doi rong !");
  73. else return Q.Data[Q.Front];
  74. }
  75.  
  76. void Push_Circular(Queue &Q, item x) //them phan tu vao cuoi hang doi vong
  77. {
  78. if (Isfull(Q)) printf("Hang doi day !");
  79. else
  80. {
  81. Q.Data[(++Q.Rear) % Max] = x;
  82. //tang Rear len va gan phan tu vao, Neu Rear dang o vi tri Max-1 thi tang ve vi tri 0
  83. Q.count++; //tang so phan tu len
  84. }
  85. }
  86.  
  87. int Pop_Circular(Queue &Q) //Loai bo phan tu khoi dau hang doi vong
  88. {
  89. if (Isempty(Q)) printf("Hang doi rong !");
  90. item x = Q.Data[Q.Front];
  91. Q.Front = (Q.Front++) % Max; // tang vi tri phan dau tien len, neu dang o Max-1 thi ve 0
  92. Q.count--;//giam so phan tu xuong
  93. return x; //tra ve phan tu lay ra
  94. }
  95.  
  96.  
  97. void Input (Queue &Q) //nhap hang doi
  98. {
  99. int n;
  100. item x;
  101. do
  102. {
  103. printf("Nhap so phan tu cua Queue (<%d) :",Max);
  104. scanf("%d",&n);
  105. } while (n>Max || n<1);
  106. for (int i=0; i<n; i++)
  107. {
  108. printf("Nhap phan tu thu %d : ", i+1);
  109. scanf("%d",&x);
  110. Push(Q,x);
  111. Push_Circular(Q,x); //hang vong
  112. }
  113. }
  114.  
  115. void Output(Queue Q)
  116. {
  117. if (Isempty(Q)) printf("Hang doi rong !");
  118. else
  119. {
  120. for (int i=Q.Front; i<=Q.Rear; i++)
  121. //for (int i=Q.Front, j=0; j<Q.count; j++, i = (i++) % Max) //hang vong
  122. printf("%d ",Q.Data[i]);
  123. printf("\n");
  124. }
  125. }
  126.  
  127. int main()
  128. {
  129. Queue Q;
  130. Init(Q);
  131. Input(Q);
  132. Output(Q);
  133.  
  134. int lua_chon;
  135. printf("Moi ban chon phep toan voi DS LKD:");
  136. printf("\n1: Kiem tra Queue rong");
  137. printf("\n2: Kiem tra Queue day");
  138. printf("\n3: Them phan tu vao Queue");
  139. printf("\n4: Xoa phan tu trong Queue");
  140. printf("\n5: Xuat Queue");
  141. printf("\n6: Thoat");
  142. do
  143. {
  144. printf("\nBan chon: ");
  145. scanf("%d",&lua_chon);
  146. switch (lua_chon)
  147. {
  148. case 1:
  149. {
  150. if (Isempty(Q)) printf("Queue rong !");
  151. else printf ("Queue khong rong !");
  152. break;
  153. }
  154. case 2:
  155. {
  156. if (Isfull(Q)) printf("Queue day !");
  157. else printf ("Queue chua day !");
  158. break;
  159. }
  160. case 3:
  161. {
  162. item x;
  163. printf ("Nhap phan tu can chen vao Queue: ");
  164. scanf("%d",&x);
  165. Push(Q,x);
  166. //Push_Circular(Q,x); hang vong
  167. break;
  168. }
  169. case 4:
  170. {
  171. Pop(Q);
  172. //Pop_Circular(Q); hang vong
  173. break;
  174. }
  175. case 5:
  176. {
  177. Output(Q);
  178. break;
  179. }
  180. case 6: break;
  181. }
  182. }while (lua_chon !=6);
  183. return 0;
  184. }
  185.  
  186.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:14:18: error: expected ‘)’ before ‘&’ token
prog.c:15:13: error: unknown type name ‘Queue’
prog.c:16:12: error: unknown type name ‘Queue’
prog.c:17:17: error: expected ‘)’ before ‘&’ token
prog.c:18:15: error: expected ‘)’ before ‘&’ token
prog.c:19:13: error: unknown type name ‘Queue’
prog.c:20:26: error: expected ‘)’ before ‘&’ token
prog.c:21:24: error: expected ‘)’ before ‘&’ token
prog.c:22:19: error: expected ‘)’ before ‘&’ token
prog.c:23:13: error: unknown type name ‘Queue’
prog.c:25:18: error: expected ‘)’ before ‘&’ token
prog.c:32:14: error: unknown type name ‘Queue’
prog.c:39:13: error: unknown type name ‘Queue’
prog.c:46:17: error: expected ‘)’ before ‘&’ token
prog.c:56:15: error: expected ‘)’ before ‘&’ token
prog.c:70:14: error: unknown type name ‘Queue’
prog.c:76:26: error: expected ‘)’ before ‘&’ token
prog.c:87:24: error: expected ‘)’ before ‘&’ token
prog.c:97:19: error: expected ‘)’ before ‘&’ token
prog.c:115:13: error: unknown type name ‘Queue’
prog.c: In function ‘main’:
prog.c:129:5: error: unknown type name ‘Queue’
prog.c:130:5: warning: implicit declaration of function ‘Init’ [-Wimplicit-function-declaration]
prog.c:131:5: warning: implicit declaration of function ‘Input’ [-Wimplicit-function-declaration]
prog.c:132:5: warning: implicit declaration of function ‘Output’ [-Wimplicit-function-declaration]
prog.c:150:5: warning: implicit declaration of function ‘Isempty’ [-Wimplicit-function-declaration]
prog.c:156:5: warning: implicit declaration of function ‘Isfull’ [-Wimplicit-function-declaration]
prog.c:165:5: warning: implicit declaration of function ‘Push’ [-Wimplicit-function-declaration]
prog.c:171:5: warning: implicit declaration of function ‘Pop’ [-Wimplicit-function-declaration]
stdout
Standard output is empty