停车场管理代码(帮忙加个注释!)

aimr0215 2012-01-08 12:52:42
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define SMAX 2 /*车库容量*/
#define QMAX 10 /*每辆车每时刻的费用*/

typedef struct{
char ad;
int num;
int time;
} ElemType; /*元素类型*/

typedef struct{
ElemType *data;
int top;
} SeqStack; /*顺序栈类型*/

typedef struct{
ElemType *data;
int front,rear;
} SeqQueue; /*顺序队类型*/

typedef struct node{
ElemType data;
struct node *link;
} NodeQueue; /*链队结点类型*/

typedef struct{
NodeQueue *front,*rear;
} LinkQueue; /*链队类型*/

void InitStack(SeqStack *S) /*初始化栈*/
{
S->data=(ElemType*)malloc(SMAX*sizeof(ElemType));
S->top=-1;
}

int SEmpty(SeqStack *S)
{
if(S->top==-1) return 1;
else return 0;
}

int SFull(SeqStack *S)
{
if(S->top==SMAX-1) return 1;
else return 0;
}

int Push(SeqStack *S,ElemType e)
{
if(S->top==SMAX-1) return 0;
S->data[++S->top]=e;
return 1;
}

int Pop(SeqStack *S,ElemType *e)
{
if(S->top==-1) return 0;
*e=S->data[S->top--];
return 1;
}

void InitQueue(SeqQueue *Q)
{
Q->data=(ElemType*)malloc(QMAX*sizeof(ElemType));
Q->front=Q->rear=0;
}

int QEmpty(SeqQueue *Q)
{
if(Q->front==Q->rear) return 1;
else return 0;
}

int EnQueue(SeqQueue *Q,ElemType e)
{
if((Q->rear+1)%QMAX==Q->front) return 0;
Q->data[Q->rear]=e;
Q->rear=(Q->rear+1)%QMAX;
return 1;
}

int DeQueue(SeqQueue *Q,ElemType *e)
{
if(Q->front==Q->rear) return 0;
*e=Q->data[Q->front];
Q->front=(Q->front+1)%QMAX;
return 1;
}

void InitLQueue(LinkQueue *Q) /*初始化便道*/
{
Q->front=Q->rear=NULL;
}

int LQEmpty(LinkQueue *Q)
{
if(Q->front==NULL) return 1;
return 0;
}

int EnLQueue(LinkQueue *Q,ElemType e)
{
NodeQueue *p;
p=(NodeQueue*)malloc(sizeof(NodeQueue));
if(p==NULL) return 0;
p->data=e;
p->link=NULL;
if(Q->front==NULL)
{
Q->front=Q->rear=p;
}
else
{
Q->rear->link=p;
Q->rear=p;
}
return 1;
}

int DeLQueue(LinkQueue *Q,ElemType *e)
{
NodeQueue *p;
if(Q->front==NULL) return 0;
p=Q->front;
Q->front=p->link;
*e=p->data;
free(p);
return 1;
}

void main()
{
SeqStack ST,temp;
LinkQueue Q;
NodeQueue *p,*q;
ElemType x,y;

InitStack(&ST);InitStack(&temp);
InitLQueue(&Q);

scanf("%c %d %d",&x.ad,&x.num,&x.time);
while(x.ad=='A'||x.ad=='D')
{
printf("%c %d %d\n",x.ad,x.num,x.time);
if(x.ad=='A')
{
if(!SFull(&ST))
{
Push(&ST,x);
printf("Arrive in the park:%d\n",ST.top);
}
else
{
EnLQueue(&Q,x);
printf("Arrive in the roadedge:\n");
}
}
else
{
while(!SEmpty(&ST)&&ST.data[ST.top].num!=x.num)
{
Pop(&ST,&y);
Push(&temp,y);
}
if(!SEmpty(&ST))
{
Pop(&ST,&y);
printf("Stop time:%d\n",x.time-y.time);
while(!SEmpty(&temp))
{
Pop(&temp,&y);
Push(&ST,y);
}
}
else
{
if(!LQEmpty(&Q))
{
p=Q.front;
while(p->data.num!=x.num)
{
q=p;
p=p->link;
}
if(p==Q.front)
{
Q.front=p->link;
}
else
{
q->link=p->link;
}
free(p);
}
}
}
scanf("%*c%c %d %d",&x.ad,&x.num,&x.time);
}
}
...全文
212 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
aimr0215 2012-01-09
  • 打赏
  • 举报
回复
是课程设计老师要求的,还要写那么多了,调试分析什么的。
鲤鱼 2012-01-08
  • 打赏
  • 举报
回复
看你什么用了;
如果是快速的用,那么知道函数功能,如何用就好;
如果是学习,那就得仔细看看每个函数是怎么写的;然后再自己写出来;
li08240418 2012-01-08
  • 打赏
  • 举报
回复
能看出来干啥就可以了
面包大师 2012-01-08
  • 打赏
  • 举报
回复

int SEmpty(SeqStack *S)/*判断栈是否为空*/
{
if(S->top==-1) return 1;
else return 0;
}

int SFull(SeqStack *S)/*判断栈是否已满*/
{
if(S->top==SMAX-1) return 1;
else return 0;
}

int Push(SeqStack *S,ElemType e) /*进栈*/
{
if(S->top==SMAX-1) return 0;
S->data[++S->top]=e;
return 1;
}

int Pop(SeqStack *S,ElemType *e) /*出栈*/
{
if(S->top==-1) return 0;
*e=S->data[S->top--];
return 1;
}

void InitQueue(SeqQueue *Q)/*初始化队列*/
{
Q->data=(ElemType*)malloc(QMAX*sizeof(ElemType));
Q->front=Q->rear=0;
}

int QEmpty(SeqQueue *Q)/*判断队列是否为空*/
{
if(Q->front==Q->rear) return 1;
else return 0;
}

int EnQueue(SeqQueue *Q,ElemType e)/*判断队列是否已满*/
{
if((Q->rear+1)%QMAX==Q->front) return 0;
Q->data[Q->rear]=e;
Q->rear=(Q->rear+1)%QMAX;
return 1;
}

int DeQueue(SeqQueue *Q,ElemType *e)/*删除队列中的元素*/
{
if(Q->front==Q->rear) return 0;
*e=Q->data[Q->front];
Q->front=(Q->front+1)%QMAX;
return 1;
}

void InitLQueue(LinkQueue *Q) /*初始化便道*/
{
Q->front=Q->rear=NULL;
}

int LQEmpty(LinkQueue *Q)/*判断便道是否为空*/
{
if(Q->front==NULL) return 1;
return 0;
}

int EnLQueue(LinkQueue *Q,ElemType e)/*增加便道中的元素*/
{
NodeQueue *p;
p=(NodeQueue*)malloc(sizeof(NodeQueue));
if(p==NULL) return 0;
p->data=e;
p->link=NULL;
if(Q->front==NULL)
{
Q->front=Q->rear=p;
}
else
{
Q->rear->link=p;
Q->rear=p;
}
return 1;
}

int DeLQueue(LinkQueue *Q,ElemType *e)/*删除便道中的元素*/
{
NodeQueue *p;
if(Q->front==NULL) return 0;
p=Q->front;
Q->front=p->link;
*e=p->data;
free(p);
return 1;
}

这样可以否???

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