search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[C/C++ 演算法]-資料結構與演算法(文魁):環狀佇列使用範例 – jashliao部落格

[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 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