}
free(p); return OK;
源程序
Stop1.h:
#include
//------------------------函数结果状态代码 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0
#define TNFEASIBLE -1 #define OVERFLOW -2
//Status 是函数的类型,其值是函数结果状态代码 typedef int Status;
#define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #define MONEY 3 Stop2.h:
#include\
typedef struct ElemType{ char a[3]; int num; int time; }ElemType;
typedef struct SqStack{ ElemType *base; ElemType *top; int stacksize; }SqStack;//栈的表示 typedef struct QNode{ ElemType data; struct QNode *next;
}QNode,*QueuePtr;//队列的表示 typedef struct LinkQueue{ QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;
Status InitStack(SqStack &S);//构造空栈 Status Push(SqStack &S,ElemType e);//进栈 Status Pop(SqStack &S,ElemType &e);//出栈
Status InitQueue(LinkQueue &Q);//构造一个空队列 Status EnQueue(LinkQueue &Q,ElemType e);//入队 Status DeQueue(LinkQueue &Q,ElemType &e);//出队
Stop.cpp:
#include\
Status InitStack(SqStack &S){//构造空栈
S.base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); if(!S.base) exit(OVERFLOW); S.top=S.base;
S.stacksize=STACK_INIT_SIZE; return OK; }
Status Push(SqStack &S,ElemType e){//插入元素e为新的栈顶元素 if(S.top-S.base>=S.stacksize){//栈满,追加存储空间 S.base=(ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType)); if(!S.base) exit(OVERFLOW); S.top=S.base+S.stacksize; S.stacksize+=STACK_INIT_SIZE; } *S.top++=e; return OK; }
Status Pop(SqStack &S,ElemType &e){//出栈 if (S.top==S.base) return OK; e=*--S.top; return OK; }
/***********************************************************************队列*/ Status InitQueue(LinkQueue &Q){//构造一个空队列 Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode)); if(!Q.front) exit(OVERFLOW); Q.front->next=NULL; return OK; }
Status EnQueue(LinkQueue &Q,ElemType e){//插入元素e为Q的新队列 struct QNode *p;
p=(QueuePtr)malloc(sizeof(QNode)); if(!p) exit(OVERFLOW); p->data=e;p->next=NULL; Q.rear->next=p; Q.rear=p; return OK; }
Status DeQueue(LinkQueue &Q,ElemType &e){ struct QNode *p; if(Q.front=Q.rear) return ERROR; p=Q.front->next=p->next; if(Q.rear==p) Q.rear=Q.front; free(p); return OK; }
Stop_main.cpp: #include\ main(){ int i,t,f,m,n,s1_num,Q_num; struct SqStack s1,s2; struct LinkQueue Q; struct ElemType e,e1; s1_num=0;Q_num=0;t=0;m=0; InitStack(s1);InitStack(s2);InitQueue(Q); printf(\停车场的容量是:\ scanf(\ printf(\输入车辆信息(E为退出,A为进入标志,D为离开标志,车牌号 时间空格隔开):\\n\ scanf(\ while(strcmp(e1.a,\ if(strcmp(e1.a,\当有车辆进来的时候 if(s1_num else Push(s2,e);} if(t==0&&m==0){printf(\此车为便道内车,故无需收费\\n\ while(s2.top!=s2.base){ Pop(s2,e);Push(s1,e);s1_num++;} if(Q.front!=Q.rear){ DeQueue(Q,e);Push(s1,e);s1_num++;} } else printf(\错误\\n\ printf(\输入数据\\n\ scanf(\ 运行结果
相关推荐: