栈和队列 实现停车场

  这几天一直在做停车场,熟悉一下学的栈和队列。

  一开始的时候,我不知道停车场停车的时间怎么表示,所以我没有加时间,只有一个车号。在快做完的时候,我突然发现时间没办法添加上去了,因为一个栈里只有一个数组,两个数组的话很难进行操作,在pop的时候只能弹出一个元素,我尝试了很多方法,最后用一个结构体数组完成了,在停车栈里面加一个结构体数组,这样就能完美的解决了两个元素的问题。时间的显示我也考虑了很久,查了资料后,我用time()函数实现了,用现在的time()减去之前的time()便得到了停车的时长。

/**********************************************************
File Name:         
Author:            xxx      Date:2016-12-15
Description:   实现停车场功能
Fuction List:
************************************************************/

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

#define  SIZE         10
#define  ok            0
#define  error        -1
#define  malloc_error -2

typedef struct car
{
	int num;
	int time;
}Car;

typedef struct park
{
	Car car[SIZE];
	int top1;
}Park;

typedef struct give_way
{
	Car car[SIZE];
	int top2;
}Give_way;

typedef struct wait_node
{
	int data;          //队列结点数据
	struct wait_node *next; //结点链指针
}Wait;
typedef Wait* Pwait;

typedef struct wait_queue
{
	Pwait front;       //队头指针
	Pwait rear;        //对尾指针
}Wait_queue;

Car EMPTY = {-3,0};

//显示函数
void show()
{
	system("clear");
	printf("\t\t*********************************************\n");
	printf("\t\t* \twelcome to our car parking          *\n");
	printf("\t\t*                                           *\n");
	printf("\t\t*\tA) 停车                             *\n");
	printf("\t\t*\tB) 离开                             *\n");
	printf("\t\t*\tC) 查看停车场停车情况               *\n");
	printf("\t\t*\tD) 退出                	            *\n");
	printf("\t\t*                                           *\n");
	printf("\t\t*********************************************\n");
	printf("                                         \n");
	printf("                                         \n");
	printf("                                         \n");
	printf("                                         \n");
	printf("         请告诉我您的选择:");
}

//初始化park栈
int Init_park(Park *p)
{
	if(p == NULL)
	{
		return error;
	}
	p->top1 = -1;
	return ok;
}

//初始化Give_way栈
int Init_Give_way(Give_way *g)
{
	if(g == NULL)
	{
		return error;
	}
	g->top2 = -1;
	return ok;
}

//初始化Wait_queue队列
int Init_Wait_queue(Wait_queue *wq)
{
	if (wq == NULL)
	{
		return error;
	}
	wq->front = NULL;
	wq->rear = NULL;
	
	return ok;
}

//判断停车场是否为空
int Park_empty(Park *p)
{
	if(p == NULL)
	{
		return error;
	}
	return p->top1 == -1;
}

//判断停车场是否满
int Park_full(Park *p)
{
	if(p == NULL)
	{
		return error;
	}
	return p->top1 == SIZE - 1;
}

//将车停入车场    入栈1
int Push_park(Park *p, int n,int time)
{
	if (p == NULL)
	{
		return error;
	}
	
	if(Park_full(p))
	{
		return error;
	}

	p->top1++;
	p->car[p->top1].num = n;
	p->car[p->top1].time = time;
	
	return ok;
}

//判断候车队列是否空队
int Queueempty(Wait_queue *q)
{
	if(q == NULL)
	{
		return error;
	}
	
	return q->front == NULL;
}

//停车场满了 停入候车区    入队
int Push_wait(Wait_queue *wq, int n)
{
	if(wq == NULL)
	{
		return error;
	}
	Pwait node = (Pwait)malloc(sizeof(Wait)/sizeof(char));
	if(node == NULL)
	{
		return malloc_error;
	}
	
	node->data = n;
	node->next = NULL;
	
	if(Queueempty(wq))
	{
		wq->front = node;
		wq->rear = node;
	}
	else
	{
		wq->rear->next = node;
		wq->rear = node;
	}
	
	return ok;
}

//候车场开出一辆车进停车场   出队
int GetPop_wait(Wait_queue *wq)
{
	if(wq == NULL)
	{
		return error;
	}
	
	if(Queueempty(wq))
	{
		return -3;
	}
	
	Pwait p = wq->front;
	int data = p->data;
	wq->front = p->next;
	
	if(wq->front == NULL)
	{
		wq->rear = NULL;
	}
	
	return data;
}

//停车场离开车  出栈1
Car Pop_park(Park *p)
{
	if (p == NULL)
	{
		return ;
	}
	if(Park_empty(p))
	{
		return EMPTY;
	}
	
	Car data;
	data = p->car[p->top1];
	p->top1--;
	
	return data;
}

//判断让车栈是否空栈
int Give_wayempty(Give_way *g)
{
	if(g == NULL)
	{
		return error;
	}
	return g->top2 == -1;
}

