数据结构实例应用(包含源代码)
一、停车场管理
/*停车场管理队列实现,离开时间比进入时间早的情况没做处理*/ #include \
#define MAXNUM 2 /*车库容量*/ #define price 0.05 /*每车每分钟费用*/ typedef struct time {
int hour; int min;
}Time; /*时间结点*/ typedef struct node {
char num[10]; Time reach; Time leave;
}CarNode; /*车辆信息结点*/ typedef struct NODE {
CarNode *stack[MAXNUM+1]; int top;
}SeqStackCar; /*模拟车站*/ typedef struct car {
CarNode *data; struct car *next; }QueueNode;
typedef struct Node {
QueueNode *head; QueueNode *rear;
}LinkQueueCar; /*模拟通道*/
void StackInit(SeqStackCar *s) /*初始化栈*/ {
int i;
s->top=0;
for(i=0;i<=MAXNUM;i++) s->stack[s->top]=NULL; }
int QueueInit(LinkQueueCar *Q) /*初始化便道*/ {
Q->head=(QueueNode *)malloc(sizeof(QueueNode));
if(Q->head!=NULL) {
Q->head->next=NULL; Q->rear=Q->head; return OK; } else return ERROR; }
void Print(CarNode *p,int room) /*打印出站车的信息*/ {
int A1,A2,B1,B2;
printf(\请输入离开的时间:/**:**/\
scanf(\ printf(\离开车辆的车牌号为:\ puts(p->num);
printf(\其到达时间为: %d:%d\ printf(\离开时间为: %d:%d\ A1=p->reach.hour; A2=p->reach.min; B1=p->leave.hour; B2=p->leave.min;
printf(\应交费用为: %2.1f元\ free(p); }
int Arrival(SeqStackCar *Enter,LinkQueueCar *W) /*车辆到达*/ {
CarNode *p; QueueNode *t;
p=(CarNode *)malloc(sizeof(CarNode)); printf(\请输入车牌号(例:陕A1234):\ getchar(); gets(p->num);
if(Enter->top Enter->top++; printf(\车辆在车场第%d位置.\ printf(\请输入到达时间:/**:**/\ scanf(\ Enter->stack[Enter->top]=p; return OK; } else /*车场已满,车进便道*/ { printf(\该车须在便道等待!\ t=(QueueNode *)malloc(sizeof(QueueNode)); t->data=p; t->next=NULL; W->rear->next=t; W->rear=t; return OK; } } void Leave(SeqStackCar *Enter,SeqStackCar *Temp,LinkQueueCar *W)/*车辆离开*/ { int room; CarNode *p,*t; QueueNode *q; /*判断车场内是否有车*/ if(Enter->top>0) /*有车*/ { while(TRUE) /*输入离开车辆的信息*/ { printf(\请输入车在车场的位置/1--%d/:\ scanf(\ if(room>=1&&room<=Enter->top) break; } while(Enter->top>room) /*车辆离开*/ { Temp->top++; Temp->stack[Temp->top]=Enter->stack[Enter->top]; Enter->stack[Enter->top]=NULL; Enter->top--; } p=Enter->stack[Enter->top]; Enter->stack[Enter->top]=NULL; Enter->top--; while(Temp->top>=1) { Enter->top++; Enter->stack[Enter->top]=Temp->stack[Temp->top]; Temp->stack[Temp->top]=NULL; Temp->top--; } Print(p,room); /*判断通道上是否有车及车站是否已满*/ if((W->head!=W->rear)&&Enter->top q=W->head->next; t=q->data; Enter->top++; printf(\便道的%s号车进入车场第%d位置.\ printf(\请输入现在的时间/**:**/:\ scanf(\ W->head->next=q->next; if(q==W->rear) W->rear=W->head; Enter->stack[Enter->top]=t; free(q); } else printf(\便道里没有车.\\n\ } else printf(\车场里没有车.\没车*/ } void List1(SeqStackCar *S) /*列表显示车场信息*/ { int i; if(S->top>0) /*判断车站内是否有车*/ { printf(\车场:\ printf(\位置 到达时间 车牌号\\n\ for(i=1;i<=S->top;i++) { printf(\ printf(\ puts(S->stack[i]->num); } } else printf(\车场里没有车\} void List2(LinkQueueCar *W) /*列表显示便道信息*/ { QueueNode *p; p=W->head->next; if(W->head!=W->rear) /*判断通道上是否有车*/ { printf(\等待车辆的号码为:\ while(p!=NULL) { puts(p->data->num); p=p->next; } } else printf(\便道里没有车.\} void List(SeqStackCar S,LinkQueueCar W) { int flag,tag; flag=1; while(flag) { printf(\请选择 1|2|3:\ printf(\车场\\n2.便道\\n3.返回\\n\ while(TRUE) { scanf(\ if(tag>=1||tag<=3) break; else printf(\请选择 1|2|3:\ } switch(tag) { case 1:/*列表显示车场信息*/ List1(&S); break; case 2:/*列表显示便道信息*/ List2(&W); break; case 3:/*列表显示便道信息*/ flag=0; break; default: break; } } } void main() { SeqStackCar Enter,Temp; LinkQueueCar Wait; int ch; StackInit(&Enter); /*初始化车站*/ StackInit(&Temp); /*初始化让路的临时栈*/ QueueInit(&Wait); /*初始化通道*/