数据结构大合集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;
}
相关推荐
alphaTao2 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
Interview Aid1122 小时前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
顶点多余11 小时前
那些在算法中适合巩固的知识点---1
java·前端·算法
AI情绪识别开源12 小时前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
罗西的思考13 小时前
【Agentic RL / 强化学习框架】Molt 设计解读
人工智能·算法·机器学习
hahaha601613 小时前
HLS高层次综合设计技巧--C++类和模板
图像处理·人工智能·算法·计算机视觉
多弗朗皮卡丘14 小时前
算法详解4:买卖股票的最佳时机系列(上)
算法
维克兜率天17 小时前
【维克】动量指标家族:RSI、ROC、CCI、Momentum全面解析
python·算法
AI情绪识别开源17 小时前
检信 AI 智能推广平台(代号:JX-Promote)
人工智能·算法·erlang
老当益壮梁奶奶18 小时前
Linux软件编程学习笔记(八):进程间通信详解(1)
linux·c语言·笔记·学习·算法