数据结构与算法:队列-单链表实现(c语言实现,带详细注释)

//Queue.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>


typedef int QDataType;

typedef struct QueueNode
{
    struct QueueNode * next;
    QDataType data;
}QNode, *pQNode;

typedef struct Queue
{
    pQNode head;
    pQNode tail;
}Queue, *pQueue;

void Queue_Init(pQueue queue);
void Queue_Destory(pQueue queue);

void Queue_Push(pQueue queue, QDataType value);
void Queue_Pop(pQueue Queue_Pop);

QDataType Queue_Front(pQueue queue);
QDataType Queue_Back(pQueue queue);
int Queue_Size(pQueue queue);
bool Queue_IsEmpty(pQueue queue);
//Qurur.c
#include "Queue.h"  // 包含自定义的队列头文件

// 初始化队列
void Queue_Init(pQueue queue)
{
    assert(queue); // 断言queue不为NULL
    queue->head = queue->tail = NULL; // 将队列的头指针和尾指针置为NULL,表示队列为空
}

// 销毁队列,释放所有节点的内存
void Queue_Destory(pQueue queue)
{
    assert(queue); // 断言queue不为NULL
    pQNode cur = queue->head; // 定义一个指针cur指向队列头节点
    while (cur)
    {
        pQNode next = cur->next; // 保存下一个节点的指针
        free(cur); // 释放当前节点的内存
        cur = next; // 将cur指向下一个节点,继续释放
    }
    queue->head = queue->tail = NULL; // 将队列的头指针和尾指针置为NULL,表示队列已被销毁
}

// 入队操作
void Queue_Push(pQueue queue, QDataType value)
{
    assert(queue); // 断言queue不为NULL
    pQNode newnode = (pQNode)malloc(sizeof(QNode)); // 分配新节点的内存
    if(newnode == NULL) // 检查内存分配是否成功
    {
        printf("malloc failed!"); // 分配失败,输出错误信息
        exit(-1); // 退出程序,返回-1表示异常终止
    }
    newnode->data = value; // 设置新节点的值为value
    newnode->next = NULL; // 将新节点的next指针置为NULL

    if(queue->tail == NULL) // 如果队列为空
    {
        queue->head = newnode; // 将头指针和尾指针都指向新节点
        queue->tail = newnode;
    }
    else
    {
        queue->tail->next = newnode; // 将新节点连接到尾节点的后面
        queue->tail = newnode; // 更新尾指针为新节点
    }
}

// 出队操作
void Queue_Pop(pQueue queue)
{
    assert(queue); // 断言queue不为NULL
    assert(queue->head); // 断言队列不为空

    if(queue->head->next == NULL) // 如果队列只有一个节点
    {
        free(queue->head); // 直接释放头节点的内存
        queue->head = NULL; // 将头指针和尾指针都置为NULL,表示队列为空
        queue->tail = NULL;
    }
    else
    {
        pQNode next = queue->head->next; // 保存下一个节点的指针
        free(queue->head); // 释放头节点的内存
        queue->head = next; // 更新头指针为下一个节点
    }
}

// 获取队列头部元素的值,但不出队
QDataType Queue_Front(pQueue queue)
{
    assert(queue); // 断言queue不为NULL
    assert(queue->head); // 断言队列不为空

    return queue->head->data; // 返回队列头部元素的值
}

// 获取队列尾部元素的值,但不出队
QDataType Queue_Back(pQueue queue)
{
    assert(queue); // 断言queue不为NULL
    assert(queue->head); // 断言队列不为空

    return queue->tail->data; // 返回队列尾部元素的值
}

// 获取队列中的元素个数
int Queue_Size(pQueue queue)
{
    int size = 0; // 初始化元素个数为0
    pQNode cur = queue->head; // 定义一个指针cur指向队列头节点
    while (cur)
    {
        cur = cur->next; // 移动cur指针到下一个节点
        size++; // 元素个数加1
    }
    return size; // 返回元素个数
}

// 检查队列是否为空
bool Queue_IsEmpty(pQueue queue)
{
    assert(queue); // 断言queue不为NULL

    return queue->head == NULL; // 头指针为NULL表示队列为空,返回true,否则返回false
}