//判断让车栈是否满栈
int Give_wayfull(Give_way *g)
{
	if(g == NULL)
	{
		return error;
	}
	return g->top2 == SIZE - 1;
}

//离开的车进入让车栈    入栈2
int Push_giveway(Give_way *g, Car data)
{
	if(g == NULL)
	{
		return error;
	}
	if(Give_wayfull(g))
	{
		return error;
	}

	g->top2++;
	g->car[g->top2].num = data.num;
	g->car[g->top2].time = data.time;
	
	
	return ok;
}

//让车栈的车出栈     出栈2
Car Pop_giveway(Give_way *g)
{
	if(g == NULL)
	{
		return ;
	}
	if(Give_wayempty(g))
	{
		return EMPTY;
	}
	Car data = g->car[g->top2];
	g->top2--;
	
	return data;
}

//当前时间
int get_time()
{
	time_t lt1;
	lt1 = time(NULL);
	return lt1;
}

int main()
{
	Park p;
	if (Init_park(&p) != ok)
	{
		return error;
	}
	
	Give_way gw;
	if (Init_Give_way(&gw) != ok)
	{
		return error;
	}
	
	Wait_queue wq;
	if (Init_Wait_queue(&wq) != ok)
	{
		return error;
	}
	
	char option[2];
	int i[10];
	int n = 1;
	int x = 1;
	int count = 0;
	int tab = 0;
	int j;
	Car dat[SIZE] = {0};
	Car data ;
	int data1;
	
	while(x)
	{
		show();
		scanf("%s", option);
		
		switch (option[0])
		{
			case 'A' :
			{
				if (Push_park(&p,n,get_time()) != ok)
				{
					Push_wait(&wq, n);
					system("clear");
					printf("\n\n\n\n\n\n");
					printf("\t\t\t停车场已满,车号为%d的车已移至候车区处!",n);
					printf("\n\n\n\n\n\n");
					printf("按下任意键返回:");
					scanf("%s", i);
				}
				else
				{
					count++;
					system("clear");
					printf("\n\n\n\n\n\n");
					printf("\t\t\t恭喜您,车号为%d的车停车成功!", n);
					printf("\n\n\n\n\n\n");
					printf("按下任意键返回:");
					scanf("%s", i);
				}
				
				n++;
				break;
			}
			case 'B' :
			{
				system("clear");
				printf("\n\n\n\n\n\n");
				printf("您想让哪辆车离开呢?请输入车号:");
				scanf("%d", &tab);
				
				int flag = 1;
				
				for(j = 1; j <= count; j++)
				{
					data = Pop_park(&p);
					
					if (data.num != tab)
					{
						Push_giveway(&gw,data);
						continue;
					}
					else
					{
						flag = 0;
						while((data = Pop_giveway(&gw)).num != EMPTY.num)              //
						{
							Push_park(&p,data.num,data.time);
						}
						data1 = GetPop_wait(&wq) ;
						if(data1 != EMPTY.num)
						{
							Push_park(&p,data1,get_time());
							count++;
						}
						break;
					}
				}
				
				
				if(flag)
				{
					printf("\n\n\n");
					printf("\t\t\t对不起,车号为%d的车不在停车场\n",tab);
					while((data = Pop_giveway(&gw)).num != EMPTY.num)                //
					{
						Push_park(&p,data.num,data.time);
					}
				}
				else
				{
					printf("\n\n\n");
					printf("\t\t\t恭喜您成功的开走了车号为%d的这辆车\n", tab);
					count--;
				}
				
				printf("\n\n\n\n\n\n");
				printf("按下任意键返回:");
				scanf("%s", i);
				break;
		
				
			}
			case 'C' :
			{
				system("clear");
				printf("\n\n\n\n\n\n");
				printf("车号:   ");
				for(j = 0; j < count; j++)
				{
					dat[j] = Pop_park(&p);
					printf("%d \t", dat[j].num);
				}
				
				printf("\n时间:   ");
				for(j = 0; j < count; j++)
				{
					printf("%d \t",get_time() - dat[j].time);
				}
				
				for(j = count - 1; j >= 0; j--)
				{
					Push_park(&p,dat[j].num,dat[j].time);
				}
				
				printf("\n\n\n\n\n\n");
				printf("按下任意键返回:");
				scanf("%s", i);
				break;
			}
			case 'D' :
			{
				x = 0;
				system("clear");
				printf("\n\n\n\n\n\n");
				printf("\t\t\tHope to you next time!");
				system("clear");
				printf("\n\n\n\n");
				printf("\t\t\tBYE BYE!!!\n");
				sleep(3);
				system("clear");
				
				break;
			}
			default :
			{
				printf("\n\n");
				system("clear");
				printf("\n\n\n\n\n\n");
				printf("\t\t请输入A、B、C或D!\n");
				printf("\n\n");
				printf("按下任意键返回:");
				scanf("%s", i);
				
				break;
			}
		}
	}
	
	return 0;
}
    原文作者:停车场模拟问题
    原文地址: https://blog.csdn.net/hjf161105/article/details/53675932
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