1 /* 2 隊列queue:特殊的有序表,插入在表的一端,刪除在表的另一端 3 特點:先進先出(fifo) 4 簡單隊列: 5 缺點:在會導致隊列(右進左出)一直移動右移,直到隊列滿。 6 當隊列滿時,需要重新移動到最初始位置 才能繼續使用。 7 指針: 后部:rear 8 前部 front 9 隊列空:front=rear;前部等於后部時 10 隊列滿;rear=MAX_QUEUE_SIZE-1 11 如 進 rear->edbca->front 出 12 【 】rear和front都是相對於棧來說的 13 數組位置: 值 14 3 4 rear=3 4 rear=3 15 2 3 3 16 1 2 front=2 17 0 1 18 -1=rear=front 入隊列: front=-1 出隊列: 19 */ 20 #include<stdio.h> 21 22 #define MAX_QUEUE_SIZE 100 23 typedef struct{ 24 int key; 25 //other fields 26 }element; 27 28 element queue[MAX_QUEUE_SIZE]; 29 30 31 void Addq(int *rear,element item) 32 { 33 if(*rear==MAX_QUEUE_SIZE-1){ 34 printf("隊列滿"); 35 return ; 36 } 37 queue[++*rear]=item; 38 } 39 40 element Deleteq(int *front,int rear)//刪除的時候rear不變只有front動所以傳道rear的值 41 { 42 if(*front == rear){ 43 printf("隊列空"); 44 } 45 return queue[++*front]; 46 } 47 48 bool IsFullq(int *rear) 49 { 50 if(*rear==MAX_QUEUE_SIZE-1){ 51 return 1; 52 } 53 else 54 return 0; 55 } 56 57 bool IsEmptyq(int *front,int rear) 58 { 59 if(*front == rear){ 60 return 1; 61 } 62 return 0; 63 } 64 65 int main() 66 { 67 element item; 68 69 int front,rear; 70 int i; 71 72 rear=front=-1; 73 if(IsEmptyq(&front,rear)){ 74 printf("\n入隊列:\n"); 75 item.key=0; 76 for(i=0;i<30;i++){ 77 if(!IsFullq(&rear)){ 78 Addq(&rear,item); 79 printf("%d\n",item.key++); 80 } 81 else 82 break; 83 } 84 } 85 printf("\n出隊列\n"); 86 while(!IsEmptyq(&front,rear)){ 87 printf("%d\n",Deleteq(&front,rear).key); 88 } 89 printf("\n隊列為空\n"); 90 91 return 0; 92 } 93 94