2008秋季-计算机软件基础-0917课堂用例(2)
生活随笔
收集整理的這篇文章主要介紹了
2008秋季-计算机软件基础-0917课堂用例(2)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
循環隊列
?參見:http://www.cnblogs.com/emanlee/archive/2007/09/17/895463.html
#include<stdlib.h>//定義隊列的結構
struct?queue
{
???int?q[4];//存放數據元素
???int?front;//隊頭指針,指向隊頭
???int?rear;//隊尾指針,隊尾指針始終指向隊尾元素的后一個位置
};
//初始化隊列
struct?queue?*?InitialQueue()
{
?struct?queue?*?head;
?head=(struct?queue?*)
?????malloc(sizeof(?struct?queue?));
?head->front=0;
?head->rear=0;
?return?head;
}
void?EnterIntoQueue(struct?queue?*?head,
????????????????????int?value)
{
????if(head->front==?(head->rear+1)%4)
????{
???????printf("Queue?is?full.?Enter?failed.\n");
????????return;
????}
????head->q[head->rear]=value;
????head->rear=(?head->rear+1)%4;
?}
void?DeleteFromQueue(struct?queue?*?head)
{
?if(head->front==head->rear)
?{
?????printf("Queue?is?empty,?Delete?failed\n");
?}
?else
????{
????????head->front=(head->front+1)%4;
????}
}
void?ShowQueue(struct?queue?*?head)
{
/*?輸出要分開設計?*/
??int?i;
??printf("\n隊列元素\n");
??for(i=0;i<=3;i++)
??????printf("?%d?",head->q[i]);
}
void?main()
{
?struct?queue?*?head;
?head=InitialQueue();
?EnterIntoQueue(head,1);
?ShowQueue(head);
?EnterIntoQueue(head,2);
?ShowQueue(head);
}
轉載于:https://www.cnblogs.com/emanlee/archive/2008/09/17/1292308.html
總結
以上是生活随笔為你收集整理的2008秋季-计算机软件基础-0917课堂用例(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Asp.net系列计划
- 下一篇: 纳米颗粒C语言数值模拟_J.Hazard