栈和队列实现停车场两种方案(1)

 停车场管理

问题描述:停车场是一个能放n辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退出,待它走后在依次进入。汽车离开时按停放时间收费。

基本功能要求:

(1) 建立三个数据结构分别是:停放队列、让路栈、等候队列。

(2) 输入数据模拟管理过程,数据(入或出,车号)。

初始停放8辆车,最多能停放10辆车,后面的车如果需要停放,需要等待

初始查看

《栈和队列实现停车场两种方案(1)》

park停车

《栈和队列实现停车场两种方案(1)》

3.再次查看

《栈和队列实现停车场两种方案(1)》

4.多次停放后需要等待

《栈和队列实现停车场两种方案(1)》

5.离开leave

《栈和队列实现停车场两种方案(1)》

规范写法

/****************************************************************************************************
Function:     interface
Description:  打印界面
Called By:    main 函数
Input:        无输入
Output:      输出界面
Return:       无返回
******************************************************************************************************/
#include <stdio.h>

void interface()
{
    printf ("                ***********************************************************\n");
    printf ("                *                                                         *\n");
    printf ("                *                    Welcom to our park                   *\n");
    printf ("                *                                                         *\n");
    printf ("                *                   park    :      停   车                *\n");
    printf ("                *                                                         *\n");
    printf ("                *                   leave   :      离   开                *\n");
    printf ("                *                                                         *\n");
    printf ("                *                   display :      查   看                *\n");
    printf ("                *                                                         *\n");
    printf ("                *                   exit    :      退   出                *\n");
    printf ("                *                                                         *\n");
    printf ("                ***********************************************************\n");
    printf ("\n                请输入相应指令操作 :");
}

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

/****************************************************************************************************
Function:     leave
Description:  车离开停车场操作
Called By:    main 函数
Input:        指针parking,wait
Output:      输出停车场信息
Return:       无返回值
******************************************************************************************************/
void display(stackpark *parking,quewait *wait)
{
     time_t now;
     time(&now);                               //记录现在时间

     struct node *p = parking->toppark;        //临时指针 遍历链表

     printf ("\n                停车场目前状况 :\n");

     printf ("\n                车位                停车号             已停时间\n");

     while(p != NULL)                         //打印车位号
     {
         printf("                 %-20d%-20d%d秒\n",p->car_num,p->car_id,now - p->t);
         p = p->next;
     }

     if (emptywait(wait) == EMPTY)
     {
         printf ("\n\n                等候区无车辆\n");
     }
     else
     {
         printf ("\n\n                等候区车辆数 : %d\n",wait->rear->car_id - wait->front->car_id + 1);
     }
}

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

/****************************************************************************************************
Function:     emptypark
Description:  判断停车栈是否空栈
Called By:    park函数  leave函数
Input:        parking
Output:      无输出
Return:       空栈返回 EMPTY 否则 返回 NO_EMPTY
******************************************************************************************************/
int emptypark(stackpark *s)     //判断停车栈是否空栈
{
    if (s->toppark == NULL)
    {
        return EMPTY;
    }
    else
    {
        return NO_EMPTY;
    }
}

/****************************************************************************************************
Function:     emptytemp
Description:  判断让车栈是否为空
Called By:    main 函数
Input:        temp
Output:      无输出
Return:       空栈返回 EMPTY 否则 返回 NO_EMPTY
******************************************************************************************************/
int emptytemp(stacktemp *s)              //判断让车栈是否为空
{
    if (s->toptemp == NULL)
    {
        return EMPTY;
    }
    else
    {
        return NO_EMPTY;
    }
}

/****************************************************************************************************
Function:     mptywait
Description:  判断等候队列是否为空
Called By:    park函数  leave函数
Input:        wait
Output:      无输出
Return:       空返回 EMPTY 否则 返回 NO_EMPTY
******************************************************************************************************/
int emptywait(quewait *s)                //判断等候队列是否为空
{
    if (s->front == NULL)
    {
        return EMPTY;
    }
    else
    {
        return NO_EMPTY;
    }
}

#include <time.h>
/*变量声明*/
struct node                    //定义结点 包含停车数 车辆id 时间
{
    int car_num;
    int car_id;
    time_t t;
    struct node *next;
};

enum link{MALLOC_NO,EMPTY,NO_EMPTY,NO_CAR_PARK,NO_CAR_TEMP,NO_CAR_WAIT};

typedef struct                  //定义停车栈栈顶
{
    struct node *toppark;
}stackpark;

typedef struct                  //定义让车栈栈顶
{
    struct node *toptemp;
}stacktemp;

typedef struct                  //定义等候队列
{
    struct node *front;
    struct node *rear;
}quewait;

