c: Queue Calling

发布时间 2023-10-19 14:51:19作者: ®Geovin Du Dream Park™

 

/**
 * *****************************************************************************
 * @file        TakeNumber.h
 * @brief       排队等号
 * @author       (geovindu,Geovin Du,涂聚文)
 * @date        2023-10-19
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * *****************************************************************************
 */

#ifndef TAKENUMBER_H
#define TAKENUMBER_H

#include <stdio.h>
#include <stdlib.h>


//循环队列 
#define QUEUEMAX 15

/**
 * @brief   排队结构体    
 * 
 */
typedef struct
{
    int num; //顾客编号 
    long time;//进入队列时间 
}DATA;

/**
 * @brief   队列数组     
 * 
 */
typedef struct
{
    DATA data[QUEUEMAX]; 
    int head; //队头 
    int tail; //队尾 
}QueueCalling;

/**
 * @brief      初始化队列  
 * 
 * @return      CycQueue* 
 */
QueueCalling *CycQueueInit();


/**
 * @brief       释放队列
 * 
 * @param       q  队列数组
 */
void CycQueueFree(QueueCalling *q); 

/**
 * @brief     队列是否为空  
 * 
 * @param       q 队列数组
 * @return      int 
 */
int CycQueueIsEmpty(QueueCalling *q); 

/**
 * @brief       队列是否已满 
 * 
 * @param       q 队列数组
 * @return      int 
 */
int CycQueueIsFull(QueueCalling *q);

/**
 * @brief       入队函数
 * 
 * @param       q 队列数组
 * @param       data 
 * @return      int 
 */
int CycQueueIn(QueueCalling *q,DATA data);

/**
 * @brief     循环队列的出队函数  
 * 
 * @param       q 队列数组
 * @return      DATA* 
 */
DATA *CycQueueOut(QueueCalling *q);

/**
 * @brief       获取队列长度
 * 
 * @param       q 队列数组
 * @return      int 返回有多少个排队人员
 */
int CycQueueLen(QueueCalling *q);  


/**
 * @brief       获取队定中第1个位置的数据
 * 
 * @param       q 队列数组
 * @return      DATA* 
 */
DATA *CycQueuePeek(QueueCalling *q); 



#endif

  

/**
 * *****************************************************************************
 * @file        TakeNumber.c
 * @brief       排队等号
 * @author       (geovindu,Geovin Du,涂聚文)
 * @date        2023-10-19
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * *****************************************************************************
 */


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "include/TakeNumber.h"





/**
 * @brief     初始化队列   
 * 
 */
QueueCalling *CycQueueInit()
{
    QueueCalling *q;
    if(q=(QueueCalling *)malloc(sizeof(QueueCalling))) //申请保存队列的内存 
    {
        q->head = 0;//设置队头 
        q->tail = 0;//设置队尾 
        return q;
    }else
        return NULL; //返回空 
}
/**
 * @brief  释放队列    
 * @param q 队列数组
 */
void CycQueueFree(QueueCalling *q) 
{
    if (q!=NULL)
        free(q);
} 

/**
 * @brief       队列是否为空
 * @param q 队列数组
 */
int CycQueueIsEmpty(QueueCalling *q)   
{
    return (q->head==q->tail);
}

/**
 * @brief       队列是否已满 
 * @param q 队列数组
 */
int CycQueueIsFull(QueueCalling *q)
{
    return ((q->tail+1)%QUEUEMAX==q->head);
}

/**
 * @brief       入队函数
 * @param q  队列数组
 * @param data  排队信息结构体
 * 
 */
int CycQueueIn(QueueCalling *q,DATA data)
{
    if((q->tail+1)%QUEUEMAX == q->head )
    { 
        printf("队列满了!\n");
        return 0;
    }else{
        q->tail=(q->tail+1)%QUEUEMAX;//求列尾序号 
        q->data[q->tail]=data;
        return 1;
    }
}

/**
 * @brief       循环队列的出队函数
 * @param q 队列数组
 */
DATA *CycQueueOut(QueueCalling *q)
{
    if(q->head==q->tail) //队列为空 
    {
        printf("队列空了!\n");
        return NULL;
    }else{
        q->head=(q->head+1)%QUEUEMAX;
        return &(q->data[q->head]);
    }
}

