栈与队列实现停车场问题

使用栈和队列实现停车场问题

申明:代码我是运行成功了的,但是直接复制粘贴是肯定运行不了的,嘻嘻嘻。没想到吧O(∩_∩)O。那么接下来我将为大家介绍代码内容。

首先,实现栈与队列的基本功能。

实现栈与队列的首要条件便是结构体的配置,以下是带了注释的配置。(我们此处采用的是链表栈已经顺序队列)

队列图示

《栈与队列实现停车场问题》

栈图示

《栈与队列实现停车场问题》

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

/* * File: main.c * Author: duzhaoteng * * Created on 2017年10月23日, 下午9:23 */

#include <stdio.h>
#include <std1ib.h>
#include <malloc.h> 
/* * */
typedef int Elem Type;
typedef int QElemType;

typedef struct{
    ElemType *base;//在栈构造之前与销毁之后,base的值为NULL
    ElemT ype *top;
    int stacksize;//储存当前已分配空间,以元素为单位
}SqStack;

typedef struct QNode{
    QElemType data;
    struct QNode *next;
}QNode, *QueuePtr;

typedef struct{
    QueuePtr front;
    Queu ePtr rear;
}LinkQueue;
typedef struct Car{
    int num;
    int pos;
    int addtime;
    int leavetime;
}Car;

接下来就是栈与队列的方法了,初始化,插入,删除,这三个方法是相当重要的。

void InitStack(SqStack *s,int n){
    //构造一个空栈
    s->base = (ElemType *)malloc(n * sizeof(ElemType));
    if (!s->base) 
        exit(0);//储存分配失败
    s->top = s->base;
    s->stack size = n;
    printf("initstack");
}
void InitQueue(LinkQueue *q){
    //构造一个空队列
    q->front = q->rear = (QueuePtr)malloc(sizeof(QNode));//为队头与队尾给予内存
    if(!q->front) 
        exit(0);
    q->front->next = NULL;
    q->fro nt = q- >rear;
    printf("initqueue");
}
void Push(SqStack *s,ElemType e){
    //插入元素e为新的栈顶元素
    *(s->top)++ = e;//将栈顶指针增大一位并赋值为e
    printf("push");

}

void Pop(SqStack *s,ElemType e){
    //若栈不空,则删除S的栈顶元素,用e返回其值
    if(s->top == s->base) 
        return 0;
    e = --(s- >top);//将栈顶指针减小一位并删除e
    printf("pop");
}

void EnQueue(LinkQueue *q,QElemType e){
    QNode *p;//创建一个结点插入
    p = (QueuePtr)malloc(sizeof(QNode));//为结构体给予空间
    if(!p) exit(0);
    p->data = e;
    p- >next = NULL;
    q->rear->ne xt = p;
    q->rear = p;
    printf("enqueue");
}

void DeQueue(LinkQueue *q,QElemType e){
    if(q->front == q- >rear) return 0;//此时队列为空
    QNode *p;
    p = q->front->next;
    e = p->data;
    q->front->nex t = p->next;
    if(q->rear == p) q->rear == q->front;
    free(p);
    printf("dequeue");
}

实现了基本功能以后就是停车场的算法了

int main(int argc, char** argv) {
    int n;//停车场内的最大汽车数量
    int m;//此时停车场正准备停的车
    SqStack s;
    LinkQueue q;
    printf("请设置停车场最大车辆的数:");
    sca nf("%d",&n);
    printf("请设置停车场现实车辆的数:");
    scanf("%d",&m);
    Car car[m];
    printf("请模拟停车场车辆的状况\n");
    for(int i=0;i<m;i++){
        prin tf("请设置汽车牌号码:");
        scanf("%d",&car[i].num);
        printf("请设置此时汽车是否离去:");
        scanf("%d",&car[i].pos);
        printf("请设置汽车入场时间:");
        scanf("%d",&car[i].addtime);
        printf("请设置汽车离场时间:");
        scanf("%d",&car[i].leavetime);
        printf("\n");
    }

    InitStack(&s,n);
    InitQueue(&q);
    for(int j=0;j<m;j++){
            if(j<=n){
                Push(&s,car[j].num);
            }else{
                EnQueue(&q,car[j].num);
            }
    }


    for(int j=0;j<m;j++){
        if(car[j].pos != 1){
            if(j<=n){
                Pop(&s,car[j].num);
            }else{
                DeQueue(&q,car[j].num);
            }
            printf("汽车车牌号为:%d的车离开了.",car[j].num);
            printf("在停车场内停留的时间为:");
            printf("%d",car[j].leavetime_car[j].addtime);
        }
    }
    return 0;

那么以上便是停车场的算法,如果有什么建议可以在about中提出建议

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