stackpark *parking;             //停车栈
stacktemp *temp;                //让车栈
quewait *wait;                  //候车栈



/*用到的函数*/
void initpark();
void inittemp(); 
void initwait();
int emptypark(stackpark *s);
int emptytemp(stacktemp *s);
int emptywait(quewait *s);
void pushpark(stackpark *s, int num, time_t t, int id);
void pushtemp(stacktemp *s, int num, time_t t,int id);
void pushwait(quewait *wait,int car_id);
int poppark(stackpark *parking);
int poptemp(stacktemp *temp);
int popwait(quewait *wait);
void park(stackpark *parking,quewait *wait);
int leave(stackpark *parking,stacktemp *temp,quewait *wait);
void display(stackpark *parking,quewait *wait);
void display(stackpark *parking,quewait *wait);

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

/****************************************************************************************************
Function:     initpark() 
Description:  停车栈初始化
Called By:    main 函数
Input:        无输入
Output:      无输出
Return:       无返回
******************************************************************************************************/
void initpark()                 //停车栈初始化
{
    if ((parking = (stackpark *)malloc(sizeof(stackpark))) == NULL)
    {
        printf ("malloc error!\n");
	    exit(MALLOC_NO);
    }
    parking->toppark = NULL;
}

/****************************************************************************************************
Function:     initpark() 
Description:  让车栈初始化
Called By:    main 函数
Input:        无输入
Output:      无输出
Return:       无返回
******************************************************************************************************/
void inittemp()                 //让车栈初始化
{
    if ((temp = (stacktemp *)malloc(sizeof(stacktemp))) == NULL)
    {
        printf ("malloc error!\n");
	    exit(MALLOC_NO);
    }
    temp->toptemp = NULL;
}

/****************************************************************************************************
Function:     initpark() 
Description:  等候队列初始化
Called By:    main 函数
Input:        无输入
Output:      无输出
Return:       无返回
******************************************************************************************************/
void initwait()                 //等候队列初始化
{
    if ((wait = (quewait *)malloc(sizeof(quewait))) == NULL)
    {
        printf ("malloc error!\n");
	    exit(MALLOC_NO);
    }

    wait->rear = wait->front = NULL;
}

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

/****************************************************************************************************
Function:     leave
Description:  车离开停车场操作
Calls    :    emptypark,poppark,pushtemp,pushpark,poptemp,emptywait,
              popwait,
Called By:    main 函数
Input:        指针parking,temp,wait
Output:      输出相应提示信息

Return:       无返回值
******************************************************************************************************/
int leave(stackpark *parking,stacktemp *temp,quewait *wait)
{
    int i;
    int num,id;
    time_t t;
    time_t pass;                      //保存要出停车栈的车的停入时间

    if (emptypark(parking) == EMPTY)  //判断停车栈是否为 空
    {
         printf("\n                站内无车\n");
         return NO_CAR_PARK;
    }

    printf("\n                请输入您的停车号 : ");
    scanf("%d",&i);

    if (parking->toppark->car_id == i)        //判断是否是栈顶的车要走
    {
        pass = parking->toppark->t;           //将车停入时间保存
        poppark(parking);                     //直接出停车栈
    }
    else                                      //不是栈顶的车要走 将其前面的车入让车栈
    {
        while(parking->toppark != NULL && parking->toppark->car_id != i)  //将要走前面的车入让车栈
        {
            num = parking->toppark->car_num;      //保存车辆信息
            id = parking ->toppark->car_id;
            t = parking->toppark->t;
            poppark(parking);                     //让车出停车栈
            pushtemp(temp,num,t,id);              //让车入让车栈
        }

        if (parking->toppark == NULL)                //没有要找的车 让出去的车先回来
        {
            while (temp->toptemp != NULL)            //让出去的车回来
            {
                num = temp->toptemp->car_num;        //保留原来的停车位
                id = temp->toptemp->car_id;          //让其保留原来 id
                t = temp->toptemp->t;
                poptemp(temp);                       //出让车栈
                pushpark(parking,num,t,id);          //让车回停车栈
            }
            printf("\n                无该车辆 操作无效\n");
            return NO_CAR_PARK;
        }

        pass = parking->toppark->t;               //将车停入时间保存
        poppark(parking);                         //让车出停车栈

        while (temp->toptemp != NULL)             //让让车栈的车返回停车栈
        {
             num = temp->toptemp->car_num - 1;    //因为有车出去 所以车位数要减 1
             id = temp->toptemp->car_id;          //让其保留原来 id
             t = temp->toptemp->t;
             poptemp(temp);                       //出让车栈
             pushpark(parking,num,t,id);          //让车回停车栈
        }
    }

    struct tm *local,*local2;

    time_t now;
    time(&now);

    local2 = localtime(&pass);
    printf("\n                您的车已取出\n");
    printf("\n                停入时间为  :  %d : %d : %d\n",local2->tm_hour,local2->tm_min,local2->tm_sec);

    local = localtime(&now);
    printf("\n                离开时间为  :  %d : %d : %d\n",local->tm_hour,local->tm_min,local->tm_sec);
    printf("\n                总停车时间为:  %d秒\n",now - pass);
    printf("\n                谢谢使用 祝出行愉快\n");

    if (emptywait(wait) == EMPTY)
    {
        return NO_CAR_WAIT;
    }
    
    num = parking->toppark->car_num + 1;        //候车队列有车要进停车栈 车位数加 1
    id = parking->toppark->car_id + 1;          //停车号加 1;

    int w_id;                                   //保存候车站车的 id
    w_id = wait->front->car_id;

    popwait(wait);                              //候车站第一辆车出站 准备进入停车栈
    pushpark(parking,num,now,id);               //候车站车入停车栈

    printf("\n                等候号为 %d 的车辆已进入停车场\n",w_id);
    printf("\n                您的停车号为 %d,停入时间为 :   %d:%d:%d\n",id,local->tm_hour,local->tm_min,local->tm_sec);
    
          return 0;
}

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

