以下是使用C语言实现与前面提供的代码相同功能的代码:

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

#define N 100000

int qu[N];
int head = 1;
int tail = 0;

void push(int x) {
    qu[++tail] = x;
}

void pop() {
    if (tail >= head)
        head++;
    else
        printf('FBI WARNING\n');
}

bool empty() {
    if (tail >= head)
        return false;
    else
        return true;
}

int query() {
    if (tail >= head)
        return qu[head];
    else {
        printf('FBI WARNING\n');
        return -1;
    }
}

int main() {
    int n;
    scanf('%d', &n);
    char str[10];
    int x;

    while (n--) {
        scanf('%s', str);

        if (strcmp(str, 'push') == 0) {
            scanf('%d', &x);
            push(x);
        }
        else if (strcmp(str, 'pop') == 0) {
            pop();
        }
        else if (strcmp(str, 'empty') == 0) {
            if (empty())
                printf('YES\n');
            else
                printf('NO\n');
        }
        else {
            printf('%d\n', query());
        }
    }

    return 0;
}

这段C语言代码实现了与之前提供的代码相同的队列操作。使用 push() 函数向队尾插入一个数,pop() 函数从队头弹出一个数,empty() 函数判断队列是否为空,query() 函数查询队头元素的值。在主函数中,根据输入的操作命令执行相应的操作,并输出结果。

希望这段代码对你有帮助!如果还有其他问题,请随时提问。

C语言实现队列操作:代码示例及解析

原文地址: https://www.cveoy.top/t/topic/crsP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录