//
DataStructTest.cpp : Defines the entry point for the console application.
//
#include
"
stdafx.h
"
#include
<
iostream.h
>
#include
<
malloc.h
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int
const
maxsize
=
20
;
typedef
struct
cycqueue
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int data[maxsize];
int front;
int rear;
}
CycqueueTP;
//
初始化队列,队列结构为保留一个结点作为标志,即front所在的点为标志点,不存储内容
void
InitCycQueue(CycqueueTP
&
sq)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
sq.front=0;
sq.rear=0;
}
//
入队,需要使用模除,用以实现循环
int
EnCycQueue(CycqueueTP
&
sq,
int
value)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if ((sq.rear+1)%maxsize==sq.front)
![ExpandedSubBlockStart.gif](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)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.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)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (sq.front==sq.rear)
return 1;
else
return 0;
}
//
取队头结点
int
GetHead(CycqueueTP
&
sq,
int
&
value)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (sq.front==sq.rear)
return 0; //空队列
value=sq.data[(sq.front+1)%maxsize];
return 1;
}
//
显示
void
Display(CycqueueTP
&
sq)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int k=1;
if (sq.front<sq.rear)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=sq.front;i<sq.rear;i++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<"第"<<k++<<"结点的值为:"<<sq.data[i+1]<<endl;
}
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=sq.front;i<maxsize-1;i++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<"第"<<k++<<"结点的值为:"<<sq.data[i+1]<<endl;
}
for(int j=0;j<=sq.rear;j++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<"第"<<k++<<"结点的值为:"<<sq.data[j]<<endl;
}
}
}
int
main(
int
argc,
char
*
argv[])
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
CycqueueTP sq;
InitCycQueue(sq);
//入队
for(int i=1;i<20;i++)
![ExpandedSubBlockStart.gif](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/fxwdl/archive/2007/06/25/795064.html