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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章