数据结构--C语言实现循环队列

C语言——循环队列的实现

循环队列

循环队列可以理解为一个环,实际上是将一个普通队列的尾部连上头部构成的,所以在实现循环队列时,需要两个指针front和rear分别指示队列头元素及队列尾元素的位置。

在初始化建空队列时可以令front=rear=0,这样当插入新元素时,尾指针rear加1表示向后移动一位,同理,当删除队列头元素时,头指针front加1表示队列头部一个元素出队列,下一个元素成队首。这样,front始终指向队头元素,rear始终指向队尾元素的下一位。

假设一个空队列空间为10,如果存入10个数据,那么此时队列就会满,但此时会发现front=rear,因为rear指向队尾元素的下一位,此时队尾元素的下一位恰好为队首元素。此时会出现一个问题,刚开始是说当front==rear时,队列为空队列,队列满时也满足front=rear。

此时可以采用一个方法:少用一个元素空间,约定以“队列首指针在队列尾指针的下一位置上”作为队列呈“满”状态的标志。

代码实现如下

#include "stdio.h"
#include "stdlib.h"

#define MAXQSIZE 100   //最大队列长度
typedef int QElemType;

typedef struct{
   QElemType *base;
    int front;   //头指针
    int rear;    //尾指针
}SqQueue;

int InitQueue(SqQueue *q){
    q->base=(QElemType *)malloc(MAXQSIZE * sizeof(QElemType));
    if(!q->base)
        exit(0);   //分配失败
    q->front=0;
    q->rear=0;
    return 1;
}

//返回队列q的元素个数
int QueueLength(SqQueue q){    
    return (q.rear-q.front+MAXQSIZE)%MAXQSIZE;
}

//向q队尾插入元素
int EnQueue(SqQueue *q,QElemType e){
    if((q->rear+1)%MAXQSIZE==q->front)  //队列满
        return 0;
    q->base[q->rear]=e;
    q->rear=(q->rear+1)%MAXQSIZE;
    return 1;
}

//删除q的队首元素,用e返回其值
int DeQueue(SqQueue *q,QElemType *e){
    if(q->front==q->rear)   //队列为空
        return 0;
    *e=q->base[q->front];
    q->front=(q->front+1)%MAXQSIZE;
    return 1;
}

int main(){
    SqQueue q;
    int e=0;
    InitQueue(&q);
    EnQueue(&q,2);
    printf("长度:%d\n",QueueLength(q));
    EnQueue(&q,5);
    printf("长度:%d\n",QueueLength(q));
    DeQueue(&q,&e);
    printf("长度:%d\n",QueueLength(q));
    printf("e= %d\n",e);
    DeQueue(&q,&e);
    printf("长度:%d\n",QueueLength(q));
    printf("e= %d\n",e);

    return 0;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言实现循环队列生成杨辉三角的代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 // 定义循环队列 typedef struct { int front; // 队首指针 int rear; // 队尾指针 int items[MAX_SIZE]; // 队列元素 } Queue; // 初始化队列 void init_queue(Queue *q) { q->front = q->rear = 0; } // 判断队列是否为空 int is_empty(Queue *q) { return q->front == q->rear; } // 判断队列是否已满 int is_full(Queue *q) { return (q->rear + 1) % MAX_SIZE == q->front; } // 元素入队 void enqueue(Queue *q, int item) { if (!is_full(q)) { q->items[q->rear] = item; q->rear = (q->rear + 1) % MAX_SIZE; } } // 元素出队 int dequeue(Queue *q) { if (!is_empty(q)) { int item = q->items[q->front]; q->front = (q->front + 1) % MAX_SIZE; return item; } return -1; } // 生成杨辉三角 void generate_yanghui_triangle(int n) { Queue q; init_queue(&q); enqueue(&q, 1); // 第一行只有一个数字1 for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { printf("%d ", q.items[j]); } printf("\n"); for (int j = 0; j <= i; j++) { enqueue(&q, dequeue(&q) + q.items[j]); } enqueue(&q, 1); // 行末添加数字1 } } int main() { generate_yanghui_triangle(5); // 生成5行杨辉三角 return 0; } ``` 代码中定义了一个循环队列结构体Queue,包含front、rear和items三个成员变量。init_queue函数用于初始化队列,is_empty和is_full函数用于判断队列是否为空和已满,enqueue和dequeue函数实现元素的入队和出队操作。generate_yanghui_triangle函数使用队列来生成杨辉三角,通过两个for循环遍历队列中的数字并输出,将队首出队并加上队列中的每个数字,将结果入队,最后再将数字1入队。重复这个过程,就可以生成指定行数的杨辉三角。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值