// DataStructTest.cpp : Defines the entry point for the console application.
//
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "stdafx.h"
#include <iostream.h>
#include <malloc.h>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int const maxsize=20;
typedef struct cycqueue
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int data[maxsize];
int front;
int rear;
}CycqueueTP;
//初始化队列,队列结构为保留一个结点作为标志,即front所在的点为标志点,不存储内容
void InitCycQueue(CycqueueTP & sq)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
sq.front=0;
sq.rear=0;
}
//入队,需要使用模除,用以实现循环
int EnCycQueue(CycqueueTP & sq,int value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if ((sq.rear+1)%maxsize==sq.front)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//溢出
return 0;
}
sq.rear=(sq.rear+1)%maxsize;
sq.data[sq.rear]=value;
return 1;
}
//出队
int OutCycQueue(CycqueueTP & sq,int & value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if (sq.front==sq.rear)
return 0; //空队列
sq.front=(sq.front+1)%maxsize;
value=sq.data[sq.front];
return 1;
}
//判断是否为空队列
int EmptyCycQueue(CycqueueTP & sq)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if (sq.front==sq.rear)
return 1;
else
return 0;
}
//取队头结点
int GetHead(CycqueueTP & sq,int & value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if (sq.front==sq.rear)
return 0; //空队列
value=sq.data[(sq.front+1)%maxsize];
return 1;
}
//显示
void Display(CycqueueTP & sq)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int k=1;
if (sq.front<sq.rear)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=sq.front;i<sq.rear;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<"第"<<k++<<"结点的值为:"<<sq.data[i+1]<<endl;
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=sq.front;i<maxsize-1;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<"第"<<k++<<"结点的值为:"<<sq.data[i+1]<<endl;
}
for(int j=0;j<=sq.rear;j++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<"第"<<k++<<"结点的值为:"<<sq.data[j]<<endl;
}
}
}
int main(int argc, char* argv[])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
CycqueueTP sq;
InitCycQueue(sq);
//入队
for(int i=1;i<20;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnCycQueue(sq,i);
}
Display(sq);
//出队
int value=0;
OutCycQueue(sq,value);
cout<<"出队的结点值为:"<<value<<endl;
Display(sq);
//
EnCycQueue(sq,500);
cout<<endl;
Display(sq);
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。