1 #include <stdio.h> 2 #define Max 100 3 4 void SetNull(front, rear) 5 int *front, *rear; 6 { 7 *front = 0; 8 *rear = 0; 9 } 10 11 int Empty(front, rear) 12 int *front, *rear; 13 { 14 if (*front == *rear) 15 return(1); 16 else 17 return(0); 18 } 19 20 int EnQueue(q, x, front, rear) 21 int q[]; 22 int x; 23 int *front, *rear; 24 { 25 *rear = (*rear + 1) % Max; 26 if (*front == *rear) 27 28 { 29 printf("队列发生上溢 "); 30 return(-1); 31 } 32 else 33 { 34 q[*rear] = x; 35 return(0); 36 } 37 } 38 39 int DelQueue(q, y, front, rear) 40 int q[]; 41 int *y; 42 int *front, *rear; 43 { 44 *front = (*front + 1) % Max; 45 if (*front == *rear) 46 { 47 printf("队列发生下溢 "); 48 return(-1); 49 } 50 else 51 { 52 *y = q[*front]; 53 return(0); 54 } 55 } 56 57 int main() 58 { 59 int q[Max]; 60 int f = 0, r = 0; /*f和r分别对应队列的头和尾在整个队列存储区域的位置*/ 61 int i, x, m, n; 62 int a; 63 SetNull(&f, &r); /*清空队列*/ 64 printf("要输入队列的字符个数: "); 65 scanf("%d", &m); 66 printf("输入队列的整型数据: "); 67 for (i = 0; i<m; i++) 68 { 69 i = i; 70 scanf("%d", &x); 71 a = EnQueue(q, x, &f, &r); 72 if (a == -1) 73 break; 74 } 75 printf("要提出队列的字符个数:"); 76 scanf("%d", &n); 77 printf("输出从队列中提取的数据: "); 78 for (i = 0; i<n; i++) 79 { 80 if (DelQueue(q, &x, &f, &r) == -1) 81 break; 82 printf("%d ", x); 83 } 84 if (Empty(&f, &r) == 1) 85 printf("队列为空"); 86 else 87 printf("队列中还有%d个数据", (m - n)); 88 getchar(); 89 return 0; 90 }