- 20
- 0
- 约2.56千字
- 约 5页
- 2017-08-18 发布于重庆
- 举报
队列的实现及应用(舞伴).doc
队列----------------------------------------------------------------------------------------------------------
头文件(头文件名为text9)
struct Queue{
ElemType *queue;
int front,rear,len;
int MaxSize;
};
void InitQueue(Queue Q); //构造一个空队列 Q
int EmptyQueue(Queue Q); //判断队列Q是否为空,若空返回1,否则返回0
void EnQueue(Queue Q, ElemType item); //元素 item 进队列Q
ElemType OutQueue(Queue Q); //队头元素出队列Q,并返回其值
ElemType PeekQueue(Queue Q); //返回队头元素值
void ClearQueue(Queue Q); //清空队
void InitQueue(Queue Q)
{
Q.MaxSize=10;
Q.len=0;
Q.queue=new ElemType[Q.MaxSize];
Q.front=Q.rear=0;
}
int EmptyQueue (Queue Q)
{
return Q.front==Q.rear;
}
void EnQueue (Queue Q, ElemType item)
{
if((Q.rear+1)%Q.MaxSize==Q.front){
int k=sizeof(ElemType);
Q.queue=(ElemType*)realloc(Q.queue,2*Q.MaxSize*k);
if(Q.rear!=Q.MaxSize-1){
for(int i=0;i=Q.rear;i++)
Q.queue[i+Q.MaxSize]=Q.queue[i];
Q.rear+=Q.MaxSize;
}
Q.MaxSize=2*Q.MaxSize;
}
Q.rear=(Q.rear+1)%Q.MaxSize;
Q.queue[Q.rear]=item;
Q.len++;
}
ElemType OutQueue (Queue Q)
{
if(Q.front==Q.rear){
cerrqueue is empty!endl;
exit(1);
}
Q.front=(Q.front+1)%Q.MaxSize;
Q.len--;
return Q.queue[Q.front];
}
ElemType PeekQueue (Queue Q)
{
if(Q.front==Q.rear){
cerrqueue is empty!endl;
exit(1);
}
return Q.queue[(Q.front+1)%Q.MaxSize];
}
void ClearQueue (Queue Q)
{
if(Q.queue!=NULL)
delete []Q.queue;
Q.front=Q.rear=0;
Q.queue=NULL;
Q.MaxSize=0;
}
CPP文件
#includeiostream.h
#includestdio.h
#includestdlib.h
#includestring.h
typedef struct{
char name[10];
char sex;
}dancer;
typedef dancer ElemType;
#includetext9.h
void partner()
{
Queue QM,QF;
dancer t;
char ch,oldch;
int i;
InitQueue(QM);
InitQueue(QF);
oldch= ;
printf(请输入跳舞者的姓名和性别(以# #结束):\n);
while((ch=getchar())!=#||oldch!=#)
{
i=0;
while(ch!=\n)
{
if(ch!= )
t.name[i++]=ch;
else{
ch=getchar();
t.sex=ch;
您可能关注的文档
最近下载
- 奥数常见裂项法经典裂项试题和裂项公式.pdf
- 2026年第六师五家渠市“百名硕士进六师”高层次人才引进备考题库及完整答案详解一套.docx VIP
- [国珍松花粉的功效与作用.ppt VIP
- 2026年第六师五家渠市“百名硕士进六师”高层次人才引进备考题库及1套完整答案详解.docx VIP
- 松花粉的功效与作用.doc VIP
- 苏教版六年级数学上册期末试卷(打印版).doc VIP
- 最优化理论与方法金海燕课后答案.pdf
- 高三病句复习充补资料——句子成分.doc VIP
- 新版教科版三年级上册科学全册教案教学设计.docx VIP
- 精品解析:山东省滨州市邹平市2023-2024学年高一下学期期中检测数学试题(解析版).docx VIP
原创力文档

文档评论(0)