/**
 * @brief       获取队列长度 
 * @param q 队列数组
 */
int CycQueueLen(QueueCalling *q) 
{
    int n;
    n=q->tail-q->head;
    if(n<0)
        n=QUEUEMAX+n;
    return n;
}

/**
 * @brief       获取队定中第1个位置的数据
 * @param q 队列数组
 */ 
DATA *CycQueuePeek(QueueCalling *q) 
{
    if(q->head==q->tail)
    {
        printf("队列已经空了!\n");
        return NULL; 
    }else{
        return &(q->data[(q->head+1)%QUEUEMAX]);
    }
} 

  

 

/**
 * *****************************************************************************
 * @file        CheckTieck.h
 * @brief       排队等号
 * @author       (geovindu,Geovin Du,涂聚文)
 * @date        2023-10-19
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * *****************************************************************************
 */

#ifndef CHECKTIECK_H
#define CHECKTIECK_H


#include <stdio.h>
#include <stdlib.h>
#include "TakeNumber.h"

//顾客序号 
  int num;

/**
 * @brief     新增顾客排列   
 * 
 * @param       q  队列数组
 * 
 */
void add(QueueCalling *q);

/**
 * @brief    通知下一顾客准备    
 * 
 * @param       q  队列数组
 * 
 */
void next(QueueCalling *q); 

#endif

  

 

/**
 * *****************************************************************************
 * @file        CheckTieck.c
 * @brief       排队等号
 * @author       (geovindu,Geovin Du,涂聚文)
 * @date        2023-10-19
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * *****************************************************************************
 */


#include <stdio.h>
#include <stdlib.h>
#include "include/CheckTieck.h"





/**
 * @brief       新增顾客排列 
 * @param q
 */
void add(QueueCalling *q) 
{
    DATA data;
    if(!CycQueueIsFull(q)) //如果队列未满
    {
        data.num=++num;
        data.time=time(NULL);
        CycQueueIn(q,data);
    }
    else
        printf("\n排队的人实在是太多了,请您稍候再排队!\n"); 
}

/**
 * @brief       通知下一顾客准备
 * @param q
 * 
 */
void next(QueueCalling *q)  
{
    DATA *data;
    if(!CycQueueIsEmpty(q)) //若队列不为空 
    {
        data=CycQueueOut(q); //取队列头部的数据 
        printf("\n欢迎编号为%d的顾客到柜台办理业务!\n",data->num);
    }
    if(!CycQueueIsEmpty(q)) //若队列不为空 
    {
        data=CycQueuePeek(q);//取队列中指定位置的数据 
        printf("请编号为%d的顾客做好准备,马上将为您办理业务!\n",data->num);      
    }
}

  

调用:

#include "include/CheckTieck.h"
#include "include/TakeNumber.h"






int main()
{

    printf("hello c world \n");
    printf("你好,中国\n");

    QueueCalling *queue1;
    int iii,nnn;
    char select;
    //int num=1;//顾客序号 
    num=0; //叫号编号
    queue1=CycQueueInit(); //初始化队列 
    if(queue1==NULL)
    {
        printf("创建队列时出错!\n");
        getch();
        return 0;
    }
    do{
        printf("\n请选择具体操作:\n");
        printf("1.新到顾客\n");
        printf("2.下一个顾客\n");
        printf("0.退出\n") ;
        fflush(stdin);
        select=getch();
        switch(select)
        {
            case '1':
                add(queue1);
                printf("\n现在共有%d位顾客在等候!\n",CycQueueLen(queue1));
                break;
            case '2':
                next(queue1);
                printf("\n现在共有%d位顾客在等候!\n",CycQueueLen(queue1));
                break;
            case '0':
                break;
        }        
    }while(select!='0');
    CycQueueFree(queue1); //释放队列
    getch();



    //内存分配函数 malloc()  分配并初始化函数 calloc() 重新分配内存函数 realloc 释放内存free()
    int *buf1,* buf2, * buf3;
    buf1=(int*)malloc(100*sizeof(int));
    buf2=(int*)calloc(100,sizeof(int));
    buf3=(int*)realloc(buf2,500*sizeof(int));
    free(buf1);
    free(buf3);

  return 0;  
}

  

输出: