首页 > 其他 > 详细

队列(queue)

时间:2020-12-15 20:02:21      阅读:67      评论:0      收藏:0      [点我收藏+]

一端插入一端删除的线性表,FIFO。

 

循环队列:

为保证队列不出现伪溢出,一般将其构造为循环队列。

为方便判断队列是否已满,将队列满定义为还剩一个数组空间。

队列满的条件:(rear+1)%QueueSize == front  (rear队尾, front队头)

队列长度计算公式:(rear-front+QueueSize) % QueueSize

指针后移:(front+1)%MAXSIZE。同时也实现了循环!

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define MAXSIZE 1000
#pragma warning (disable:4996)

typedef int QElemType;

typedef struct {
    QElemType data[MAXSIZE];
    int front;
    int rear;
}SqQueue;

bool initQueue(SqQueue* Q) {
    Q->front = Q->rear = 0;
    return 1;
}

bool insertElem(SqQueue* Q, QElemType e) {
    if ((Q->rear + 1) % MAXSIZE == Q->front) {
        return 0;
    }
    Q->data[Q->rear] = e;
    Q->rear = (Q->rear + 1) % MAXSIZE;//循环队列里的指针后移!
    return 1;
}

bool deleteData(SqQueue* Q, QElemType* e) {
    if (Q->front == Q->rear) {
        return 0;
    }
    *e = Q->data[Q->front];
    Q->front = (Q->front + 1) % MAXSIZE;
    return 1;
}

 

链式队列:
操作比较简单,同单链表。

队列(queue)

原文:https://www.cnblogs.com/porest/p/14139233.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!