数据结构之队列
2011-05-20 11:06
323 查看
#include<stdio.h>
#define QueueSize 20
typedef struct {
char name[QueueSize];
char sex;
}Person;
typedef struct {
Person *dancer;
int front;
int rear;
int count;
} CirQueue;
void InitQueue(CirQueue *Q)
{
Q->front=Q->rear=0;
Q->count=0;
}
int QueueEmpty(CirQueue *Q)
{
return Q->count==0;
}
int QueueFull(CirQueue *Q)
{ return Q->count == QueueSize;
}
Person QueueFront(CirQueue *Q)
{
if(QueueEmpty(Q)) printf("The queue is empty./n");
return Q->dancer[Q->front];
}
void EnQueue(CirQueue *Q,Person dancer)
{
if(QueueFull(Q)) printf("Team full!/n");
Q->count++;
Q->dancer[Q->rear]=dancer;
Q->rear=(Q->rear+1)%QueueSize;
}
Person DeQueue(CirQueue *Q)
{
Person temp;
if(QueueEmpty(Q))printf("Team empty!/n");
temp=Q->dancer[Q->front];
Q->count--;
Q->front=(Q->front+1)%QueueSize;
return temp;
}
void DancePartners(Person *dancer, int num)
{ int i;
Person P;
CirQueue Mdancers, Fdancers;
InitQueue(&Mdancers);
InitQueue(&Fdancers);
for(i=0;i<num;i++) {
P=dancer[i];
if(P.sex=='F') EnQueue(&Fdancers, P);
else EnQueue(&Mdancers, P);
}
printf("The partner mix is:/n");
while(!QueueEmpty(&Fdancers)&&!QueueEmpty(&Mdancers)) {
P=DeQueue(&Fdancers);
printf("%s,",P.name);
P=DeQueue(&Mdancers);
printf("%s/n",P.name);
}
if(!QueueEmpty(&Fdancers)) {
printf("There are %d women in the waiting./n",Fdancers.count);
P=QueueFront(&Fdancers);
printf("The first waiting's woman is:%s./n", P.name);
}
if(!QueueEmpty(&Mdancers)) {
printf("There are %d men in the waiting./n",Mdancers.count-1);
P=QueueFront(&Mdancers);
printf("The first waiting's man is:%s./n", P.name);
}
}
int main()
{
int i,j;
Person dancer[QueueSize];
printf("/n Please enter the number of the dances:");
scanf("%d",&j);
while(j<=0)
{
printf("Input error, please input again:");
scanf("%d",&j);
}
for(i=1;i<=j;i++)
{
printf("Please input the %d honored person's name :",i);
scanf("%s",&dancer[i-1].name);
printf("Please input the %d honored person's sex (F/M):",i);
scanf("%s",&dancer[i-1].sex);
while(dancer[i-1].sex!='F'&&dancer[i-1].sex!='M')
{
printf("Input error, please input again:");
scanf("%s",&dancer[i-1].sex);
}
}
DancePartners(dancer,i);
}
相关文章推荐
- NDK 数据结构之队列与栈等的实现
- 数据结构---队列
- 数据结构实验之队列一:排队买饭
- 数据结构实验之队列一:排队买饭
- 数据结构——循环队列操作
- 4.[数据结构和算法分析笔记]队列 Queue
- 2135-->数据结构实验之队列一:排队买饭
- 数据结构之顺序队列的操作(C语言)
- 数据结构复习——队列解决报数问题
- 第六周--数据结构之自建算法库之链式队列
- (编程训练)再回首,数据结构——入队列、出队列操作
- 数据存储的常用结构 堆栈、队列、数组、链表
- 数据结构——栈与队列
- 数据结构(队列--两个栈实现)
- 数据结构基础温故-3.队列
- 数据结构(严蔚敏)之六——链式队列c语言实现
- 数据结构顺序队列的各种操作
- 看数据结构写代码(15)链式队列的实现(总结篇)
- 数据结构(三)——双链表、链式栈、链式队列 及实现
- sdut 2088 数据结构实验之栈与队列十一:refresh的停车场