一端插入一端删除的线性表,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; }
链式队列:
操作比较简单,同单链表。
原文:https://www.cnblogs.com/porest/p/14139233.html