栈和队列的应用:球钟问题(栈和队列的应用)



#<stdio.h>
#<conio.h>
#<malloc.h>
# N 30/*27<=N<=127*/
# NULL 0
typedef struct Qnode/*定义队列接点类型*/
{
data;
struct Qnode *next;
}Qnode,*Queueptr;
Queueptr p,q;
typedef struct
{
Queueptr front;
Queueptr rear;
}linkQueue;
linkQueue *Q;/*定义队列*/
typedef struct stack4/*定义分钟栈*/
{
Queueptr top;
Queueptr base;
}stack4;
stack4 *min;
typedef struct stack11/*定义5分钟栈类型(小时栈类型)*/
{
Queueptr top;
Queueptr base;
}stack11;
stack11 *fmin,*hour;/*建立 5分钟和小时栈*/
minute;
void initQueue(linkQueue *O)/*队列化*/
{
i=1,j;
Queueptr head,;
head=(Qnode *)malloc((Qnode));
(!head)
{
prf(\"failture for applying a area!\\n\");
exit(0);
}
O->front=p=head;
head->data=i;
head->next=NULL;
while(i<N)
{
=(Qnode *)malloc((Qnode));
(!)
{
prf(\"failture for applying a area!\\n\");
exit(0);
}
head->next=;
head=head->next;
head->data=i;
}
head->next=NULL;
O->rear=head;
}
/*主*/
{
day=1,j=1;/*天数*/
initQueue(Q);
while(1)/*循环*/
{
for(minute=1;minute<1440;minute)
{
Q->front=Q->front->next;
p->next=NULL;
min->base=min->top=p;
min->top;
p=Q->front;
(minute%40)
{
Q->front=Q->front->next;
p->next=NULL;
min->top=p;
min->top;
p=Q->front;
min->top--;
Q->rear->next=min->top;
min->top--;
}
(minute%50)
{
Q->front=Q->front->next;
p->next=NULL;
fmin->base=fmin->top=p;
fmin->top;
p=Q->front;
fmin->top--;
Q->rear->next=min->top;
fmin->top--;
}
(minute%600)
{
Q->front=Q->front->next;
p->next=NULL;


hour->base=hour->top=p;
hour->top;
p=Q->front;
hour->top--;
Q->rear->next=hour->top;
hour->top--;
}
}
(Q->front->data1)/*测试是否符合条件,若符合跳出,否则继续循环下去*/
{
while(Q->front->data(Q->front->next->data+1)&&j<=N)
{
Q->front=Q->front->next;
j;
}
(Q->front->dataN)
;
}
day;
}
prf(\"after %d days,the condition will the original!\",day);/*输出天数*/
}
Tags:  队列和栈有什么区别 栈和队列的区别 栈和队列 栈和队列的应用

延伸阅读

最新评论

发表评论