C语言停车场管理系统,使用栈和队列实现

使用栈和队列实现的狭长停车场管理

1、情况说明: 
(1)停车场结构为一条狭长的通道(可视为栈)。 
(2)若停车场内车辆已经停满,后来的车需要在路边排队等待,库内有车出来才能入库    (可视为队列)。 
(3)使用A代表入库,D代表出库,车辆信息包括入库时间和车牌。 

2、数据定义

#define M 3//车库能停几辆车,在这里定义
//-----------------///--------------------///--------------------//
typedef struct {
    int num;
    int ID;
    int arrivetime;
}carinformation;
//定义停车场的结构体
typedef struct parkinglot {
    carinformation stack[M];
    int top;
}carpark;//定义栈结构
//-----------------///--------------------///--------------------//
//--------------------///--------------------///--------------------//
typedef struct carnode {
    int num;
    int ID;
    int arrivetime;
    //struct node *next;
}qptr;
//定义外面狭长路的队列
typedef struct {
    qptr *front, *rear;
}carqueue;
//--------------------///--------------------///--------------------//
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

3、初始化栈和队列函数

carpark *initstack()
{
    carpark *T;
    T = (carpark *)malloc(sizeof(carpark));
    if (T == NULL)
    {
        /*地址分配错误直接退出程序,不能继续了*/
        system("cls");
        printf("\n\n\t因为内存空间问题,导致程序出错!即将退出!\n\n");
        system("pause");
        exit(0);
    }
    T->top = -1;
    return T;
}
//初始化停车场
carqueue *initqueue()
{
    carqueue *L;
    L = (carqueue *)malloc(sizeof(carqueue));
    L->front = (carqueue *)malloc(sizeof(carqueue));
    if (L == NULL||L->front == NULL)
    {
        /*地址分配错误直接退出程序,不能继续了*/
        system("cls");
        printf("\n\n\t因为内存空间问题,导致程序出错!即将退出!\n\n");
        system("pause");
        exit(0);
    }
    if (L->front == NULL);
    L->front->next = NULL;
    L->rear = L->front;
    return L;
}
//初始化路边停车位
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

4、算法实现 
先上代码后解释:

