#define FALSE 0
#define TURE 1
#define N 3
#include<stdio.h>
typedef struct
{
int number;
int t_come;
int t_go;
} car,*CAR;
typedef struct Stack
{
CAR c[N];
int n;
}Stack;
typedef struct Node
{
car *Car;
struct Node *next;
}LinkQueueNode;
typedef struct
{
LinkQueueNode *front;
LinkQueueNode *rear;
}LinkQueue;
Stack S;
LinkQueue Q;
LinkQueue ranglu;
int Time;
void print();
int QInit_queue()
{
Q.front=(LinkQueueNode *)
malloc(sizeof(LinkQueueNode));
if(Q.front !=NULL)
{
Q.rear =Q.front ;
Q.front ->next =NULL;
return( TURE );
}
else return(FALSE);
}
int rangluInit_queue()
{
ranglu.front=(LinkQueueNode *)
malloc(sizeof(LinkQueueNode));
if(ranglu.front !=NULL)
{
ranglu.rear =ranglu.front ;
ranglu.front ->next =NULL;
return( TURE );
}
else return(FALSE);
}
int Enter_queue( car x)
{
int i=0;
LinkQueueNode *temp;
temp=(LinkQueueNode *)
malloc(sizeof(LinkQueueNode));
if(temp == NULL)
return( FALSE);
else
{
*(temp->Car) = x;
temp->next = NULL;
Q.rear ->next=temp;
Q.rear=temp;
i++;
return(i);
}
}
int Enter_ranglu_queue( car x)
{
int i=0;
LinkQueueNode *temp;
temp=(LinkQueueNode *)
malloc(sizeof(LinkQueueNode));
if(temp == NULL)
return( FALSE);
else
{
*(temp->Car) = x;
temp->next = NULL;
ranglu.rear ->next=temp;
ranglu.rear=temp;
i++;
return(i);
}
}
CAR DeleteQueue()
{
LinkQueueNode *p;
if(Q.front == Q.rear)
return( FALSE);
p=Q.front->next;
Q.front ->next=p->next ;
if(Q.rear==p)
Q.rear=Q.front;
p->Car->t_come =Time;
return(p->Car);
}
CAR Delete_ranglu_queue()
{
LinkQueueNode *p,*pre;
if(ranglu.front == ranglu.rear)
return( FALSE);
p=ranglu.rear;
p->Car->t_come =Time;
pre=ranglu.front;
while(pre->next->next!=NULL) pre=pre->next;
ranglu.rear=pre;
return(p->Car);
free(p);
}
comein()
{
car mm;
printf("\n\tPlease put in the coming car's number: ");
scanf("%d",&mm.number);
printf("\n\tPlease put in the car's come time: ");
scanf("%d",&mm.t_come);
Time=mm.t_come ;
if(S.n<N)
{
car *temp;
temp=(car *)malloc(sizeof(car));
*temp=mm;
S.c[S.n]=temp;
S.n++;
printf("\nThe car insert the stack %d",S.n);
}
else
{
printf("\n\rThe car insert the queue %d",Enter_queue(mm));
}
}
goout()
{
int i,j;
int str;
CAR mm;
car *huifu;
printf("\n\tPlease put in the number of the going car: ");
scanf("%d",&str);
for(i=0;i<S.n;i++)
{
if(S.c[i]->number!=str) i++;
if(S.c[i]->number==str) break;
Enter_ranglu_queue(*S.c[i]);
}
if(i==N || S.n==0)
printf(" Not in the stack");
else{
printf("\n\tPlease put in the go time: ");
scanf("%d",&Time);
S.c[i]->t_go=Time;
printf("%d %d",i+1,S.c[i]->number);
printf("\n Stop time *money is %d*2.5",S.c[i]->t_go - S.c[i]->t_come);
while(ranglu.rear!=ranglu.front&&i>=0)
{
*(S.c[i])=*(Delete_ranglu_queue());
i--;
}
if(Q.rear!=Q.front&&i>=0)
{S.c[i]=DeleteQueue();}
}
}
void print()
{
int i;
LinkQueueNode *temp=Q.front;
printf("\n\n----------------------------------------\n");
printf("The cars in the Stack\n");
printf("place \t number\n");
for(i=0;i<S.n;i++)
printf("%4d\t %4d \n",i+1, S.c[i]->number);
i=0;
if(Q.front != Q.rear)
{
printf("\nThe cars in the queue:\n");
printf("place \t number\n");
while(temp!=Q.rear)
{
i++;
temp=temp->next;
printf("%4d \t %4d \n",i, temp->Car->number);
}
}
else
printf("\nThere is no car in the queue.");
printf("\n------------------------ok------------");
}
main()
{
int k;
char ch;
S.n=0;
QInit_queue();
rangluInit_queue();
while(1)
{ fflush(stdin);
printf("\n\n\rPLEASE CHOSE:\n\tC: COME IN\n\tG: GO OUT \n\tF: LOOK\n\t0: EXIT");
ch=getch();
if(ch=='c'||ch=='C')
comein();
else if(ch=='g'||ch=='G')
goout();
else if(ch=='f'||ch=='F')
print();
else if(ch=='0'||ch=='O' || ch=='o'||ch==27)
break;
}
for(k=0;k<N;k++)
{
printf("%d",S.c[k]->number);
}
}
帮帮忙改改错。。。