- 数据类型:队列(也是属于线性表)
- 存储方式:顺序循环存储
- 常用名称:顺序循环队列
(注:图片截取自《数据结构与算法》-青岛大学王卓bilibili视频)
#includeusing namespace std; #define MAX_SIZE 100 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; // Status是函数的类型,其值是函数结果状态代码 typedef int QElemType; // 根据自己的数据类型进行定义 typedef struct { QElemType* base; // 初始化的动态分配内存空间 int front; // 头指针,指向队头下标 int rear; // 尾指针,指向队尾下标 }SqQueue; // 初始化 Status InitQueue(SqQueue& Q) { Q.base = new QElemType[MAX_SIZE]; // 分配内存 if (!Q.base) exit(OVERFLOW); Q.front = Q.rear = 0; return OK; } // 队列长度 int QueueLength(SqQueue Q) { return (Q.rear - Q.front + MAX_SIZE) % MAX_SIZE; } // 循环入队 Status EnQueue(SqQueue& Q, QElemType e) { if ((Q.rear + 1) % MAX_SIZE == Q.front) return ERROR; // 判断是否满了;判断方法,少用一个元素空间。 Q.base[Q.rear] = e; // 插入 Q.rear = (Q.rear + 1) % MAX_SIZE; // 移动队尾指针 取模运算 return OK; } // 出队 Status DeQueue(SqQueue& Q, QElemType& e) { if (Q.front == Q.rear) return ERROR; // 队列为空 e = Q.base[Q.front]; Q.front = (Q.front + 1) % MAX_SIZE; // 移动队头指针 return OK; } // 取队头元素 QElemType GetHead(SqQueue Q) { if (Q.front != Q.rear) return Q.base[Q.front]; } int main() { SqQueue q; InitQueue(q); EnQueue(q, 20); EnQueue(q, 80); EnQueue(q, -120); EnQueue(q, 50); int e; int len = QueueLength(q); for (size_t i = 0; i < len; i++) { DeQueue(q, e); cout << e << endl; } return 0; }



