sqstack.h
#ifndef SQSTACK_H__
#define SQSTACK_H__
#define SIZE 32
typedef int type;
typedef struct node_st
{
type data[SIZE];
int top;
}sqstack;
sqstack *st_create(void);
int st_isempty(sqstack *);
int st_push(sqstack *,type *);
int st_pop(sqstack *,type *);
int st_top(sqstack *,type *);
void st_travel(sqstack *);
void st_destory(sqstack *);
#endif
sqstack.c
#include<stdio.h>
#include<stdlib.h>
#include"sqstack.h"
sqstack *st_create(void)
{
sqstack *st;
st = malloc(sizeof(*st));
if(st == NULL)
return NULL;
st->top = -1;
return st;
}
int st_isempty(sqstack *st)
{
return (st->top == -1);
}
int st_push(sqstack *st,type *data)
{
if(st->top == SIZE -1)
return -1;
st->data[++st->top] = *data;
return 0;
}
int st_pop(sqstack *st,type *data)
{
if(st_isempty(st))
return -1;
*data = st->data[st->top--];
return 0;
}
int st_top(sqstack *st,type *data)
{
if(st_isempty(st))
return -1;
*data = st->data[st->top];
return 0;
}
void st_travel(sqstack *st)
{
int i;
if(st_isempty(st))
return ;
for(i = 0; i < st->top;i++)
printf("%d ",st->data[i]);
printf("\n");
}
void st_destory(sqstack *st)
{
free(st);
}
queue.h
#ifndef QUEUE_H__
#define QUEUE_H__
typedef int datatype;
#define MAXSIZE 32
typedef struct
{
datatype data[MAXSIZE];
int head,tail;
}queue;
queue *qu_create();
int qu_isempty(queue *);
int qu_enqueue(queue *,datatype *);
int qu_dequeue(queue *,datatype *);
void qu_travel(queue *);
void qu_clear(queue *);
void qu_destroy(queue *);
#endif
queue.c
#include<stdio.h>
#include<stdlib.h>
#include"queue.h"
queue *qu_create()
{
queue *sq;
sq = malloc(sizeof(*sq));
if(sq == NULL)
return NULL;
sq->head = 0;
sq->tail = 0;
return sq;
}
int qu_isempty(queue *sq)
{
return (sq->head == sq->tail);
}
int qu_enqueue(queue *sq,datatype *x)
{
id((sq->tail + 1)% MAXSIZE == sq->head)//队列判断 是不是满的
return -1;
sq->tail = (sq->tail + 1)% MAXIZIE;
sq->data[sq->tail] = *x;
return 0;
}
int qu_dequeue(queue *sq,datatype *x)
{
if(qu_isqueue(sq))
return -1;
sq->head = (sq->head+1)%MAXSIZE;
*x = sq->data[sq->head];
return 0;
}
void qu_travel(queue *sq)
{
if(sq->head = sq->tial)
return ;
i = (sq->head +1) % MAXSIZE;
while(i != sq->tial)
{
printf("%d ",sq->tail[i]);
i = (i + 1)%MAXSIZE;
}
printf("%d\n",sq->data[i]);
}
void qu_clear(queue *sq)
{
sq->head = sq->tail;
}
void qu_destroy(queue *sq)
{
free(sq);
}
main.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include"queue.h"
#include"sqstack.h"
#define NR_BALL 27
static int check(queue *qu)
{
int i = (qu->head+1)%MAXSIZE;
whlie(i != qu->tail)
{
if(qu->data[i] > qu->data[(i+1)%MAXSIZE])
return 0;
i = (i+1)%MAXSIZE;
}
return 1;
}
int main()
{
int time = 0;
int t,value,i;
queue *qu;
sqstack *st_min,*st_fivemin,*st_hour;
qu = qu_create();
if(qu == NULL)
exit(1);
st_min = st_create();
if(st_min == NULL)
exit(1);
st_fivemin = st_create();
if(st_fivemin == NULL)
exit(1);
st_hour = st_create();
if(st_hour == NULL)
exit(1);
for(i = 1;i <= NR_BALL;i++)
qu_enqueue(qu,&i);
qu_travel(qu);
while(1)
{
qu_dequeue(qu,&t);
time++;
if(st_min->top != 3)
{
st_push(st_min,&t);
}
else
{
while(!st_isempty(st_min))
{
st_pop(st_min,&value);
qu_enqueue(qu,&value);
}
if(st_fivemin->top !=10)
{
st_push(st_fivemin,&t);
}
else
{
while(!st_isempty(st_fivemin))
{
st_pop(st_fivemin,&value);
qu_enqueue(qu,&value);
}
if(st_hour->top !=10)
st_push(st_hour,&t);
else
{
while(!st_isempty(st_hour))
{
st_pop(st_hour,&value);
qu_enqueue(qu,&value);
}
qu_enqueue(qu,&t);
qu_travel(qu);
sleep(1);
if(check(qu))
break;
}
}
}
}
printf("time = %d\n",time);
qu_destroy(qu);
st_destory(st_min);
st_destory(st_fivemin);
st_destory(st_hour);
exit(0);
}
Makefile
all:ball_clock
sqstack:sqstack.o main.o queue.o
$(CC) $^ -o $@
clean:
rm ball_clock *.o -rf