标签:return 数据结构 == value NPU can bre 队列 出队
使用数组创建队列
1 #include <stdio.h> 2 #define MAXQUEUE 10 3 4 int queue[MAXQUEUE]; 5 int front = -1; 6 int rear = -1; 7 8 int enqueue(int value) 9 { 10 if(rear >= MAXQUEUE) 11 return -1; 12 rear++; 13 queue[rear] = value; 14 } 15 16 int dequeue() 17 { 18 if(front == rear) 19 return -1; 20 front++; 21 return queue[front]; 22 } 23 24 int main() 25 { 26 int input[100]; 27 int output[100]; 28 int select; 29 int i_count = 0; 30 int o_count = 0; 31 int loop = 1; 32 int i,temp; 33 34 while(loop) 35 { 36 printf("[1]输入 [2]取出 [3]列出全部内容 ==> "); 37 scanf("%d",&select); 38 switch(select) 39 { 40 case 1: printf("请输入存入队列的值(%d) ==> ",i_count + 1); 41 scanf("%d",&temp); 42 if(enqueue(temp) == -1) 43 printf("队列全满. \n"); 44 else 45 input[i_count++] = temp; 46 break; 47 case 2: if((temp = dequeue()) == -1) 48 printf("队列是空的. \n"); 49 else 50 { 51 printf("取出队列元素: %d\n",temp); 52 output[o_count++] = temp; 53 } 54 break; 55 case 3: loop = 0; 56 break; 57 58 } 59 } 60 printf("输入队列的元素:"); 61 for(i = 0; i < i_count;i++) 62 printf("[%d]",input[i]); 63 printf("\n取出队列的元素:"); 64 for(i = 0;i < o_count;i++) 65 printf("[%d]",output[i]); 66 printf("\n剩下队列的元素:"); 67 while((temp = dequeue()) != -1) 68 printf("[%d]",temp); 69 printf("\n"); 70 71 return 0; 72 }
标签:return 数据结构 == value NPU can bre 队列 出队
原文地址:https://www.cnblogs.com/hulianxingkong/p/14218485.html