停车场(1)

#include "L.h"
#include <stdlib.h>
#include <time.h>

Queue* Create_Queue()//创建一个队列
{
	Queue * q = (Queue*)malloc(sizeof(Queue)/sizeof(char));
	if (q == NULL)
	{
		return NULL;
	}
	
	// 置空队
	q->front = NULL;
	q->rear  = NULL;
	
	return q;
}

int QueueEmpty (Queue *q)//置空队列
{
	if (q == NULL)
	{
		return FALSE;
	}
	
	
	return q->front == NULL;
}

int EnQueue (Queue *q, QueueData x)//进队
{
	if (q == NULL)
	{
		return FALSE;
	}
		Node * node = (Node*)malloc(sizeof(Node)/sizeof(char));
		if (node == NULL)
		{
			return FALSE;
		}
		node->data.id = x.id;
		node->data.time_first = x.time_first;
		node->next = NULL;
		
		if (q->front == NULL)
		{
			q->front = node;
			q->rear  = node;
		}
		else
		{
			q->rear->next = node;
			q->rear = node;
		}
		return TRUE;
	
	
	
}

int re_DeQueue (Queue *q, QueueData *x)//倒序出队
{
	if (q == NULL)
	{
		return FALSE;
	}
	
	if (QueueEmpty(q))
	{
		return FALSE;
	}
	(*x).id = q->rear->data.id;
	(*x).time_first = q->rear->data.time_first;
	
	if(q->rear == q->front)
	{
		q->rear = NULL;
		q->front = NULL;
		return TRUE;
	}
	
	Node *p = q->front;
	while(p->next != q->rear)
	{
		p = p->next;
	}

	
	q->rear = p;
	free(p->next);
	p = NULL;
	free(p);
	
	if (q->rear == NULL)
		q->front = NULL;
	
	return TRUE;
}

int DeQueue (Queue *q, QueueData *x)//出队
{
	if (q == NULL)
	{
		return FALSE;
	}
	
	if (QueueEmpty(q))
	{
		return FALSE;
	}
		
	Node *p = q->front;
	(*x).id = p->data.id;
	(*x).time_first = p->data.time_first;
	
	q->front = p->next;
	free(p);
	
	if (q->front == NULL)
		q->rear = NULL;
		
	return TRUE;
}

int Destroy_Queue (Queue *q)//销毁队列
{
	if (q == NULL)
	{
		return FALSE;
	}
	
	QueueData x;
	while (QueueEmpty(q) != TRUE)
	{
		DeQueue(q, &x);
	}
	
	free(q);
	
	return TRUE;
}

int Time_Stop(QueueData x)//获得停放时间
{
	time_t time_now;
	time_now = time(NULL);
	
	return (int)difftime( time_now, x.time_first);
}

LinkStack *Create_Stack()//创建一个栈
{
	LinkStack* s = (LinkStack*)malloc(sizeof(LinkStack)/sizeof(char));
	if (s == NULL)
	{
		return NULL;
	}
	
	// 置空栈
	s->top = NULL;
	
	return s;
}

int StackEmpty (LinkStack *s)//判断栈是否为空
{
	if (s == NULL)
	{
		return FALSE;
	}
	
	return s->top == NULL;
}

int Push (LinkStack *s, QueueData x)//进栈
{
	if (s == NULL)
	{
		return FALSE;
	}
	
	// 新建结点
	NodeS* node = (NodeS*)malloc(sizeof(NodeS)/sizeof(char));
	if (node == NULL)
	{
		return FALSE;
	}
	
	node->data = x;
	node->next = s->top;
	s->top = node;
	
	return TRUE;
}

int Pop (LinkStack *s, QueueData *x)//出栈
{
	if (s == NULL)
	{
		return FALSE;
	}
	
	if (StackEmpty(s))
	{
		return FALSE;
	}
	
	NodeS *p = s->top;
	(*x).id = p->data.id;
	(*x).time_first = (p->data).time_first;
	
	s->top = p->next;
	free(p);	
	
	return TRUE;
}

int Destroy(LinkStack *s)//销毁栈
{
	if (s == NULL)
	{
		return FALSE;
	}
	
	QueueData x;
	while(StackEmpty(s) != TRUE)
	{
		Pop (s, &x);
	}
	
	free(s);
	
	return TRUE;
}

int find_id(Queue* q, int id)//寻找是否有编号为id的车辆
{
	Node * node = (Node*)malloc(sizeof(Node)/sizeof(char));
	if (node == NULL)
	{
		return FALSE;
	}
	int b = 0;
	node = q->front;
	while(node)
	{
		if(node->data.id == id)
		{
			b = 1;
			break;
		}
		node = node->next;
	}
	node = NULL;
	free(node);
	return b == 1;
}

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