C# · 12月 20, 2021

数据结构 九

简单的停车场系统

Queue.h文件

#ifndef _QUEUE_H_

#define _QUEUE_H_

#define SIZE 3

#define FLAST 0

#define TRUE 1

//typedef int Data;

//数据

typedef struct node

{

int number;

int time;

}Node;

//栈结点

typedef struct stacknode

{

Node data;

struct stacknode *next;

}StackNode;

typedef struct stack

{

StackNode *top;

int sum;

}Stack;

//队列结点

typedef struct queuenode

{

Node infom;

struct queuenode *next;

}QueueNode;

//链式队列

typedef struct queue

{

QueueNode *front;

QueueNode *rear;

}Queue;

//初始化

int Init(Queue *park,Queue *wait,Stack *turn);

//主菜单

void Menu(Queue *park,Stack *turn,int num,int t);

//测队列长度

int Length(Queue q);

//进队列

int Push(Queue *q,int t);

//出队列

int Pop(Queue *q,int *num,int *t);

//停车

void park_car(Queue *wait,Queue *park);

//进栈

int Push1(Stack *s,int t);

//出栈

int Pop1(Stack *s,int *t);

//离开

void leave_car(Queue *wait,Queue *park,int t);

//显示停车场状况

void examine_car(Queue wait,Queue park);

#endif// _QUEUE_H_

Queue.c文件

#include “Queue.h”

#include

#include

//初始化

int Init(Queue *wait,Stack *turn)

{

QueueNode *newnode1=(QueueNode *)malloc(sizeof(QueueNode) );

if(NULL==newnode1)

return FLAST;

newnode1->next=NULL;

park->front=newnode1;

park->rear=newnode1;

QueueNode *newnode2=(QueueNode *)malloc(sizeof(QueueNode) );

if(NULL==newnode2)

return FLAST;

newnode2->next=NULL;

wait->front=newnode2;

wait->rear=newnode2;

turn->top=NULL;

turn->sum=0;

}

//主菜单

void Menu(Queue *wait,int t)

{

printf(“*****************************************n”);

printf(“* 欢迎来到停车场管理系统 *n”);

printf(“*****************************************n”);

printf(“* 请选择 *n”);

printf(“*****************************************n”);

printf(“* 1.停车: *n”);

printf(“* 2.离开: *n”);

printf(“* 3.查看停车场所有情况: *n”);

printf(“* 4.退出: *n”);

printf(“*****************************************n”);

int i;

printf(“请选择功能n”);

scanf(“%d”,&i);

switch(i)

{

case 1:

{

park_car(wait,park);

printf(“——-nihaon”);

Menu(wait,park,turn,num,t);

}

break;

case 2:

{

leave_car(wait,t);

Menu(wait,t);

}

break;

case 3:

{

examine_car(*wait,*park);

Menu(wait,t);

}

break;

case 4:

{

printf(“停车结束n”);

}

break;

default:

Menu(wait,t);

break;

}

}

//测队列长度

int Length(Queue q)

{

int i=0;

while(q.front!=NULL)

{

i++;

q.front=q.front->next;

}

return i;

}

//进队列

int Push(Queue *q,int t)

{

QueueNode *newnode=(QueueNode *)malloc(sizeof(QueueNode) );

if(NULL==newnode)

return FLAST;

newnode->infom.number=num;

newnode->infom.time=t;

newnode->next=NULL;

//if(q->rear!=NULL)

//{

q->rear->next=newnode;

q->rear=newnode;

//}

//else

//{

// q->rear=newnode;

/// q->front=newnode;

//}

return TRUE;

}

//出队列

int Pop(Queue *q,int *t)

{

if(NULL==q)

return FLAST;

int num1=*num;

int t1=*t;

if(q->front==NULL)

{

printf(“队列以空n”);

return FLAST;

}

num1=q->front->infom.number;

t1=q->front->infom.time;

QueueNode *temp=q->front;

q->fremp->next;

free(temp);

if(q->front==NULL)

q->rear=q->front;

printf(“%d,%dn”,num1,t1);

*num=num1;

*t=t1;

return TRUE;

}

//停车

void park_car(Queue *wait,Queue *park)

{

int num;

int t;

printf(“请输入车牌和时间n”);

scanf(“%d%d”,&num,&t);

printf(“=======nihaon”);

if(Length(*park)>SIZE )

{

printf(“停车场满了n”);

Push(wait,t);

}

else

{

Push(park,t);

//printf(“%d,park->num,park->t);

}

}

//进栈

int Push1(Stack *s,int t)

{

if(NULL==s)

return FLAST;

StackNode *newnode1=(StackNode *)malloc(sizeof(StackNode) );

if(NULL==newnode1)

return FLAST;

newnode1->data.number=num;

newnode1->data.time=t;

newnode1->next=s->top;

s->top=newnode1;

s->sum++;

return TRUE;

}

//出栈

int Pop1(Stack *s,int *t)

{

if(NULL==s)

return FLAST;

int num2=*num;

int t2=*t;

if(s->sum=0)

{

printf(“栈是空的n”);

return FLAST;

}

num2=s->top->data.number;

t2=s->top->data.time;

StackNode *temp=s->top;

s->top=temp->next;

free(temp);

s->sum–;

*num=num2;

*t=t2;

return TRUE;

}

//离开

void leave_car(Queue *wait,int t)

{

printf(“请输入你要离开的车牌n”);

int j=0;

int i;

scanf(“%d”,&i);

QueueNode *head=park->front;

printf(“—-dddddn”);

while(head!=park->rear)

{

if(head->infom.number!=i)

{

head=head->next;

j++;

}

else

{

break;

}

printf(“—n”);

}

printf(“—n”);

int a=0;

if(j<SIZE)

{

while(j!=a)

{

printf(“——dsdsadn”);

Pop(park,&t);

printf(“====dsdsadn”);

Push1(turn,t);

a++;

//printf(“%d,%d”,t);

}

Pop(park,&t);

}

else

{

printf(“没有要离开的车n”);

}

while(turn->top!=NULL)

{

Pop1(turn,&t);

Push(park,t);

}

if(Length(*wait)!=0)

{

Pop(wait,t);

}

}

//显示停车场状况

void examine_car(Queue wait,Queue park)

{

printf(“查看当前停车场状况n”);

printf(“——kkn”);

printf(“停车场共有%d车位,当前停车共有%d辆,等待区共有%d辆n”,SIZE,Length(park),Length(wait) );

printf(“************************************************n”);

printf(“t车号tn”);

QueueNode *head1=park.front;

QueueNode *head2=park.front;

while(head1!=NULL)

{

printf(“%dt”,head1->infom.number);

head1=head1->next;

}

//printf(“n”);

printf(“t停车时间tn”);

while(head2!=NULL)

{

printf(“t%d”,head2->infom.time);

head2=head2->next;

}

printf(“n”);

}

main.c文件

#include

#include “Queue.h”

int main()

{

Queue park;

Queue wait;

Stack turn;

int num=0;

int t=0;

Init(&wait,&park,&turn);

Menu(&wait,&turn,t);

return 0;

}