隊列是一種先進先出的數據存儲結構,一般操作系統中用的比較多,本文主要介紹對順序隊列的入隊列,出隊列,遍歷隊列操作。
定義順序隊列:
我們可以用一個數組來表示一個順序存儲結構,兩個整數來分別指向數組的下標,表示隊列的頭指針和尾指針;
typedef struct queue { int data[MAX]; int front; //頭指針 int rear; //尾指針 }queue;
定義隊列之后首先就是初始化隊列:
初始化隊列的時候隊列一定是空的,隊列的頭指針和尾指針必須指向數組的首端;
queue *init() { queue *h; h = (queue *)malloc(sizeof(queue)); h->front = 0; h->rear = 0; return h; }
定義隊列的存儲結構並且初始化隊列之后,接下來就要入隊列了。
在入隊列的時候需要注意幾個問題;
①:入隊列是必須判斷隊列是否已滿,可以用一個數學公式來判斷:(h->rear + 1) % MAX == h->front 如果該表達式為TRUE,說明隊列已滿;
②:把此隊列設置成循環隊列,即當h->rear走到數組末端的時候,必須考慮把h->rear指向數組的首端(0);【入隊列移動的是隊列的尾指針】
③:連續入隊列的時候必須在入隊列之后檢查隊列是否已滿;
我們有了這些規則之后就可以進行編碼了:
void insert(queue *h) //插入隊列 { int value; char ch; if((h->rear + 1) % MAX == h->front) //判斷隊列是否已滿 printf("隊列已滿,請刪除后再插入!\n"); else { do { printf("請輸入需要插入的值:"); value = get_int(); h->data[h->rear] = value; h->rear++; if(h->rear == MAX) //因為是循環隊列,所以當隊列不滿時,並且h->rear已大於數組下標時,h->rear要指向數組初始位置 h->rear = 0; if((h->rear + 1) % MAX == h->front) //入隊列之后要進一步的判斷隊列是否已滿 { printf("隊列已滿,請刪除后再插入!\n"); ch = 'n'; } else { do { printf("是否需要繼續插入?y/n"); ch = get_first(); }while(ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N'); } }while(ch == 'y' || ch == 'Y'); } }
出隊列需要注意的問題:
①:判斷隊列是否為空;我們認為當頭指針等於尾指針是隊列為空;
②:當隊列的頭指針走到數組尾部是需要改變頭指針指向數組頭部;【出隊列只改變頭指針的位置】
③:連續出隊列的時候必須在出隊列之后檢查隊列是否為空;
void del(queue *h) //出隊列 { int value; char ch; if(h->front == h->rear) printf("隊列以空!!!\n"); else { do { value = h->data[h->front]; h->data[h->front] = 0; h->front++; printf("\n%d 出隊列\n",value); if(h->front == MAX) h->front = 0; if(h->front == h->rear) { printf("隊列以空!!!\n"); ch = 'n'; } else { do { printf("是否需要繼續出隊列?y/n\n"); ch = get_first(); }while(ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N'); } }while(ch == 'y' || ch == 'Y'); } }
遍歷隊列:
void bianli(queue *h) //遍歷隊列中的元素 { int i,j; if(h->front == h->rear) printf("隊列為空!!!\n"); else { j = h->front; printf("出隊列的順序為:\n"); for(i = 0;i < (h->rear - h->front + MAX) % MAX;i++) { printf("the %d value is %d\n",i + 1,h->data[j]); j++; } } }
完整的代碼如下:【代碼粘不上了,只能這樣了】

1 #include<stdio.h> 2 #include<stdlib.h> 3 4 #define MAX 5 5 6 typedef struct queue 7 { 8 int data[MAX]; 9 int front; 10 int rear; 11 }queue; 12 13 queue *h; //隊列頭結點 14 15 16 queue *init() 17 { 18 queue *h; 19 h = (queue *)malloc(sizeof(queue)); 20 h->front = 0; 21 h->rear = 0; 22 return h; 23 } 24 25 26 27 int get_int() //得到整型 28 { 29 int input; 30 char ch; 31 while(scanf("%d",&input) != 1) 32 { 33 while((ch = getchar()) != '\n'); //截取多余的字符串 34 printf("輸入格式不對,請重新輸入!\n"); 35 } 36 while((ch = getchar()) != '\n'); //截取多余的字符串 37 return input; 38 39 } 40 41 char get_first() //得到第一個字符 42 { 43 char c; 44 scanf("%c",&c); 45 while(getchar() != '\n') 46 continue; 47 return c; 48 } 49 50 int menu() //選擇菜單 51 { 52 int result; 53 printf("**********請選擇:***********\n"); 54 printf("**********1.插入:***********\n"); 55 printf("**********2.刪除:***********\n"); 56 printf("**********3.遍歷:***********\n"); 57 58 result = get_int(); 59 while(result > 3 || result < 1) 60 { 61 printf("請輸入1-3!\n"); 62 result = get_int(); 63 } 64 return result; 65 } 66 67 void insert(queue *h) //插入隊列 68 { 69 int value; 70 char ch; 71 if((h->rear + 1) % MAX == h->front) //判斷隊列是否已滿 72 printf("隊列已滿,請刪除后再插入!\n"); 73 else 74 { 75 do 76 { 77 printf("請輸入需要插入的值:"); 78 value = get_int(); 79 80 h->data[h->rear] = value; 81 h->rear++; 82 83 if(h->rear == MAX) //因為是循環隊列,所以當隊列不滿時,並且h->rear已大於數組下標時,h->rear要指向數組初始位置 84 h->rear = 0; 85 86 if((h->rear + 1) % MAX == h->front) //入隊列之后要進一步的判斷隊列是否已滿 87 { 88 printf("隊列已滿,請刪除后再插入!\n"); 89 ch = 'n'; 90 } 91 else 92 { 93 do 94 { 95 printf("是否需要繼續插入?y/n"); 96 ch = get_first(); 97 }while(ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N'); 98 } 99 100 }while(ch == 'y' || ch == 'Y'); 101 } 102 103 } 104 105 void bianli(queue *h) //遍歷隊列中的元素 106 { 107 int i,j; 108 if(h->front == h->rear) 109 printf("隊列為空!!!\n"); 110 else 111 { 112 j = h->front; 113 printf("出隊列的順序為:\n"); 114 for(i = 0;i < (h->rear - h->front + MAX) % MAX;i++) 115 { 116 printf("the %d value is %d\n",i + 1,h->data[j]); 117 j++; 118 } 119 } 120 } 121 122 void del(queue *h) //出隊列 123 { 124 int value; 125 char ch; 126 if(h->front == h->rear) 127 printf("隊列以空!!!\n"); 128 else 129 { 130 do 131 { 132 value = h->data[h->front]; 133 h->data[h->front] = 0; 134 h->front++; 135 136 printf("\n%d 出隊列\n",value); 137 138 if(h->front == MAX) 139 h->front = 0; 140 141 if(h->front == h->rear) 142 { 143 printf("隊列以空!!!\n"); 144 ch = 'n'; 145 } 146 else 147 { 148 do 149 { 150 printf("是否需要繼續出隊列?y/n\n"); 151 ch = get_first(); 152 }while(ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N'); 153 } 154 }while(ch == 'y' || ch == 'Y'); 155 } 156 } 157 158 int main(void) 159 { 160 char ch; 161 h = init(); 162 do 163 { 164 switch(menu()) 165 { 166 case 1: insert(h);break; 167 case 2: del(h);break; 168 case 3: bianli(h);break; 169 } 170 do 171 { 172 printf("是否要繼續操作?(y/n)"); 173 ch = get_first(); 174 }while(ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N'); 175 }while(ch == 'y' || ch == 'Y'); 176 177 }