数据结构大合集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;
}
相关推荐
我想进大厂6 分钟前
图论---朴素Prim(稠密图)
数据结构·c++·算法·图论
我想进大厂11 分钟前
图论---Bellman-Ford算法
数据结构·c++·算法·图论
AIGC大时代13 分钟前
高效使用DeepSeek对“情境+ 对象 +问题“型课题进行开题!
数据库·人工智能·算法·aigc·智能写作·deepseek
lkbhua莱克瓦2421 分钟前
用C语言实现——一个中缀表达式的计算器。支持用户输入和动画演示过程。
c语言·开发语言·数据结构·链表·学习方法·交友·计算器
CODE_RabbitV1 小时前
【深度强化学习 DRL 快速实践】近端策略优化 (PPO)
算法
Wendy_robot2 小时前
【滑动窗口+哈希表/数组记录】Leetcode 438. 找到字符串中所有字母异位词
c++·算法·leetcode
程序员-King.2 小时前
day49—双指针+贪心—验证回文串(LeetCode-680)
算法·leetcode·贪心算法·双指针
转基因2 小时前
Codeforces Round 1020 (Div. 3)(题解ABCDEF)
数据结构·c++·算法
Forworder3 小时前
[数据结构]树和二叉树
java·数据结构·intellij-idea·idea
我想进大厂3 小时前
图论---Kruskal(稀疏图)
数据结构·c++·算法·图论