int main()
{
    initpark();                       //停车栈初始化
    inittemp();                       //让车栈初始化
    initwait();                       //等候队列初始化

    time_t now;                       //记录时间
    time(&now);

    int i;
    char str[100];

    for (i = 1; i <= 8; i++)
    {
        pushpark(parking,i,now,1000+i);      //先停入8辆车
    }

    while(1)
    {
        interface();         //界面初始化 

        gets(str);           //输入相应指令

	if (strcmp(str,"park") == 0)
	{
	    printf ("\n                操作成功,开始停车\n");
            park(parking,wait);

	    while(strcmp(str,"back") != 0)
	    {
	        printf ("\n                输入 back 返回操作界面 : ");
	        gets(str);
	    }
	    system("clear");
        }

	if (strcmp(str,"leave") == 0)
	{
	    printf ("\n                操作成功\n");
            leave(parking,temp,wait);

	    while(strcmp(str,"back") != 0)
	    {
	        printf ("\n                输入 back 返回操作界面 : ");
	        gets(str);
	    }
	    system("clear");
        }

	if (strcmp(str,"display") == 0)
	{
	    printf ("\n                操作成功\n");
            display(parking,wait);

	    while(strcmp(str,"back") != 0)
	    {
	        printf ("\n                输入 back 返回操作界面 : ");
	        gets(str);
	    }
	    system("clear");
        }

        if (strcmp(str,"exit") == 0)
        {
            exit(0);
        }
	else
	{
	    system("clear");
	}
    }

    return 0;
}

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


/****************************************************************************************************
Function:     park
Description:  停车操作
Calls    :    emptywait,pushwait,emptypark,pushpark
Called By:    main 函数
Input:        指针parking,wait
Output:      输出相应提示信息

Return:       无返回值
******************************************************************************************************/
void park(stackpark *parking,quewait *wait)
{
    struct tm *local;
    time_t now;
    time(&now);
    local = localtime(&now);

    if (emptypark(parking) == EMPTY)          //停车栈为空
    {
         pushpark(parking,1,now,1001);

         printf("\n                停车成功\n");
         printf("\n                您的停车号为  :  %d\n",parking->toppark->car_id);
         printf("\n                您的停入时间为:  %d : %d : %d\n",local->tm_hour,local->tm_min,local->tm_sec);
    }

    else if (parking->toppark->car_num == 10)             //停车栈已满
    {
        printf("\n                车位已满,请进进候车栈等待\n");
        if (emptywait(wait) == EMPTY)              //如果等候队列为空
        {
            pushwait(wait,1);                      //直接入等候队列 等候号为1
        }
        else
        {
            int a;
            a = wait->rear->car_id + 1;           //不为空 等候号为前一辆车的Id + 1
            pushwait(wait,a);
        }
        printf("\n                您的等候号为 : %d\n",wait->rear->car_id);
        printf("\n                您前面还有 %d 辆车等候\n",wait->rear->car_id - wait->front->car_id);
    }
    else
    {
        int b,c;
        b = parking->toppark->car_num + 1;
        c = parking->toppark->car_id + 1;

        pushpark(parking,b,now,c);

        printf("\n                停车成功\n");
        printf("\n                您的停车号为  :  %d\n",parking->toppark->car_id);
        printf("\n                您的停入时间为:  %d : %d : %d\n",local->tm_hour,local->tm_min,local->tm_sec);
    }
}

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

