数据结构大合集04——队的相关函数运算算法

函数运算算法合集04

注:

本篇文章的概念合集
数据结构的概念大合集04(队列)

顺序队的结构体

c 复制代码
//顺序队的结构体
typedef struct
{
    ElemType data[MaxSize];
    int front,rear;
} SqQueue;

顺序队的基本运算的实现

1. 初始化队列

c 复制代码
//初始化队列
void InitQueue(SqQueue *q)
{
    q = (SqQueue *)malloc(sizeof(SqQueue));
    q->front = q->rear = -1;
}

2. 销毁队列

c 复制代码
//销毁队列
void DestroyQueue(SqQueue *q)
{
    free(q);
}

3. 判断队列是否为空

c 复制代码
//判断队列是否为空
bool QueueEmpy(SqQueue *q)
{
    return (q->front == q->rear);
}

4. 进队

c 复制代码
//进队
bool enQueue(SqQueue *q,ElemType e)
{
    if(q->rear == MaxSize - 1)  return false;
    q->rear++;
    q->data[q->rear] = e;
    return true;
}

5. 出队

c 复制代码
//出队
bool deQueue(SqQueue *q,ElemType *e)
{
    if (q->front == q->rear) return false;
    q->front++;
    e = q->data[q->front];
    return true;
}

环形队列的结构体

环形队列的结构体采用的是和顺序队一样的结构体,因为环形队列是顺序队的衍生物

c 复制代码
//顺序队的结构体
typedef struct
{
    ElemType data[MaxSize];
    int front,rear;
} SqQueue;

环形队列的基本运算的实现

初始化,进队,出队与顺序队有所不同,读者可细心体会。

1.初始化队列

c 复制代码
//初始化队列
void InitQueue(SqQueue *q)
{
    q = (SqQueue *)malloc(sizeof(SqQueue));
    q->front = q->rear = 0;
}

2. 销毁队列

c 复制代码
//销毁队列
void DestroyQueue(SqQueue *q)
{
    free(q);
}

3. 判断队列是否为空

c 复制代码
//判断队列是否为空
bool QueueEmpy(SqQueue *q)
{
    return (q->front == q->rear);
}

4. 进队

c 复制代码
//进队
bool enQueue(SqQueue *q,ElemType e)
{
    if((q->rear + 1) % MaxSize == q->front)  return false;
    q->rear = (q->rear + 1) % MaxSize;
    q->data[q->rear] = e;
    return true;
}

5.出队

c 复制代码
//出队
bool deQueue(SqQueue *q,ElemType *e)
{
    if (q->front == q->rear) return false;
    q->front = (q->front + 1) % MaxSize;
    e = q->data[q->front];
    return true;
}

链队的结构体

数据结点

c 复制代码
//链队的结构体------数据结点
typedef struct qnode
{
    ElemType data;
    struct qnode *next;
} DataNode;

头结点

c 复制代码
//链队的结构体------头结点
typedef struct
{
    DataNode *front;
    DataNode *rear;
} LinkQuNode;

链队的基本运算的实现

1. 初始化队列

c 复制代码
//初始化队列
void InitQueue(LinkQuNode *q)
{
    q = (LinkQuNode *)malloc(sizeof(LinkQuNode));
    q->front = q->rear = NULL;
}

2. 销毁队列

c 复制代码
//销毁队列
void DestroyQueue(LinkQuNode *q)
{
    DataNode *pre = q->front, *p;
    if(pre != NULL)
    {
        p = pre->next;
        while(p != NULL)
        {
            free(pre);
            pre = p;
            p = p->next;
        }
        free(pre);
    }
    free(q);
}

3. 判断队列是否为空

c 复制代码
//判断队列是否为空
bool QueueEmpty(LinkQuNode *q)
{
    return(q->rear == NULL);
}

4. 进队

c 复制代码
//进队
bool enQueue(LinkQuNode *q,ElemType e)
{
    DataNode *p;
    p = (LinkQuNode *)malloc(sizeof(LinkQuNode));
    p->data = e;
    p->next = NULL;
    if(q->rear == NULL)
        q->front = q->rear = p;
    else
    {
        q->rear->next =p;
        q->rear = p;
    }
    return true;
}

5. 出队

c 复制代码
//出队
bool deQueue(LinkQuNode *q,ElemType e)
{
    DataNode *t;
    if(q->rear == NULL)
        return false;
    t = q->front;
    if(q->front == q->rear)
        q->front = q->rear = NULL;
    else
        q->front = q->front->next;
    e = t->data;
    free(t);
    return true;
}
相关推荐
意疏1 小时前
深入解析MySQL Join算法原理与性能优化实战指南
mysql·算法·性能优化
菜鸟小九1 小时前
Leetcode20 (有效的括号)
java·数据结构·算法
N_NAN_N2 小时前
[蓝桥杯 2024 国 Python B] 设计
java·数据结构·算法·并查集
量子-Alex2 小时前
【DETR目标检测】ISTD-DETR:一种基于DETR与超分辨率技术的红外小目标检测深度学习算法
深度学习·算法·目标检测
小猫咪怎么会有坏心思呢3 小时前
华为OD机试-猴子爬山-dp(JAVA 2025A卷)
java·算法·华为od
酷爱码3 小时前
Python虚拟环境与Conda的使用方式详解
开发语言·python·算法
-qOVOp-3 小时前
408第一季 - 数据结构 - 栈与队列
数据结构
Epiphany.5563 小时前
堆排序code
数据结构·c++·算法
秋山落叶万岭花开ღ3 小时前
树的基本概念与操作:构建数据结构的层级世界
数据结构·python·算法
追烽少年x4 小时前
数据结构---红黑树
数据结构