C与C++队列实现

C队列实现:

#include <stdio.h>

//节点
struct Node{
    int data;//节点数据
    Node *next;//下一节点
};

//队列
struct Queue{
    Node *front;//队头
    Node *end;//队尾
};

//创建节点
struct Node* createNode(int _data){
    //创建节点内存
    struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = _data;//节点数据
    newNode->next = NULL;//下一节点
    return newNode;//返回节点
};

//创建队列
struct Queue* createQueue(){
    //分配队列内存
    struct Queue *newQueue = (struct Queue*)malloc(sizeof(struct Queue));
    newQueue->front = newQueue->end = NULL;//默认队头队尾为空
    return newQueue;//返回队列
};

//空队判断
bool isEmpty(struct Queue *q){
    return q->front == NULL;
}

//取队头数据
int front(struct Queue *q){
    if (isEmpty(q)) {
        printf("队列为空");
        return -1;
    }
    return q->front->data;
}

//队列大小
int size(struct Queue *q){
    int count = 0;
    struct Node *cur = q->front;

相关推荐

  1. CC++队列实现

    2024-01-09 20:08:03       57 阅读
  2. c++ 实现栈、单向队列、双向队列

    2024-01-09 20:08:03       24 阅读
  3. 队列实现栈(C

    2024-01-09 20:08:03       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-09 20:08:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-09 20:08:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-09 20:08:03       82 阅读
  4. Python语言-面向对象

    2024-01-09 20:08:03       91 阅读

热门阅读

  1. Zookeeper集群+Kafka集群

    2024-01-09 20:08:03       56 阅读
  2. okhttpclient.setsslsocketfactory 报错解决

    2024-01-09 20:08:03       60 阅读
  3. 「 PyMuPDF专栏 」PyMuPDF为PDF文件添加注释

    2024-01-09 20:08:03       52 阅读
  4. 彻底卸载Microsoft Edge的几种方法

    2024-01-09 20:08:03       80 阅读
  5. Docker 的基本概念和优势

    2024-01-09 20:08:03       52 阅读
  6. PHP 完整表单实例

    2024-01-09 20:08:03       48 阅读
  7. leetcode09-机器人能否返回原点

    2024-01-09 20:08:03       64 阅读
  8. LeetCode 447. 回旋镖的数量,枚举+哈哈希

    2024-01-09 20:08:03       72 阅读
  9. leetcode07-罗马数字的转换

    2024-01-09 20:08:03       64 阅读
  10. Qt基础-容器类详解

    2024-01-09 20:08:03       58 阅读
  11. 国内免费协同办公软件盘点,快速入门指南

    2024-01-09 20:08:03       58 阅读