3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
[C/C++ 演算法]-資料結構與演算法(文魁):環狀佇列使用範例   線上執行結果:http://www.tutorialspoint.com/compile_c_online.php code2html:http://tohtml.com/     /* =============== Program Description =============== */ /* 程式名稱 : 8_3.cpp */ /* 程式目的 : 環狀佇列使用範例 */ /* 輸 入 : 輸入到環狀佇列的整數資料 */ /* 輸 出 : 由環狀佇列所輸出的整數資料 */ /* =================================================== */ #define n 5 // 宣告標頭檔 #include "stdio.h" // 宣告全域變數 int cQueue[n]; int cfront=-1; int crear=-1; // 宣告函式原型 // 利用傳值的方式把整數data傳進addq函式中 void addcq(int data); // 從delq函式中回傳一個整數 int delcq(void); // 從isEmpty函式中回傳一個整數 int iscEmpty(void); // 從isFull函式中回傳一個整數 int iscFull(void); void main(void) { int i; int A[n]={1,3,5,7,9}; // 輸入的資料 printf(" Data input: "); /* 依序資料放入佇列 */ for(i=0;i<n;i++) { printf("%d",A[i]); addcq(A[i]); } printf("\n"); // 輸出的資料 printf(" Data output : "); /* 依序從佇列取出資料 */ for(i=0;i<n;i++) printf("%d",delcq()); printf("\n"); getchar(); } void addcq(int data) { if(iscFull()) /* iscFull��檢查佇列��否已滿了*/ printf("cQueue is Full!\n"); else cQueue[++crear%n] = data; } int delcq(void) { if(iscEmpty()) /* iscEmpty��檢查佇列��否已空了*/ { printf("cQueue is Empty!\n"); return 0; /* 傳回0代表失敗*/ }else{ return cQueue[++cfront%n]; /* 一切正常front,加1,且傳回資料*/ } } int iscFull(void) { if(cfront==(crear+1)%n) return 1; /* 1代表已滿 */ else return 0; /* 0代表未滿 */ } int iscEmpty(void) { if(cfront==crear) return 1; /* 1代表已空 */ else return 0; /* 0代表未空 */ }    

本文由jashliaoeuwordpress提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