构建一个链式队列,当用户输入数字时,将数字入队,当用户输入字母时,将队头元素出队。每次操作队列之后,将队列中的元素显示出来。

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

typedef int dataType;

typedef struct node
{
    dataType data;
    struct node *next;
} node;

typedef struct linkQueue
{
    node *front;
    node *rear;
    int size;
} linkQueue;

linkQueue *init_linkQueue(void)
{
    linkQueue *lq = malloc(sizeof(linkQueue));
    if (lq == NULL)
    {
        return NULL;
    }
    lq->front = NULL;
    lq->rear = NULL;
    lq->size = 0;
    return lq;
}

node *init_node(dataType data)
{
    node *pnew = malloc(sizeof(node));
    if (pnew == NULL)
    {
        return NULL;
    }
    pnew->data = data;
    pnew->next = NULL;

    return pnew;
}

bool isEmpty(linkQueue *lq)
{
    return lq->size == 0;
}

bool enQueue(linkQueue *lq, dataType data)
{
    node *pnew = init_node(data);
    if (pnew == NULL)
    {
        return false;
    }
    if (isEmpty(lq))
    {
        lq->front = pnew;
        lq->rear = pnew;
        lq->size++;
    }
    else
    {
        lq->rear->next = pnew;
        lq->rear = pnew;
        lq->size++;
    }
    return true;
}

bool get_front(linkQueue *lq, dataType *data)
{
    if (isEmpty(lq))
    {
        return false;
    }
    *data = lq->front->data;
    return true;
}

bool out_Queue(linkQueue *lq, dataType *data)
{
    if (!get_front(lq, data))
    {
        return false;
    }
    if (lq->size == 1)
    {
        free(lq->front);
        lq->front = NULL;
        lq->rear = NULL;
    }
    else
    {
        node *temp = lq->front;
        lq->front = lq->front->next;
        temp->next = NULL;
        free(temp);
    }
    lq->size--;
    return true;
}

void show(linkQueue *lq)
{
    node *p = lq->front;
    dataType num = 0;
    printf("当前队列:");

    for (; num < lq->size;num++, p = p->next)
    {
        printf("%d\t", p->data);
    }

    printf("\n");
}

int main(int argc, char const *argv[])
{
    linkQueue *lq = init_linkQueue();
    if (lq == NULL)
    {
        return -1;
    }
    // enQueue(lq, 1);
    // enQueue(lq, 2);
    // enQueue(lq, 3);

    // dataType data;
    // while (1)
    // {
    //     if (!out_Queue(lq, &data))
    //     {
    //         printf("队列为空\n");
    //         break;
    //     }
    //     printf("%d\n", data);
    // }
    while (1)
    {
        dataType data;
        if (scanf("%d", &data) == 0)
        {
            while (getchar() != '\n')
            {
                if (isEmpty(lq))
                {
                    printf("队列为空\n");
                    break;
                }
                out_Queue(lq, &data);
                printf("%d\n", data);
            }
        }
        else
        {
            enQueue(lq, data);
        }
        show(lq);
    }

    return 0;
}

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值