数据结构大合集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;
}
相关推荐
并不喜欢吃鱼4 分钟前
从零开始 C++----- 十三【C++ 数据结构】哈希表从原理到手撕实现(开放定址 + 链地址全覆盖)
数据结构·c++·散列表
_Oracle7 分钟前
机器学习——常见算法
人工智能·算法·机器学习
x_xbx9 分钟前
LeetCode:17. 电话号码的字母组合
算法·leetcode·职场和发展
山楂树の11 分钟前
广度优先搜索 (BFS)
算法·广度优先·宽度优先
say_fall14 分钟前
深入理解Linux内核进程调度:从基础概念到O(1)调度算法
linux·运维·服务器·算法·计算机组成
拂拉氏15 分钟前
【知识讲解-题目讲解】算法系列之动态规划入门(上)
算法·leetcode·动态规划
lDevinl20 分钟前
【无标题】
数据结构·c++·青少年编程
菜菜的顾清寒23 分钟前
力扣HOT100(46)将有序数组转换为二叉搜索树
算法·leetcode·职场和发展
随意起个昵称9 小时前
区间dp-基础题目1(石子合并)
算法·动态规划
吞下星星的少年·-·9 小时前
线段树模板
算法