// 主函数
int main()
{
    Queue q; // 定义队列变量
    Queue_Init(&q); // 初始化队列

    // 将元素依次入队
    Queue_Push(&q, 1);
    Queue_Push(&q, 2);
    Queue_Push(&q, 3);
    Queue_Push(&q, 4);
    Queue_Push(&q, 5);

    // 依次出队并打印出队的元素值
    while (!Queue_IsEmpty(&q))
    {
        printf("%d ", Queue_Front(&q));
        Queue_Pop(&q);
    }
    printf("\n");
    
    // 输出队列的元素个数
    printf("Queue size is: %d", Queue_Size(&q));
    printf("\n");

    Queue_Destory(&q); // 销毁队列,释放内存
    return 0; // 返回0表示正常结束程序
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
全集内容结构如下: ├─图 │ ├─关键路径(有向无环图及其应用2) │ │ 1.txt │ │ ALGraph.cpp │ │ ALGraph.h │ │ CriticalPath.cpp │ │ CriticalPath.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ SqStack.cpp │ │ SqStack.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的关节点 │ │ 1.txt │ │ ALGraph.cpp │ │ ALGraph.h │ │ FindArticul.cpp │ │ FindArticul.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的数组表示法 │ │ InfoType.cpp │ │ InfoType.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的遍历 │ │ ALGraph.cpp │ │ ALGraph.h │ │ DEBUG.txt │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ MTraverse.cpp │ │ MTraverse.h │ │ Status.h │ │ t1.txt │ │ t2.txt │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的邻接表存储结构 │ │ ALGraph.cpp │ │ ALGraph.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ Status.h │ │ t1.txt │ │ t2.txt │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─最短路径(从某个源点到其余各顶点的的最短路径) │ │ 1.txt │ │ 2.txt │ │ InfoType.cpp │ │ InfoType.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ ShortestPath_DIJ.cpp │ │ ShortestPath_DIJ.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ └─最短路径(每一对顶点间的最短路径) │ 1.txt │ 2.txt │ InfoType.cpp │ InfoType.h │ Main.cpp │ map.txt │ MGraph.cpp │ MGraph.h │ RailwaySearch.cpp │ ShortestPath_FLOYD.cpp │ ShortestPath_FLOYD.h │ Status.h │ VertexType.cpp │ VertexType.h │ ├─排序 │ ├─冒泡排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_BubbleSort.cpp │ │ Sq_BubbleSort.h │ │ │ ├─哈希表(哈希查找) │ │ ElemType.cpp │ │ ElemType.h │ │ HashTable.cpp │ │ HashTable.h │ │ main.cpp │ │ Records.txt │ │ │ ├─基数排序 │ │ 1.txt │ │ main.cpp │ │ SLL_RadixSort.cpp │ │ SLL_RadixSort.h │ │ │ ├─归并排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ sq_MergeSort.cpp │ │ sq_MergeSort.h │ │ │ ├─快速排序 │ │ 1.txt │ │ 2.txt │ │ 3.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_QuitSort.cpp │ │ Sq_QuitSort.h │ │ │ ├─拓扑排序(有向无环图及其应用) │ │ 1.txt │ │ ALGraph.cpp │ │ ALGraph.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ SqStack.cpp │ │ SqStack.h │ │ Status.h │ │ TopologicalSort.cpp │ │ TopologicalSort.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─插入排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_InsertSort.cpp │ │ Sq_InsertSort.h │ │ │ ├─插入排序(希尔排序) │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_InsertSort.cpp │ │ Sq_InsertSort.h │ │ │ ├─插入排序(表插入排序) │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ SL_InsertSort.cpp │ │ SL_InsertSort.h │ │ │ ├─选择排序(堆排序) │ │ 1.txt │ │ 2.txt │ │ 3.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_HeapSort.cpp │ │ Sq_HeapSort.h │ │ │ ├─选择排序(树形选择排序) │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_TreeSelectionSort.cpp │ │ Sq_TreeSelectionSort.h │ │ │ └─选择排序(简选择排序) │ 1.txt │ main.cpp │ RedType.cpp │ RedType.h │ Sq_SelectSort.cpp │ Sq_SelectSort.h │ ├─查找 │ ├─动态查找表(二叉排序树) │ │ 1.txt │ │ BiTree.cpp │ │ BiTree.h │ │ DElemType.cpp │ │ DElemType.h │ │ DSTable.cpp │ │ DSTable.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─平衡二叉树(二叉排序树的平衡旋转生成) │ │ 1.txt │ │ BBSTree.cpp │ │ BBSTree.h │ │ BiTree.cpp │ │ BiTree.h │ │ DElemType.cpp │ │ DElemType.h │ │ DSTable.cpp │ │ DSTable.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─平衡的m路查找树—B_树 │ │ BTree.cpp │ │ BTree.h │ │ main.cpp │ │ Record.h │ │ Status.h │ │ │ ├─键树(Trie树) │ │ 1.txt │ │ main.cpp │ │ Record.h │ │ Status.h │ │ TrieTree.cpp │ │ TrieTree.h │ │ │ ├─键树(双链键树) │ │ 1.txt │ │ DLTree.cpp │ │ DLTree.h │ │ main.cpp │ │ Record.h │ │ Status.h │ │ │ ├─静态查找表(有序表的查找) │ │ 1.txt │ │ main.cpp │ │ SElemType.cpp │ │ SElemType.h │ │ SSTable.cpp │ │ SSTable.h │ │ Status.h │ │ │ ├─静态查找表(静态树表查找) │ │ 1.txt │ │ BiTree.cpp │ │ BiTree.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ SElemType.cpp │ │ SElemType.h │ │ SSTable.cpp │ │ SSTable.h │ │ Status.h │ │ TElmeType.h │ │ │ └─静态查找表(顺序表的查找) │ 1.txt │ main.cpp │ SElemType.cpp │ SElemType.h │ SSTable.cpp │ SSTable.h │ Status.h │ ├─树 │ ├─二叉树的二叉链表存储 │ │ BiTree.cpp │ │ BiTree.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─二叉树的顺序存储结构 │ │ main.cpp │ │ SqBiTree.cpp │ │ SqBiTree.h │ │ Status.h │ │ TELemType_define.cpp │ │ │ ├─哈夫曼树和哈夫曼编码 │ │ HuffmanTree.cpp │ │ HuffmanTree.h │ │ main.cpp │ │ Status.h │ │ │ ├─最小生成树 │ │ 1.txt │ │ InfoType.cpp │ │ InfoType.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ MiniSpanTree_Kruskal.cpp │ │ MiniSpanTree_Kruskal.h │ │ MiniSpanTree_PRIM.cpp │ │ MiniSpanTree_PRIM.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─树的二叉链表 │ │ CSTree.cpp │ │ CSTree.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─深度优先生成森林(无向图的连通性和生成树) │ │ ALGraph.cpp │ │ ALGraph.h │ │ CSTree.cpp │ │ CSTree.h │ │ DFSForest.cpp │ │ DFSForest.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ └─线索二叉树 │ BiThrTree.cpp │ BiThrTree.h │ main.cpp │ Status.h │ TElmeType.h │ └─表和数组 ├─KMP算法 │ Basic_operation_functions.h │ def_SString.h │ KMP.h │ main.cpp │ Status.h │ ├─n阶对称矩阵的压缩存储 │ Basic_operation_functions.h │ mian.cpp │ Status.h │ struct SyMatrix.h │ ├─三元组稀疏矩阵的压缩存储 │ Basic_operation_functions.h │ B_opera_fun_called_fun.h │ main.cpp │ Status.h │ struct TSMatrix.h │ Universal_main.h │ Universa_ts_b_opera_fun.h │ ├─不设头结点的链表 │ LinkList.cpp │ LinkList.h │ main.cpp │ Status.h │ ├─串的堆存储结构 │ Basic_operation_functions.h │ HString.h │ Line_List.h │ main.cpp │ Status.h │ ├─串的定长顺序存储结构 │ Basic_operation_functions.h │ def_SString.h │ Main.cpp │ Status.h │ ├─广义表 │ GList.cpp │ GList.h │ main.cpp │ SString.cpp │ SString.h │ Status.h │ ├─数组 │ Basic_operation_functions.h │ main.cpp │ Status.h │ struct array.h │ ├─文本编辑(串和行表操作) │ Basic_operation_functions.h │ HString.h │ Line_List.h │ main.cpp │ Status.h │ ├─栈的顺序存储结构 │ main.cpp │ SqStack.cpp │ SqStack.h │ Status.h │ ├─走迷宫 │ Basic_called_functions.h │ Basic_operation_functions.h │ Main_Maze.cpp │ Status.h │ struct SqStack.h │ └─链队列 Basic_called_functions.cpp Basic_called_functions.h Basic_operation_functions.cpp main.cpp QElemType.h Status.h Struct_LinkQueue.h
目 录 摘 要 1 前 言 2 基本算法实现 3 采用类C语言定义相关的数据类型 3 各模块的伪码算法 4 函数的调用关系图 6 调试分析 7 测试结果 8 源程序(注释) 11 总 结 22 参考文献 23 致 谢 24 附件Ⅰ 部分源程序代码 25 摘 要 该设计要求对飞机航班信息进行排序和查找。可按航班的班号、起点站、到达站、起飞时间以及到达时间等信息时行查询。对于本设计,主要是通过线性表的逻辑结构、存储结构,线性表及队列上基本运算实现的。可以让我们学会如何把学到的知识用于解决实际问题。 关键词: 航班信息;客户信息;链式存储结构; 前 言 飞机在现今的社会中已是越来越重要了,人们在旅游、工作、学习等生活中对飞机几乎是不可缺少了。而由乘坐飞机所引起的问题也深入到了计算机领域,飞机订票系统就是解决这种问题的一种途径。如人们常常在订票前要对问很多信息(飞机的起落时间,机票的价格,乘坐的人数以及是否有票等等)。这个系统主要是由线性表的链式存储结构以及队列的存储结构来存储航班信息与客户信息的。它的主要功能:1、是录入航班信息,其调用函数为—insert_airline;2、查询航线(航班号,飞机号,日期,航班容量,余票数),其调用函数为:search_air;3、订票(根据客户提供的情况,办理订票手续),其调用函数为:book_air;4、承办退票业务(根据客户提供的情况,办理退票手续),其调用函数为:del_cus;5、删除已有航班信息 ,其调用函数为:del_airline . 设计这样的一个系统可以很快的查出人们所需要的信息,能省去很多的步骤,而且还非常的准确。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值