数据结构大合集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;
}
相关推荐
夏末秋也凉35 分钟前
力扣-回溯-491 非递减子序列
数据结构·算法·leetcode
penguin_bark37 分钟前
三、动规_子数组系列
算法·leetcode
kyle~1 小时前
thread---基本使用和常见错误
开发语言·c++·算法
曲奇是块小饼干_1 小时前
leetcode刷题记录(一百零八)——322. 零钱兑换
java·算法·leetcode·职场和发展
小wanga2 小时前
【leetcode】滑动窗口
算法·leetcode·职场和发展
少年芒2 小时前
Leetcode 490 迷宫
android·算法·leetcode
BingLin-Liu2 小时前
蓝桥杯备考:搜索算法之枚举子集
算法·蓝桥杯·深度优先
码农诗人2 小时前
调用openssl实现加解密算法
算法·openssl·ecdh算法
老菜鸡mou2 小时前
[OD E 100] 生成哈夫曼树
数据结构·c++
IT猿手2 小时前
2025最新智能优化算法:鲸鱼迁徙算法(Whale Migration Algorithm,WMA)求解23个经典函数测试集,MATLAB
android·数据库·人工智能·算法·机器学习·matlab·无人机