c语言模拟停车场

#include <stdio.h>
#include <stdlib.h>

#define F 0
#define T 1
#define MAX 3

typedef int ElementType;
typedef int Status;

typedef struct node          //数据
{
	ElementType number;
	ElementType time;
}Node;

typedef struct queuenode     //队列的链表结构
{
	 Node inform;
     struct queuenode * next;
}*QueueNode;

typedef struct linkqueue     //链队列
{ 
	QueueNode front;
	QueueNode rear;
}LinkQueue;

typedef struct stacknode     //栈的链表结构
{
	Node data;
	struct stacknode *next;
}*StackNode;

typedef struct linkstack     //链栈
{
	StackNode top;
	ElementType count;
}LinkStack;

void menu();
void option(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,ElementType num,ElementType t);
Status init(LinkQueue *wait,LinkQueue *park,LinkStack *giveway);
Status linklength(LinkQueue q);
Status enqueue(LinkQueue *q,ElementType num,ElementType t);
Status dequeue(LinkQueue *q,ElementType *num,ElementType *t);
Status push(LinkStack *s,ElementType num,ElementType t);
Status pop(LinkStack *s,ElementType *num,ElementType *t);
void park1(LinkQueue *wait,LinkQueue *park);
void leave2(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,ElementType num,ElementType t);
void view3(LinkQueue wait,LinkQueue park);

int main()
{
	LinkQueue wait;
	LinkQueue park;
	LinkStack giveway;
	int num = 0;
	int t = 0;
	init(&wait,&park,&giveway);     
	menu();
	option(&wait,&park,&giveway,num,t);
    return 0;
}

Status init(LinkQueue *wait,LinkQueue *park,LinkStack *giveway)
{
   QueueNode newnode1 = (QueueNode)malloc(sizeof(struct queuenode));
   if(NULL == newnode1)
   {
   	return F;
   }
   newnode1->next = NULL;
   wait->front = newnode1;
   wait->rear = newnode1;

   
   QueueNode newnode2 = (QueueNode)malloc(sizeof(struct queuenode));
   if(NULL == newnode2)
   {
   	return F;
   }
   newnode2->next = NULL;
   park->front = newnode2;
   park->rear = newnode2;

   
   giveway->top = NULL;
   giveway->count = 0;
}  

void menu()
{
	printf("\n");
	printf("\t**********Welcome to our Car Parking !**********\n");
	printf("\t**********  Please choose function    **********\n");
	printf("\t**********         1 : park.          **********\n");
	printf("\t**********         2 : leave.         **********\n");
	printf("\t**********         3 : view.          **********\n");
	printf("\t**********         4 : exit.          **********\n");
    printf("\n");
}

void option(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,ElementType num,ElementType t)
{
	int option1;
	scanf("%d",&option1);
	switch(option1)
	{
		case 1:
		{
			park1(wait,park);
			printf("停车完成!\n");
			menu();
	        option(wait,park,giveway,num,t);
			break;
		}
		case 2:
		{
			leave2(wait,park,giveway,num,t);
			printf("成功离开!\n");
			menu();
	        option(wait,park,giveway,num,t);
			break;
		}
		case 3:
		{
			view3(*wait,*park);
			menu();
	        option(wait,park,giveway,num,t);
			break;
		}
		case 4:
		{
			printf("\t**********     欢迎再次使用,谢谢!   **********\n");
			break;
		}
		default:
        {
			printf("\t**********      请输入正确的指令!    **********\n");
			menu();
	        option(wait,park,giveway,num,t);
			break;
		}
	}
}

Status linklength(LinkQueue q)
{
    int len = 0;
	while(q.front != q.rear)
	{
		len++;
		q.front = q.front->next;
	}
	return len;
}

Status enqueue(LinkQueue *q,ElementType num,ElementType t)
{
    QueueNode newnode = (QueueNode)malloc(sizeof(struct queuenode));
    if(NULL == newnode)
    {
    	 return F;
    }

    newnode->inform.number = num;
    newnode->inform.time = t;
    q->rear->next = newnode;
    q->rear = newnode;
    return T;
}

Status dequeue(LinkQueue *q,ElementType *num,ElementType *t)
{
    if(q->front == q->rear)
	{
		printf("the queue is empty!\n");
		return F;
	}
	*num = q->front->next->inform.number;
	*t = q->front->next->inform.time;
	QueueNode temp = q->front->next;
	q->front->next = temp->next;
	if(temp->next == NULL)
	{
		q->rear = q->front;
	}
	free(temp);
	return T;
}

Status push(LinkStack *s,Status num,Status t)
{
	StackNode newnode = (StackNode)malloc(sizeof(struct stacknode));
	if(NULL == newnode)
	{
		return F;
	}
	newnode->data.number = num;
	newnode->data.time = t;
	newnode->next = s->top;
	s->top = newnode;
	s->count++;
	return T;
}

Status pop(LinkStack *s,Status *num,Status *t)
{
	if(0 == s->count)
	{
		printf("the stack is empty !\n");
	    return F;
	}
	*num = s->top->data.number;
	*t = s->top->data.time;
	StackNode temp = s->top;
	s->top = s->top->next;
	free(temp);
	s->count--;
	return T;
}

void park1(LinkQueue *wait,LinkQueue *park)
{
	printf("请输入车号和停车时间\n");
	int num,t;
	scanf("%d,%d",&num,&t);
	if(linklength(*park) >= MAX)
	{
		printf("停车场已满,进入等待区!\n");
		enqueue(wait,num,t);
	}
	else
	{
		enqueue(park,num,t);
	}
}

void leave2(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,ElementType num,ElementType t)
{
	printf("请输入要离开车的车号\n");
	int leavenumber;
	scanf("%d",&leavenumber);
	int i = 0;
	QueueNode head = park->front;
	while(head != park->rear)
	{
		if(head->next->inform.number != leavenumber)
		{
			head = head->next;
			i++;
		}
		else
			break;
	}
	int j = 0;
	if(i <= MAX-1)
	{
		while(j != i)
		{
			dequeue(park,&num,&t);
			push(giveway,num,t);
			j++;
		}
		dequeue(park,&num,&t);
	}
	else
	{
		printf("\t查无此车!\n");
	}
	while(giveway->top != NULL)
	{
		pop(giveway,&num,&t);
		enqueue(park,num,t);
	}
	if(linklength(*wait) != 0)
	{
		dequeue(wait,&num,&t);
		enqueue(park,num,t);
	}
}

void view3(LinkQueue wait,LinkQueue park)
{
	printf("\t********************    目前停车场状况    ********************\n");
	printf("\t停车场共%d个车位,当前停车场共有%d量车,等待区共有%d量车\n",
		MAX,linklength(park),linklength(wait));
	printf("\t**************************************************************\n");
	//printf("车    号:");
	QueueNode head1 = park.front;
	QueueNode head2 = park.front;
	while(head1 != park.rear)
	{
		printf("\t车    号:%d  ",head1->next->inform.number);
		head1 = head1->next;
		printf("\t停车时间:%d ",head2->next->inform.time);
	 	head2 = head2->next;
	 	printf("\n");
	}
	printf("\t**************************************************************\n");
	// printf("\n");
	// printf("停车时间:");
	// while(head2 != park.rear)
	// {
	// 	printf("%d ",head2->next->inform.time);
	// 	head2 = head2->next;
	// }
	// printf("\n");
}

    原文作者:停车场模拟问题
    原文地址: https://blog.csdn.net/sinat_38642518/article/details/78280820
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