/****************************************************************************************************
Function:     poppark
Description:  停车栈出栈操作
Called By:    leave函数
Input:        指针parking
Output:      无输出
Return:       停车栈空时 返回 NO_CAR_PARK
******************************************************************************************************/
int poppark(stackpark *parking)
{
    struct node *p = parking->toppark;    //保存栈顶指针

    if (emptypark(parking) == EMPTY)
    {
        printf("停车场已无车辆\n");
        return NO_CAR_PARK;
    }
    else
    {
        parking->toppark = parking->toppark->next;
        free(p);                        //释放栈顶空间
        p = NULL;
    }
}

/****************************************************************************************************
Function:     poptemp
Description:  让车栈出栈操作
Called By:    leave函数
Input:        指针temp
Output:      无输出
Return:       让车栈空时返回 NO_CAR_TEMP
******************************************************************************************************/
int poptemp(stacktemp *temp)
{
    struct node *p = temp->toptemp;     //保存栈顶指针

    if (emptytemp(temp) == EMPTY)
    {
        printf("无待入车辆\n");
        return NO_CAR_TEMP;
    }
    else
    {
        temp->toptemp = temp->toptemp->next;
        free(p);                        //释放栈顶空间
        p = NULL;
    }
}

/****************************************************************************************************
Function:     pushwait
Description:  候车队列出队操作
Called By:    leave函数
Input:        指针wait 
Output:      无输出
Return:       候车队列为空 时候 返回 NO_CAR_WAIT
******************************************************************************************************/
int popwait(quewait *wait)
{
    struct node *p = wait->front;         //保存队头指针
    if (emptywait(wait) == EMPTY)
    {
        printf("无等候车辆\n");
        return NO_CAR_WAIT;
    }

    wait->front = wait->front->next;
    free(p);                      //释放队头空间
    p = NULL;
}

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

/****************************************************************************************************
Function:     pushpark
Description:  停车栈入栈操作
Called By:    park函数    leave函数
Input:        指针parking,车位数num,时间t,停车号id 
Output:      无输出
Return:       无返回值
******************************************************************************************************/
void pushpark(stackpark *s, int num, time_t t, int id)        //车辆入停车栈
{
    struct node *p = (struct node *)malloc(sizeof(struct node)); //创建结点

    p->car_num = num;                //记录有多少辆车
    p->t = t;                        //记录停车时间
    p->car_id = id;                  //记录车号

    p->next = s->toppark;            //表头插入
    s->toppark = p;
}

/****************************************************************************************************
Function:     pushtemp
Description:  让车栈入栈操作
Called By:    main函数   leave函数
Input:        指针temp,车位数num,时间t,停车号id 
Output:      无输出
Return:       无返回值
******************************************************************************************************/
void pushtemp(stacktemp *s, int num, time_t t,int id)         //让车入让车栈
{
    struct node *p = (struct node *)malloc(sizeof(struct node));
    p->car_num = num;
    p->t = t;
    p->car_id = id;
    p->next = s->toptemp;
    s->toptemp = p;
}

/****************************************************************************************************
Function:     pushwait
Description:  候车队列入队操作
Called By:    park 函数
Input:        指针wait,候车号id 
Output:      无输出
Return:       无返回值
******************************************************************************************************/
void pushwait(quewait *wait,int car_id)
{
    struct node *p = (struct node *)malloc(sizeof(struct node));

    p->car_id = car_id;
    p->next = NULL;

    if (emptywait(wait) == EMPTY)
    {
        wait->front = wait->rear = p;
    }
    else
    {
        wait->rear->next = p;
        wait->rear = p;
    }
}

工程管理文件 Mkaefile

CC:=gcc

CFLAGS:= -Wall -o3 -I ../include

Target :=park1

Source :=$(wildcard src/*.c)

Objs   :=$(patsubst %.c,%.o,$(Source))

Modules +=display empty init interface leave main park pop push

AllObjs:=$(addsuffix /src/*.o,$(Modules))

include script/makefile

modules_make = $(MAKE) -C $(1);

modules_clean = $(MAKE) -C $(1);

.PHONY: all mm mc clean

all: $(Target)

mm:

@$(foreach n,$(Modules),$(call modules_make,$(n)))

mc:

@$(foreach n,$(Modules),$(call modules_clean,$(n)))

$(Target) : mm

$(CC) $(CFLAGS) -o $(Target) $(AllObjs)

@ echo $(Target) make done!

clean : mc

rm -rf $(Target)

@ echo done!

需要生成的目标文件(target file)

生成目标文件所需要依赖的文件(dependency file)

生成目标文件的编译规则命令(command)

这三项按照规则

target file:dependency file

command

其中,Makefile规定在书写command命令行前必须加一个<tab>按键

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