#include
#include
#include
#include
#define MAXSIZE 100
typedef struct Ttime{
int t_hour;
int t_min;
int t_sec;
}Ttime;
typedef struct Mcar{
int number;
Ttime t[2];
}Mcar;
typedef struct{
Mcar elements[MAXSIZE];
int top;
}CarStop;
typedef struct node{
Mcar date;
struct node *next;
}LinkQueue;
typedef struct{
LinkQueue *front;
LinkQueue *rear;
}Queue;
void InitQueue(Queue *Q){
Q -> front = (LinkQueue *) malloc(sizeof(LinkQueue));
Q -> front -> next = NULL;
Q -> rear = Q -> front;
}
int IsEmptyH(Queue Q){
if(Q.front == Q.rear)
return 1;
else
return 0;
}
void EeQueue(Queue *Q, Mcar x){
Q -> rear -> next = (LinkQueue *)malloc(sizeof(LinkQueue));
Q -> rear = Q -> rear -> next;
Q -> rear -> date = x;
Q -> rear -> next = NULL;
}
Mcar DeQueue(Queue *Q){
LinkQueue *p;
Mcar y;
if(!IsEmpty(*Q)){
p = Q -> front -> next;
Q -> front -> next = p -> next;
if(p -> next == NULL)
Q -> rear = Q -> front;
y = p -> date;
free(p);
return y;
}
}
int IsEmpty(CarStop S){
if(S.top == 0)
return 1;
else
return 0;
}
int IsFull(CarStop S){
if(S.top == MAXSIZE)
return 1;
else
return 0;
}
int Init(CarStop *S){
S -> top = 0;
}
void PutIn(CarStop *S, Mcar D){
if(!IsFull(*S)){
S -> elements[S -> top] = D;
S -> top++;
}
else
printf("IS FULL");
}
Mcar Pop(CarStop *S){
if(!IsEmpty(*S)){
S -> top--;
return S -> elements[S -> top];
}
else
printf("IS NULL");
}
Mcar TopNum(CarStop S){
if(!IsEmpty(S)){
return S.elements[S.top - 1];
}
}
int serachOneCar(CarStop S, Mcar num){
int i = 0;
if(!IsEmpty(S)){
while(i <= S.top){
if(num.number == S.elements[i].number)
return 1;
i++;
}
}
else
return 0;
}
void getInOneCar(CarStop *CarStack, Queue *Q, Mcar car){
time_t secnow;
struct tm * tmptr;
time(&secnow);
tmptr = localtime(&secnow);
car.t[0].t_hour= tmptr -> tm_hour;
car.t[0].t_min = tmptr -> tm_min;
car.t[0].t_sec = tmptr -> tm_sec;
printf("please put in you car number:\n");
scanf("%d", &car.number);
if(!IsFull(*CarStack))
PutIn(CarStack, car);
else{
printf("car stop is full, please stay in biandao\n");
EeQueue(Q, car);
}
printf("you car's intime is :[%d]:[%d]:[%d]\n\n\n", car.t[0].t_hour, car.t[0].t_min, car.t[0].t_sec);
}
void getOutOneCar(CarStop *CarStack, CarStop *CarStackL, Queue *Q, Mcar car){
Mcar lin;
time_t secnow;
struct tm * tmptr;
time(&secnow);
tmptr = localtime(&secnow);
car.t[1].t_hour= tmptr -> tm_hour;
car.t[1].t_min = tmptr -> tm_min;
car.t[1].t_sec = tmptr -> tm_sec;
printf("please put in you car number:\n");
scanf("%d", &car.number);
if(IsEmpty(*CarStack) || !serachOneCar(*CarStack, car))
printf("soory, there is no the car\n");
else{
while(1){
lin = Pop(CarStack);
if(lin.number == car.number)
break;
else
PutIn(CarStackL, lin);
}
while(!IsEmpty(*CarStackL)){
PutIn(CarStack, Pop(CarStackL));
}
if(!IsEmptyH(*Q))
PutIn(CarStack, DeQueue(Q));
printf("\n\nyou car's intime is :[%d]:[%d]:[%d]\n", lin.t[0].t_hour, lin.t[0].t_min, lin.t[0].t_sec);
printf("you car's out time is :[%d]:[%d]:[%d]\n", car.t[1].t_hour, car.t[1].t_min, car.t[1].t_sec);
int alltime = (car.t[1].t_hour - lin.t[0].t_hour) * 3600 + (car.t[1].t_min - lin.t[0].t_min) * 60 + (car.t[1].t_sec - lin.t[0].t_sec);
printf("you car's all time is second:[%d]\n", alltime);
}
}
int main(){
CarStop CarStack, CarStackL;
Queue Q;
LinkQueue *pp;
InitQueue(&Q);
Init(&CarStack);
Init(&CarStackL);
Mcar car;
//String caozuo;
int caozuo, i, j;
while(1){
printf("\t**************the car stop menu*************\n");
printf("\t\t***** 1. get in a car\n\n");
printf("\t\t***** 2. get out a car\n\n");
printf("\t\t***** 0. out the car stop\n\n");
printf("\t\t*****put in the else you can see all the car in the carstop\n\n");
printf("\t*****************************************\n");
printf("please put in you caozuo: \n");
scanf("%d", &caozuo);
switch(caozuo){
case 1: getInOneCar(&CarStack, &Q, car);
break;
case 2: getOutOneCar(&CarStack, &CarStackL, &Q, car);
break;
case 0: return 0; break;
default : break;
}
if(!IsEmpty(CarStack)){
i = 0;
printf("the all car is:\n");
while(i < CarStack.top){
printf("the [%d] is %d\n",i + 1, CarStack.elements[i].number);
i++;
}
}
else
printf("the car stop IS NULL\n");
printf("****************************\n");
if(!IsEmptyH(Q)){
j = 1;
pp=Q.front;
printf("the biandao's car is:\n");
while(pp!=Q.rear){
printf("the [%d] wait car is %d\n", j, pp -> next -> date.number);
pp = pp -> next;
j++;
}
}
else
printf("the biandao is NULL\n");
printf("****************************\n");
system("pause");
system("cls");
}
}
C语言编译通过
应用个简单的栈与队列实现该功能
自己先动手。。。