arrayQueue
生活随笔
收集整理的這篇文章主要介紹了
arrayQueue
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用數組實現隊列:
定義頭文件:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define MAX_SIZE 10 4 int queue[MAX_SIZE]; 5 int front=-1; 6 int rear=-1; 7 8 int IsFull(int queue[]); //判斷隊列是否已滿 9 int IsEmpty(int queue[]); //判斷隊列是否為空 10 void queueFull(); //隊列滿時拋出函數,并退出程序 11 void queueEmpty(); //隊列為空時的函數 12 void addQ(int queue[],int element); //向隊列中追加元素 13 int deleteQ(int queue[]); //取出隊首元素各函數的具體實現:
1 int IsFull(int queue[]){ 2 if(rear==MAX_SIZE-1) 3 return 1; 4 return 0; 5 } 6 int IsEmpty(int queue[]){ 7 if(front==rear) 8 return 1; 9 return 0; 10 } 11 void queueFull(){ 12 fprintf(stderr,"Queue is full!"); 13 exit(EXIT_FAILURE); 14 } 15 void queueEmpty(){ 16 fprintf(stderr,"Queue is empty!"); 17 exit(EXIT_FAILURE); 18 } 19 void addQ(int queue[],int element){ 20 if(IsFull(queue)) 21 queueFull(); 22 queue[++rear] = element; 23 } 24 int deleteQ(int queue[]){ 25 if(IsEmpty(queue)) 26 queueEmpty(); 27 return queue[++front] ; 28 }用來測試的主函數(在devcpp中編譯通過):
1 int main(void) 2 { 3 int i=0; 4 for(i=0;i<MAX_SIZE;i++) { 5 addQ(queue,i+1); 6 } 7 for(i=0;i<MAX_SIZE;i++){ 8 printf("%d ",deleteQ(queue)); 9 } 10 printf("%d\n",deleteQ(queue)); 11 }在抽象數據類型中有創建一個隊列的函數,卻不知道是怎么實現~~
2016-10-24 ? ??10:30:45
轉載于:https://www.cnblogs.com/dtdyq/p/5992045.html
總結
以上是生活随笔為你收集整理的arrayQueue的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iterator and iterabl
- 下一篇: 利用Aspose.Word控件实现Wor