论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

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

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:45:14

*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define N 30                                                            /*27<=N<=127*/
#define NULL 0
typedef struct Qnode                                              /*定义队列的接点类型*/
{
int 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;                                                 /*建立五分钟和小时栈*/
int minute;
void initQueue(linkQueue *O)                                /*队列的初始化*/
{
int i=1,j;
Queueptr head,new;
head=(Qnode *)malloc(sizeof(Qnode));
if(!head)
{
  printf("failture for applying a area!\n");
   exit(0);
}
O->front=p=head;
head->data=i;
head->next=NULL;
while(i<N)
{
new=(Qnode *)malloc(sizeof(Qnode));
if(!new)
{
  printf("failture for applying a area!\n");
   exit(0);
}
head->next=new;
head=head->next;
head->data=++i;
}
head->next=NULL;
O->rear=head;
}
main()                                                /*主函数*/
{
int 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;
if(minute%4==0)
{
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--;
}
if(minute%5==0)
{
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--;
}
if(minute%60==0)
{
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--;
}
}
if(Q->front->data==1)                                                /*测试是否符合条件,若符合跳出,否则继续循环下去*/
{
  while(Q->front->data==(Q->front->next->data+1)&&j<=N)
     {
      Q->front=Q->front->next;
      j++;
     }
if(Q->front->data==N)
break;
}
day++;
}
printf("after %d days,the condition will return the original!",day);/*输出天数*/
}
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058