目录

一、需要实现的功能

二、具体功能的实现

1.初始化队列

2.摧毁我们的队列

3.入队操作

4.出队操作

5.判断队列是否为空

6.查看我们队列元素的个数

7.输出队首和队尾元素

8.汇总


一、需要实现的功能

以下是我们队列需要实现的功能

#pragma once

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

typedef int QDataType;

//定义我们队列的结点
typedef struct QueueNode
{
    QDataType data;
    struct QueueNode* next;
}QNode;

//定义一个队列类型,并且给我们的队列定义头指针和尾指针
typedef struct Queue
{
    QNode* head;
    QNode* tail;

}Queue;

//队列初始化
void QueueInit(Queue* pq);
//摧毁队列
void QueueDestory(Queue* pq);
//元素入队
void QueuePush(Queue* pq, QDataType x);
//元素出队
void QueuePop(Queue* pq);
//判断当前队列是否为空
bool QueueEmpty(Queue* pq);
//判断队列的大小
size_t QueueSize(Queue* pq);
//获取队首元素
QDataType QueueFront(Queue* pq);
//获取队尾元素
QDataType QueueBack(Queue* pq);

二、具体功能的实现

1.初始化队列

将我们的队首指针和队尾指针全部都置空

void QueueInit(Queue* pq)
{
    assert(pq);
    pq->head = pq->tail = NULL;
}

2.摧毁我们的队列

void QueueDestory(Queue* pq)
{
    assert(pq);
//创建一个临时指针指向我们队伍的头
    QNode* cur = pq->head;
    while (cur)
    {
//用next指针记录下我们临时指针的下一个结点
        QNode* next = cur->next;
//将我们的临时指针置空
        free(cur);
//将我们的cur指针指向下一个需要释放空间的位置,实现迭代。
        cur = next;
    }

    pq->head = pq->tail = NULL;
}

3.入队操作

void QueuePush(Queue* pq, QDataType x)
{
    assert(pq);
//开辟一片的新的空间作为新的结点
    QNode* newnode = (QNode*)malloc(sizeof(QNode));
    assert(newnode);
//将我们的新的结点的数据传入
    newnode->data = x;
//将我们新的结点的next指针置空
    newnode->next = NULL;
//队列都是从队尾入队的,我们需要从队尾尾插我们的结点。
    if (pq->tail == NULL)
    {
        assert(pq->head == NULL);
        pq->head = pq->tail = newnode;
    }
    else
    {
        pq->tail->next = newnode;
        pq->tail = newnode;
    }
}

4.出队操作

void QueuePop(Queue* pq)
{
    assert(pq);
    assert(pq->head && pq->tail);
//队列的出队操作是在队首,同时将我们的队首的结点空间释放
    if (pq->head->next == NULL)
    {
        free(pq->head);
        pq->head = pq->tail = NULL;
    }
    else
    {
        QNode* next = pq->head->next;
        free(pq->head);
        pq->head = next;
    }
}

5.判断队列是否为空

bool QueueEmpty(Queue* pq)
{
    assert(pq);

//判断时否为空,只需要判断我们的头指针是否为NULL就可以了
    return pq->head == NULL;
}

6.查看我们队列元素的个数

size_t QueueSize(Queue* pq)
{
    assert(pq);
//创建一个临时结点指向我们的头结点,然后遍历完整张队列,就能够得到我们队列的长度
    QNode* cur = pq->head;
    size_t size = 0;
    while (cur)
    {
        size++;
        cur = cur->next;
    }

    return size;
}

7.输出队首和队尾元素

//因为我们有队首指针和队尾指针,所以直接返回队首指针和队尾指针指向的结点的数据
QDataType QueueFront(Queue* pq)
{
    assert(pq);
    assert(pq->head);

    return pq->head->data;
}

QDataType QueueBack(Queue* pq)
{
    assert(pq);
    assert(pq->tail);

    return pq->tail->data;
}

8.汇总

以下是写在queue.c中的内容。


#include "queue.h"

void QueueInit(Queue* pq)
{
    assert(pq);
    pq->head = pq->tail = NULL;
}

void QueueDestory(Queue* pq)
{
    assert(pq);
    QNode* cur = pq->head;
    while (cur)
    {
        QNode* next = cur->next;
        free(cur);
        cur = next;
    }

    pq->head = pq->tail = NULL;
}

void QueuePush(Queue* pq, QDataType x)
{
    assert(pq);
    QNode* newnode = (QNode*)malloc(sizeof(QNode));
    assert(newnode);

    newnode->data = x;
    newnode->next = NULL;

    if (pq->tail == NULL)
    {
        assert(pq->head == NULL);
        pq->head = pq->tail = newnode;
    }
    else
    {
        pq->tail->next = newnode;
        pq->tail = newnode;
    }
}

void QueuePop(Queue* pq)
{
    assert(pq);
    assert(pq->head && pq->tail);

    if (pq->head->next == NULL)
    {
        free(pq->head);
        pq->head = pq->tail = NULL;
    }
    else
    {
        QNode* next = pq->head->next;
        free(pq->head);
        pq->head = next;
    }
}

bool QueueEmpty(Queue* pq)
{
    assert(pq);

    //return pq->head == NULL && pq->tail == NULL;
    return pq->head == NULL;
}

size_t QueueSize(Queue* pq)
{
    assert(pq);
    QNode* cur = pq->head;
    size_t size = 0;
    while (cur)
    {
        size++;
        cur = cur->next;
    }

    return size;
}

QDataType QueueFront(Queue* pq)
{
    assert(pq);
    assert(pq->head);

    return pq->head->data;
}

QDataType QueueBack(Queue* pq)
{
    assert(pq);
    assert(pq->tail);

    return pq->tail->data;
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