链式队列

以链表为基础实现链式队列


/********************************************************************************
 *
 *  file name:  Linked_Queues
 *  author   :  yxw18679428019@163.com
 *  date     :  2024/04/26
 *  function :  以链表为基础实现链式队列,一般把链表头部作为队头,可以实现头删,
 *              把链表尾部作为队尾,可以实现尾插。
 *
 *  note     :  None
 *  CopyRight  (c)  2023-2024   yxw18679428019@163.com    All  Right  Reseverd
 *   ****************************************************************************/

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

// 指的是顺序栈中的元素的数据类型,用户可以根据需要进行修改
typedef int DataType_t;

// 构造链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct Linked_Queues
{
    struct Linked_Queues *next; // 创建一个指针域
    DataType_t data;            // 创建数据域
} LinkQueu_t;

/*******************************************************************************
 *  name     :  LinkQueu_Create
 *  function :  创建一个空链式对列,空链表应该有一个头结点,对链表进行初始化
 *  argument :  None
 *
 *
 *  retval   :  返回管理顺序栈的地址
 *  author   :  yxw18679428019@163.com
 *  date     :  2024/04/26
 *  note     :  None
 *   ***************************************************************************/
LinkQueu_t *LinkQueu_Create(void)
{
    // 创建一个头结点并对头结点申请堆内存
    LinkQueu_t *Head = (LinkQueu_t *)calloc(1, sizeof(LinkQueu_t));
    // 判断申请的堆内存是否成功如果失败直接结束程序
    if (NULL == Head)
    {
        perror("calloc memroy for Head is failed \n");
        exit(-1);
    }

    // 2.对头结点进行初始化,头结点是不存储有效内容的!!!
    Head->next = NULL;

    // 返回头结点的地址
    return Head;
}

/*******************************************************************************
 *  name     :  LinkQueu_NewNode
 *  function :  创建一个新的结点并对数据域与指针域进行初始化
 *  argument :
 *              @Data :传入的数据值
 *
 *  retval   :  返回管理顺序栈的地址
 *  author   :  yxw18679428019@163.com
 *  date     :  2024/04/26
 *  note     :  None
 *   ***************************************************************************/

LinkQueu_t *LinkQueu_NewNode(DataType_t Data)
{
    // 创建一个新结点并申请堆内存
    LinkQueu_t *New = (LinkQueu_t *)calloc(1, sizeof(LinkQueu_t));
    // 判断堆内存是否申请成功如果申请失败则直接退出程序
    if (NULL == New)
    {
        perror("calloc memroy for NewNode is failed \n");
        exit(-1);
    }

    // 对新结点的数据域与指针域进行初始化
    New->next = NULL;
    New->data = Data;

    // 返回新结点的地址
    return New;
}

/*******************************************************************************
 *  name     :  LinkQueu_Front
 *  function :  链式队列的入列
 *  argument :
 *              @Head :头结点的地址
 *              @Data :传入的数据值
 *  retval   :  true and false
 *  author   :  yxw18679428019@163.com
 *  date     :  2024/04/26
 *  note     :  None
 *   ***************************************************************************/

bool LinkQueu_Rear(LinkQueu_t *Head, DataType_t Data)
{

    LinkQueu_t *Phead = Head; // 定义一个指针记录头结点
    // 创建一个新的结点,
    LinkQueu_t *New = LinkQueu_NewNode(Data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
        return false;
    }
    // 判断链式队列是否为空
    if (NULL == Head->next)
    {
        Head->next = New; // 如果为空则头结点的next指针指向新结点
        return true;
    }

    // 遍历到尾结点
    while (Phead->next)
    {
        Phead = Phead->next;
    }

    // 链式队列为非空
    Phead->next = New; // 尾结点的next指针指向新结点
    return true;
}

/*******************************************************************************
 *  name     :  LinkQueu_Rear
 *  function :  链式队列的出列
 *  argument :
 *              @Head :头结点的地址
 *
 *  retval   :  返回删除结点数据域的值
 *  author   :  yxw18679428019@163.com
 *  date     :  2024/04/26
 *  note     :  None
 *   ***************************************************************************/
DataType_t LinkQueu_Front(LinkQueu_t *Head)
{

    LinkQueu_t *Phead = Head->next; // 定义一个指针记录首结点
    DataType_t temp;                // 备份首结点数据域的值
    // 判断链式队列是否为空
    if (Head->next == NULL)
    {
        printf("Linked Queues is empty \n");
        return 0;
    }
    else
    {
        // 链式队列不为空
        if (Phead->next)
        {
            Head->next = Phead->next; // 头结点的next指针指向首结点的直接后继
            Phead->next = NULL;       //// 首结点的next指针指向NULL
            temp = Phead->data; 
            free(Phead);        // 释放首结点的内存
        }
        else
        {
            Head->next = NULL;
            free(Phead);
        }
    }
    // 返回删除结点的值
    return temp;
}

/*******************************************************************************
 *  name     :  LinkQueu_Rear
 *  function :  遍历结点,并输出结点的数据域的值
 *  argument :
 *              @Head :头结点的地址
 *
 *  retval   :  None
 *  author   :  yxw18679428019@163.com
 *  date     :  2024/04/26
 *  note     :  None
 *   ***************************************************************************/
void LinkQueu_Print(LinkQueu_t *Head)
{
    LinkQueu_t *Phead = Head; // 备份头结点
    while (Phead->next)
    {
        Phead = Phead->next;
        printf("%d\n", Phead->data);
    }
}

int main(void)
{
    LinkQueu_t *Head = LinkQueu_Create();
    LinkQueu_Rear(Head, 1);

    LinkQueu_Rear(Head, 2);
    LinkQueu_Rear(Head, 3);
    LinkQueu_Print(Head);

    LinkQueu_Front(Head);
    LinkQueu_Front(Head);
    LinkQueu_Front(Head);
    LinkQueu_Front(Head);

    LinkQueu_Rear(Head, 3);
    LinkQueu_Print(Head);

    return 0;
}
posted @   Yxw_lp  阅读(2)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示