数据结构——循环队列和链队列(C语言版)

循环队列和链队列的基本操作

循环队列

需要注意几个问题

  • 判断队列满(Q.rear + 1) % MaxSize == Q.front和队列为空Q.front == Q.rear
  • rear和front的指向
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100
typedef struct SeQueue
{
    int *base;
    int front, rear;
} SeQueue;
// 初始化一个顺序链表
void InitQueue(SeQueue &Q)
{
    Q.base = (int *)malloc(MaxSize * sizeof(int));
    Q.front = Q.rear = 0;
}
// 返回顺序链表的长度
int length(SeQueue Q)
{
    return (Q.rear - Q.front + MaxSize) % MaxSize;
}
// 队列插入元素
void EnQueue(SeQueue &Q, int elem)
{
    // 判断队列是否满
    if ((Q.rear + 1) % MaxSize == Q.front)
    {
        printf("队列已满\n");
        return;
    }
    // 这里设置rear初始化为插入元素的位置,所以先插入再+1
    Q.base[Q.rear] = elem;
    Q.rear = (Q.rear + 1) % MaxSize;
}
// 队列元素出队
void DeQueue(SeQueue &Q, int &elem)
{
    // 判断队列是否为空
    if (Q.front == Q.rear)
    {
        printf("队列为空\n");
        return;
    }
    elem = Q.base[Q.front];
    Q.front = (Q.front + 1) % MaxSize;
}
int main()
{
    SeQueue queue;
    InitQueue(queue);
    //插入数据 1->2->3->4->5
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    for (int i = 0; i < 5; i++)
    {
        EnQueue(queue, arr[i]);
    }
    //出队元素
    // 4->5
    for (int i = 0; i < 2; i++)
    {
        int x;
        DeQueue(queue, x);
        printf("出队元素:%d\n", x);
    }
    //入队数据
    // 4->5->6->7->8->9->10
    for (int i = 5; i < 10; i++)
    {
        EnQueue(queue, arr[i]);
    }
    //出队元素
    // 4->5
    for (int i = 0; i < 8; i++)
    {
        int x;
        DeQueue(queue, x);
        printf("出队元素:%d\n", x);
    }
}

链队列

需要注意的几个问题

  • 带头结点和不带头结点(我的是带头节点)
  • LinkedQueue的含义,注意对比链表
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkNode
{
    int data;
    LinkNode *next;
} LinkNode;
typedef struct LinkedQueue
{
    LinkNode *front;
    LinkNode *rear;
    int size;
} LinkedQueue;
//初始化链队列
void InitQueue(LinkedQueue &Q)
{
    Q.rear = Q.front = (LinkNode *)malloc(sizeof(LinkNode));
    Q.rear->next = nullptr;
}
//销毁链队列
void DestroyQueue(LinkedQueue &Q)
{
    while (Q.front)
    {
        Q.rear = Q.front->next;
        free(Q.front);
        Q.front = Q.rear;
    }
}
// 入队操作
void EnQueue(LinkedQueue &Q, int elem)
{
    LinkNode *node = (LinkNode *)malloc(sizeof(LinkNode));
    node->data = elem;
    node->next = nullptr;
    Q.rear->next = node;
    Q.rear = node;
}
// 出队操作
void DeQueue(LinkedQueue &Q, int &x)
{
    LinkNode *node = (LinkNode *)malloc(sizeof(LinkNode));
    node = Q.front->next;
    Q.front->next = node->next;
    if (node == Q.rear)
    {
        Q.front = Q.rear;
    }
    x = node->data;
    free(node);
}
int main()
{
    LinkedQueue queue;
    InitQueue(queue);
    //插入数据 1->2->3->4->5
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    for (int i = 0; i < 5; i++)
    {
        EnQueue(queue, arr[i]);
    }
    //出队元素
    // 4->5
    for (int i = 0; i < 2; i++)
    {
        int x;
        DeQueue(queue, x);
        printf("出队元素:%d\n", x);
    }
    //入队数据
    // 4->5->6->7->8->9->10
    for (int i = 5; i < 10; i++)
    {
        EnQueue(queue, arr[i]);
    }
    //出队元素
    // 4->5
    for (int i = 0; i < 8; i++)
    {
        int x;
        DeQueue(queue, x);
        printf("出队元素:%d\n", x);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值