/*
下面是整个程序的核心内容
主要实现了进入车库和路边等待的调用
两个表的地址需要传过来
车库表和路边队列表;
carinter(char _array, int num, int time,carpark *P,carqueue *L)
carinter(进库还是出库char A/D ,车牌号int , 到达时间int ,carpark *P 车库表 ,carqueue *L 路边表)
*/
int carinter(char _array, int num, int time,carpark *P,carqueue *L)
{
    if (_array == 'A')
    {
        if (parkfull(P) == 1)
        {
            int temp;
            //这里写车库满后进入队列的code
            printf("\t车牌为:%d的车辆,已进入路边等待!\n\n",num);
            temp = carroadsaid(time, num, L);
            if (temp == 0)
            {
                /*地址分配错误直接退出程序,不能继续了*/
                system("cls");
                printf("\n\n\t因为内存空间问题,导致程序出错!即将退出!\n\n");
                system("pause");
                exit(0);
            }
            return 1;
        }
        else
        {
            int _top;
            printf("\t车牌为:%d的车辆,已进入车库!\n\n",num);
            _top = ++P->top;
            P->stack[_top].arrivetime = time;
            P->stack[_top].ID = num;
            P->stack[_top].num = internum++;
            return 1;
        }
    }
    else if (_array == 'D')
    {
        //这里是出库code
        outparking(num, time, P, L);
        return 2;//返回2,不清楚是否成功,只是返回一个数,错误在函数内判断
    }
    else//读取的char是个非法值,所以返回错误
    {
        return 0;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

解释: 
(1)函数调用方法为: 
carinter(char _array, int num, int time,carpark *P,carqueue *L) 
第一个参数char _array为车辆是进入还是出去,第二个参数num是车牌号,第三个time是到达/离去时间(假设为int),后面两个参数为栈表和队列表的地址。 
(2)首先判断_array是 A 入库还是 D 出库。如果为入库,就再接一个IF判断函数用于判出库是否已满(栈是否满),车满判断函数如下:

int parkfull(carpark *P)
{
    if (P->top == M - 1)
        return 1;
    else
        return 0;
}
//车满判断
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

如果车位没有满,就执行上面入库核心代码else里面的函数直接执行入栈操作。ps:车满是if内的,未满写在了else里面。 
(3)对于库满的操作: 
调用入队操作函数,carroadsaid(),参数明白撒~

/*
路边等待队列分配
carroadsaid(int time, int ID, carqueue *L)
carroadsaid(时间, 车牌, carqueue *L 队列表)
*/
int carroadsaid(int time, int ID, carqueue *L)
{
    qptr *T;
    T = (qptr *)malloc(sizeof(qptr));
    if (T == NULL) return 0;//地址分配失败,返回失败
    T->num = outsidenum++;
    T->ID = ID;
    T->arrivetime = time;
    T->next = NULL;
    L->rear->next = T;
    L->rear = T;
    return 1;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

4、出库运算:

/*
这里是出库的核心函数
具体使用如下:
通过ID(车牌)寻找库内ID(车牌),如果不位于库顶(栈顶),就转移前方车辆至临时路边等待
出库后,计算停车时间,并将外面临时等待的车重新放入车库
如果入库后仍有空位,就将外面的等待队列添加进库,直到库满(栈满)
outparking(int ID, int time, carpark *P, carqueue *L)
outparking(车牌ID, 出库时间int , carpark *P 库表, carqueue *L 队列表)
*/
int outparking(int ID, int time, carpark *P, carqueue *L)
{
    int down = M-1;
    while (down >= 0)
    {
        if (P->stack[down].ID == ID)//找到位置
        {
            if (down == P->top)//车在最外面
            {
                //这里是车在最外面的code
                printf("\t\n车牌为%d的,已出库!停车时间:%d,请按标准收费!\n\n", P->stack[down].ID, time - P->stack[down].arrivetime);
                P->stack[down].ID = P->stack[down].arrivetime = P->stack[down].num = -1;//清空位置
                --internum;//库内记数减一个
                --P->top;
                while (parkfull(P) == 0)//出库后再看有空位没有
                {
                    //有空位就把等待队列的第一个放进来,且重新输入进入时间
                    //出队操作
                    int _time;
                    qptr *temp;
                    if (L->front == L->rear || L->front->next == NULL) return 0;//队列是空的,直接返回就好
                    temp = L->front->next;
                    P->stack[++P->top].num = internum++;
                    P->stack[P->top].ID = temp->ID;
                    printf("\t\t\n\n车库有空位,请输入队列此车(车牌为:%d)当前入库时间:", temp->ID);
                    scanf("%d", &_time);
                    P->stack[P->top].arrivetime = _time;
                    printf("\n\t此车已在队列中等待:%d time,现已进入车库!\n\n", _time - temp->arrivetime);
                    /*调整队头指针位置,并释放内存空间*/
                    L->front->next = temp->next;
                    if (L->rear == temp) L->rear = temp;//对于队列只剩一个数据时的队头调整
                    outsidenum--;
                    free(temp);
                }
                //outqueue(ID, time, P, L);//库空填充函数
                return 1;
            }
            else 
            {
                //这里是车不在最外的code
                //down变量就是当前找到的位置,已经是数组了,不用减1
                carpark *temp;
                int x;
                temp = initstack();//初始化临时停车场
                P->top = down;//P表的top现在直接调整到找到元素那里
                for (x=M-1; x >down; x--)
                {
                    temp->stack[++temp->top].ID = P->stack[x].ID;
                    temp->stack[temp->top].arrivetime = P->stack[x].arrivetime;
                    temp->stack[temp->top].num = P->stack[x].num;
                }//往临时停车场停车
                printf("\t\n车牌为%d的,已出库!停车时间:%d,请按标准收费!\n\n", P->stack[down].ID, time - P->stack[down].arrivetime);
                //P->stack[down].ID = P->stack[down].arrivetime = P->stack[down].num = -1;//清空位置
                --internum;//库内记数减一个
                --P->top;//移动top指针
                while (temp->top >= 0)
                {
                    if (temp->stack[temp->top].num != -1) //判断是否已经取到原栈高度,也就是不用复制-1值过来,要不然top位置会错位
                    {
                        P->stack[++P->top].ID = temp->stack[temp->top].ID;
                        P->stack[P->top].arrivetime = temp->stack[temp->top].arrivetime;
                        P->stack[P->top].num = temp->stack[temp->top].num;
                        temp->top--;//temp表top位置改变
                        //P->top++;//P表top位置改变
                    }
                    else
                    {
                        break;//顶上是空不再复制过来
                    }
                }//重新放回车库
                for (x = P->top+1; x < M; x++)
                    P->stack[x].arrivetime = P->stack[x].ID = P->stack[x].num = -1;//向上清空车库
                /*
                这里应该还要判断车库有空位,所以放上面的代码过来
                要改成单独的函数,要不然太长了
                不具有独立性
                ===========================================================================
                int _num, _time;
                qptr *temp;
                if (L->front == L->rear || L->front->next == NULL) return 0;//队列是空的,直接返回就好
                temp = L->front->next;
                P->stack[++P->top].num = internum++;
                P->stack[P->top].ID = temp->ID;
                printf("\t\t\n\n车库有空位,请输入队列此车(车牌为:%d)当前入库时间:", temp->ID);
                scanf("%d", &_time);
                P->stack[P->top].arrivetime = _time;
                printf("\n\t此车已在队列中等待:%d time,现已进入车库!\n\n", _time - temp->arrivetime);
                //调整队头指针位置,并释放内存空间
                L->front->next = temp->next;
                if (L->rear == temp) L->rear = temp;//对于队列只剩一个数据时的队头调整
                free(temp);
                ===========================================================================
                */
                //outqueue(ID, time, P, L);//库空填充函数(已弃)
                while (parkfull(P) == 0)//出库后再看有空位没有
                {
                    //有空位就把等待队列的第一个放进来,且重新输入进入时间
                    //出队操作
                    int _time;
                    qptr *temp;
                    if (L->front == L->rear || L->front->next == NULL) return 0;//队列是空的,直接返回就好
                    temp = L->front->next;
                    P->stack[++P->top].num = internum++;
                    P->stack[P->top].ID = temp->ID;
                    printf("\t\t\n\n车库有空位,请输入队列此车(车牌为:%d)当前入库时间:", temp->ID);
                    scanf("%d", &_time);
                    P->stack[P->top].arrivetime = _time;
                    printf("\n\t此车已在队列中等待:%d time,现已进入车库!\n\n", _time - temp->arrivetime);
                    /*调整队头指针位置,并释放内存空间*/
                    L->front->next = temp->next;
                    if (L->rear == temp) L->rear = temp;//对于队列只剩一个数据时的队头调整
                    outsidenum--;
                    free(temp);
                }
                break;
            }
        }
        down--;//下个比较位置
    }
    return 2;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130

5、总览:

主函数main.c:

#include <stdio.h>
#include <stdlib.h>
#include "mainhead.h"
main()
{
    system("color 70");
    char arr;
    char arrtemp;
    int ID,timee;
    carpark *P;//停车场
    carqueue *L;//路边
    /**************初始化**************/
    L= initqueue();
    P = initstack();
    /**************初始化**************/


    /*
    欢迎信息
    */
    printf("\n\n");
    printf("\t-------- 欢迎使用停车场管理系统 ---------\n");
    printf("\t--------Welcome to parking management system!---------\n");
    printf("\n\n");
    do
    {
        printf("格式:入库/出库(A/D),车牌,入库时间/出库时间\t输入T退出,输C查看当前状态!\n请输入:");
        scanf("%c,%d,%d", &arr, &ID, &timee);
        if (arr == 'A' || arr == 'D')
        {
            carinter(arr, ID, timee, P, L);
            arrtemp = getchar();
            fflush(stdin);
        }
        else if (arr == 'T')
        {
            exit(0);
        }
        else  if (arr == 'C')
        {
            arrtemp = getchar();
            fflush(stdin);
            printf("\n\n库内有:%d,库外等待车辆为:%d\n\n", P->top + 1, check());
        }
        else
        {
            system("cls");
            printf("\n\n程序错误的读取了一个字符,导致不能运行,请重新启动!\n\n");
            system("pause");
        }

    } while (arr!='\n');
    system("cls");
    printf("\n\n程序错误的读取了一个字符,导致不能运行,请重新启动!\n\n");
    system("pause");



    /* 2017年4月1日00:02:56 bug report 有bug产生,导致错误输出 循环输出了3遍 */
    /*
    20174214:27:27
    bug
    scanf那里会不明误读,暂时没有解决方案,后序再改
    */



    /*carinter('A', 1, 5, P, L);
    //上面车库已满,入库没问题,开始试库外队列
    carinter('A', 10, 5, P, L);
    carinter('D', 9, 20, P, L);
    system("pause");*/
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78


头文件mainhead.h:

#include <stdio.h>
#include <stdlib.h>
#define M 3//车库能停10辆
static int internum = 0;//总计进入车库的车数辆
static int outsidenum = 0;//总计还在外面的车辆数
//-----------------///--------------------///--------------------//
typedef struct {
    int num;
    int ID;
    int arrivetime;
}carinformation;
//定义停车场的结构体
typedef struct parkinglot {
    carinformation stack[M];
    int top;
}carpark;
//-----------------///--------------------///--------------------//
//--------------------///--------------------///--------------------//
typedef struct carnode {
    int num;
    int ID;
    int arrivetime;
    //struct node *next;
}qptr;
//定义外面狭长路的队列
typedef struct {
    qptr *front, *rear;
}carqueue;
//--------------------///--------------------///--------------------//

carpark *initstack()
{
    carpark *T;
    T = (carpark *)malloc(sizeof(carpark));
    if (T == NULL)
    {
        /*地址分配错误直接退出程序,不能继续了*/
        system("cls");
        printf("\n\n\t因为内存空间问题,导致程序出错!即将退出!\n\n");
        system("pause");
        exit(0);
    }
    T->top = -1;
    return T;
}
//初始化停车场
carqueue *initqueue()
{
    carqueue *L;
    L = (carqueue *)malloc(sizeof(carqueue));
    L->front = (carqueue *)malloc(sizeof(carqueue));
    if (L == NULL||L->front == NULL)
    {
        /*地址分配错误直接退出程序,不能继续了*/
        system("cls");
        printf("\n\n\t因为内存空间问题,导致程序出错!即将退出!\n\n");
        system("pause");
        exit(0);
    }
    if (L->front == NULL);
    L->front->next = NULL;
    L->rear = L->front;
    return L;
}
//初始化路边停车位
int parkfull(carpark *P)
{
    if (P->top == M - 1)
        return 1;
    else
        return 0;
}
//车满判断


/* 下面是整个程序的核心内容 主要实现了进入车库和路边等待的调用 两个表的地址需要传过来 车库表和路边队列表; carinter(char _array, int num, int time,carpark *P,carqueue *L) carinter(进库还是出库char A/D ,车牌号int , 到达时间int ,carpark *P 车库表 ,carqueue *L 路边表) */
int carinter(char _array, int num, int time,carpark *P,carqueue *L)
{
    if (_array == 'A')
    {
        if (parkfull(P) == 1)
        {
            int temp;
            //这里写车库满后进入队列的code
            printf("\t车牌为:%d的车辆,已进入路边等待!\n\n",num);
            temp = carroadsaid(time, num, L);
            if (temp == 0)
            {
                /*地址分配错误直接退出程序,不能继续了*/
                system("cls");
                printf("\n\n\t因为内存空间问题,导致程序出错!即将退出!\n\n");
                system("pause");
                exit(0);
            }
            return 1;
        }
        else
        {
            int _top;
            printf("\t车牌为:%d的车辆,已进入车库!\n\n",num);
            _top = ++P->top;
            P->stack[_top].arrivetime = time;
            P->stack[_top].ID = num;
            P->stack[_top].num = internum++;
            return 1;
        }
    }
    else if (_array == 'D')
    {
        //这里是出库code
        outparking(num, time, P, L);
        return 2;//返回2,不清楚是否成功,只是返回一个数,错误在函数内判断
    }
    else//读取的char是个非法值,所以返回错误
    {
        return 0;
    }
}


/* 路边等待队列分配 carroadsaid(int time, int ID, carqueue *L) carroadsaid(时间, 车牌, carqueue *L 队列表) */
int carroadsaid(int time, int ID, carqueue *L)
{
    qptr *T;
    T = (qptr *)malloc(sizeof(qptr));
    if (T == NULL) return 0;//地址分配失败,返回失败
    T->num = outsidenum++;
    T->ID = ID;
    T->arrivetime = time;
    T->next = NULL;
    L->rear->next = T;
    L->rear = T;
    return 1;
}



/* 这里是出库的核心函数 具体使用如下: 通过ID(车牌)寻找库内ID(车牌),如果不位于库顶(栈顶),就转移前方车辆至临时路边等待 出库后,计算停车时间,并将外面临时等待的车重新放入车库 如果入库后仍有空位,就将外面的等待队列添加进库,直到库满(栈满) outparking(int ID, int time, carpark *P, carqueue *L) outparking(车牌ID, 出库时间int , carpark *P 库表, carqueue *L 队列表) */
int outparking(int ID, int time, carpark *P, carqueue *L)
{
    int down = M-1;
    while (down >= 0)
    {
        if (P->stack[down].ID == ID)//找到位置
        {
            if (down == P->top)//车在最外面
            {
                //这里是车在最外面的code
                printf("\t\n车牌为%d的,已出库!停车时间:%d,请按标准收费!\n\n", P->stack[down].ID, time - P->stack[down].arrivetime);
                P->stack[down].ID = P->stack[down].arrivetime = P->stack[down].num = -1;//清空位置
                --internum;//库内记数减一个
                --P->top;
                while (parkfull(P) == 0)//出库后再看有空位没有
                {
                    //有空位就把等待队列的第一个放进来,且重新输入进入时间
                    //出队操作
                    int _time;
                    qptr *temp;
                    if (L->front == L->rear || L->front->next == NULL) return 0;//队列是空的,直接返回就好
                    temp = L->front->next;
                    P->stack[++P->top].num = internum++;
                    P->stack[P->top].ID = temp->ID;
                    printf("\t\t\n\n车库有空位,请输入队列此车(车牌为:%d)当前入库时间:", temp->ID);
                    scanf("%d", &_time);
                    P->stack[P->top].arrivetime = _time;
                    printf("\n\t此车已在队列中等待:%d time,现已进入车库!\n\n", _time - temp->arrivetime);
                    /*调整队头指针位置,并释放内存空间*/
                    L->front->next = temp->next;
                    if (L->rear == temp) L->rear = temp;//对于队列只剩一个数据时的队头调整
                    outsidenum--;
                    free(temp);
                }
                //outqueue(ID, time, P, L);//库空填充函数
                return 1;
            }
            else 
            {
                //这里是车不在最外的code
                //down变量就是当前找到的位置,已经是数组了,不用减1
                carpark *temp;
                int x;
                temp = initstack();//初始化临时停车场
                P->top = down;//P表的top现在直接调整到找到元素那里
                for (x=M-1; x >down; x--)
                {
                    temp->stack[++temp->top].ID = P->stack[x].ID;
                    temp->stack[temp->top].arrivetime = P->stack[x].arrivetime;
                    temp->stack[temp->top].num = P->stack[x].num;
                }//往临时停车场停车
                printf("\t\n车牌为%d的,已出库!停车时间:%d,请按标准收费!\n\n", P->stack[down].ID, time - P->stack[down].arrivetime);
                //P->stack[down].ID = P->stack[down].arrivetime = P->stack[down].num = -1;//清空位置
                --internum;//库内记数减一个
                --P->top;//移动top指针
                while (temp->top >= 0)
                {
                    if (temp->stack[temp->top].num != -1) //判断是否已经取到原栈高度,也就是不用复制-1值过来,要不然top位置会错位
                    {
                        P->stack[++P->top].ID = temp->stack[temp->top].ID;
                        P->stack[P->top].arrivetime = temp->stack[temp->top].arrivetime;
                        P->stack[P->top].num = temp->stack[temp->top].num;
                        temp->top--;//temp表top位置改变
                        //P->top++;//P表top位置改变
                    }
                    else
                    {
                        break;//顶上是空不再复制过来
                    }
                }//重新放回车库
                for (x = P->top+1; x < M; x++)
                    P->stack[x].arrivetime = P->stack[x].ID = P->stack[x].num = -1;//向上清空车库
                /* 这里应该还要判断车库有空位,所以放上面的代码过来 要改成单独的函数,要不然太长了 不具有独立性 =========================================================================== int _num, _time; qptr *temp; if (L->front == L->rear || L->front->next == NULL) return 0;//队列是空的,直接返回就好 temp = L->front->next; P->stack[++P->top].num = internum++; P->stack[P->top].ID = temp->ID; printf("\t\t\n\n车库有空位,请输入队列此车(车牌为:%d)当前入库时间:", temp->ID); scanf("%d", &_time); P->stack[P->top].arrivetime = _time; printf("\n\t此车已在队列中等待:%d time,现已进入车库!\n\n", _time - temp->arrivetime); //调整队头指针位置,并释放内存空间 L->front->next = temp->next; if (L->rear == temp) L->rear = temp;//对于队列只剩一个数据时的队头调整 free(temp); =========================================================================== */
                //outqueue(ID, time, P, L);//库空填充函数
                while (parkfull(P) == 0)//出库后再看有空位没有
                {
                    //有空位就把等待队列的第一个放进来,且重新输入进入时间
                    //出队操作
                    int _time;
                    qptr *temp;
                    if (L->front == L->rear || L->front->next == NULL) return 0;//队列是空的,直接返回就好
                    temp = L->front->next;
                    P->stack[++P->top].num = internum++;
                    P->stack[P->top].ID = temp->ID;
                    printf("\t\t\n\n车库有空位,请输入队列此车(车牌为:%d)当前入库时间:", temp->ID);
                    scanf("%d", &_time);
                    P->stack[P->top].arrivetime = _time;
                    printf("\n\t此车已在队列中等待:%d time,现已进入车库!\n\n", _time - temp->arrivetime);
                    /*调整队头指针位置,并释放内存空间*/
                    L->front->next = temp->next;
                    if (L->rear == temp) L->rear = temp;//对于队列只剩一个数据时的队头调整
                    outsidenum--;
                    free(temp);
                }
                break;
            }
        }
        down--;//下个比较位置
    }
    return 2;
}
int check()//检查队列数量
{
    return outsidenum;
}

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