栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

队列的实现(c语言数据结构)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

队列的实现(c语言数据结构)

一、需要实现的功能

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

#pragma once

#include 
#include 
#include 
#include 

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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/887055.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号