数据结构——队列

什么是队列

队列是一种特殊的线性表,既可以以数组形式存储也可以以链式结构的形式存储。以数组形式存储称为数组队列(特殊的线性表),以链式结构的形式存储称为链式队列(特殊的链表)。
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out) 入队列的原则。
队尾:进行插入操作的一端。
队头:进行删除操作的一端。
队列是从队尾插入数据,队尾不能删除数据。
从队头删除数据,队头不能插入数据。

程序实例

首先是队列的头文件 Queue.h:

// Queue.h
#ifndef _QUEUE_H_
#define _QUEUE_H_

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

typedef int QDataType;

// 链式结构:表示队列节点
typedef struct QListNode {
    QDataType data;
    struct QListNode* pNext;
} QNode;

// 队列的结构
typedef struct Queue {
    QNode* head;
    QNode* tail;
} Queue;

// 初始化队列
void QueueInit(Queue* q);

// 动态申请一个节点
QNode* BuyQNode(QDataType x);

// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q);

// 销毁队列
void QueueDestroy(Queue* q);

// 队尾入队列
void QueuePush(Queue* q, QDataType x);

// 队头出队列
void QueuePop(Queue* q);

// 获取队列头部元素
QDataType QueueFront(Queue* q);

// 获取队列队尾元素
QDataType QueueBack(Queue* q);

// 获取队列中有效元素个数
int QueueSize(Queue* q);

// 打印队列
void QueuePrint(Queue* q);

#endif // _QUEUE_H_

接下来是队列的实现 Queue.c:

// Queue.c
#include "Queue.h"

// 初始化队列
void QueueInit(Queue* q) {
    q->head = q->tail = NULL;
}

// 动态申请一个节点
QNode* BuyQNode(QDataType x) {
    QNode* newnode = (QNode*)malloc(sizeof(QNode));
    if (newnode == NULL) {
        printf("malloc fail\n");
        exit(-1);
    }
    newnode->data = x;
    newnode->pNext = NULL;
    return newnode;
}

// 销毁队列
void QueueDestroy(Queue* q) {
    QNode* cur = q->head;
    while (cur != NULL) {
        QNode* next = cur->pNext;
        free(cur);
        cur = next;
    }
    q->head = q->tail = NULL;
}

// 队尾入队列
void QueuePush(Queue* q, QDataType x) {
    QNode* newnode = BuyQNode(x);
    if (q->tail != NULL) {
        q->tail->pNext = newnode;
    } else {
        q->head = newnode;
    }
    q->tail = newnode;
}

// 队头出队列
void QueuePop(Queue* q) {
    assert(!QueueEmpty(q));
    QNode* cur = q->head;
    q->head = q->head->pNext;
    free(cur);
    if (q->head == NULL) {
        q->tail = NULL;
    }
}

// 获取队列头部元素
QDataType QueueFront(Queue* q) {
    assert(!QueueEmpty(q));
    return q->head->data;
}

// 获取队列队尾元素
QDataType QueueBack(Queue* q) {
    assert(!QueueEmpty(q));
    return q->tail->data;
}

// 获取队列中有效元素个数
int QueueSize(Queue* q) {
    int count = 0;
    QNode* cur = q->head;
    while (cur != NULL) {
        count++;
        cur = cur->pNext;
    }
    return count;
}

// 打印队列
void QueuePrint(Queue* q) {
    QNode* cur = q->head;
    while (cur != NULL) {
        printf("%d ", cur->data);
        cur = cur->pNext;
    }
    printf("\n");
}

最后,我们创建一个测试用例 main.c 来展示如何使用这些函数:

// main.c
#include <stdio.h>
#include "Queue.h"

int main() {
    Queue qu;
    // 初始化队列
    QueueInit(&qu);
    // 入队操作
    QueuePush(&qu, 1);
    QueuePush(&qu, 2);
    QueuePush(&qu, 3);
    QueuePush(&qu, 4);
    // 打印队列
    QueuePrint(&qu);
    // 出队操作
    QueuePop(&qu);
    QueuePop(&qu);
    // 再次打印队列
    QueuePrint(&qu);
    // 获取队列头部元素
    printf("Front element is: %d\n", QueueFront(&qu));
    // 获取队列尾部元素
    printf("Back element is: %d\n", QueueBack(&qu));
    // 获取队列元素个数
    printf("Queue size is: %d\n", QueueSize(&qu));
    // 销毁队列
    QueueDestroy(&qu);
    return 0;
}

要运行这个程序,您需要将 Queue.h、Queue.c 和 main.c 保存到您的计算机上,然后使用C编译器编译它们。例如,如果您使用的是GCC编译器,可以通过以下命令编译程序:

gcc -o main Slist.c main.c
./main

原文链接

https://blog.csdn.net/qq_52158753/article/details/129929860

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值