模拟港澳通行证办理流程系统(队列基础练习)

来源:互联网 发布:左右脑平衡 知乎 编辑:程序博客网 时间:2024/04/28 19:13
#define N 2
#define M 5
#define True 1
#define False 0
#define NULL 0
typedef struct
{
    int num;
    int arrtime;
}ELEMTP;
typedef struct node
{
    int num;
    struct node *next;
}QNode;
typedef struct
{
    QNode *front,*rear;
}LQueue;

void InitQueue(LQueue *s);
void EnQueue(LQueue *q, int num1);
int DelQueue(LQueue *q);

void InitQueue(LQueue *q)
{
    q->front = (QNode *)malloc(sizeof(QNode));
    q->rear = q->front;
    q->front->next = NULL;
    q->front->num = 0;
}

void EnQueue(LQueue *q,int num1)
{
    QNode *p;
    p = (QNode *)malloc(sizeof(QNode));
    p->next = NULL;
    p->num = num1;
    q->rear->next = p;
    q->rear = p;
    q->front->num++;
}

int DelQueue(LQueue *q)
{
    QNode *p;
    int n;
    if(q->front == q->rear)
    {
        printf("no person here");
        return 0;       
    }
    else
    {
        p = q->front->next;
        q->front->next = p->next;
        if(p->next == NULL)
        {
            q->rear = q->front;
        }
        n = p->num;
        free(p);
        q->front->num--;
        return n;
    }
}

int Display(LQueue *q)
{
    int k;
    QNode *p;
    if(q->front->num)
    {
        for(k = 1,p = q->front->next;p;p = p->next)
        {
            printf("%d NUM:%d\n",k++,p->num);
        }
    }

}

int Endq(LQueue *q)
{
    while(q->front->num)
    {
        DelQueue(q);
    }
    printf("the queue is empty\n");

}

int main()
{
    LQueue * q,* q1,* q2;
    char ch2;
    int n = 100;
    char cmd;
    int num;
    int quenum = 0;
    q1 = (LQueue *)malloc(sizeof(LQueue));
    q2 = (LQueue *)malloc(sizeof(LQueue));
    while(1)
    {
        printf("********************************************************************\n");
        printf("opt   E:enqueue  D:deal  I:start construction  L:time up  R:status\n  ");
        printf("\n");
        cmd = getchar();
        printf("your input is %c\n",cmd);
        printf("please choice your queue:1~2\n");
        ch2 = getchar();
        scanf("%d",&quenum);
        printf("queue = %d\n",quenum);
        if(quenum == 1)
        {
            q = q1;
        }
        else
        {
            q = q2;
        }
        
        if((q->front == NULL)&&(cmd != 'I'))
        {
                printf("makesure we are in workting ime\n");
        }
        switch(cmd)
        {
            case 'I':
                InitQueue(q);
            break;
            case 'E':
                printf("your num is %d\n",n);
                
                EnQueue(q,n++);
            break;
            case 'D':
                num = DelQueue(q);
                if(num == 0)
                {
                    continue;
                }
                printf("It's %d's turn\n",num);
            break;
            case 'R':
                printf("the current status of the queue: \n");
                Display(q);
            break;
            case 'L':
                printf("time is up\n");
                Endq(q);
            break;
            default:
                printf("cmd err\n");
            break;
        }
        ch2 = getchar();
    }
}