循环队列
Posted linkmust
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了循环队列相关的知识,希望对你有一定的参考价值。
#include "stdafx.h" #include<iostream> using namespace std; #define MAXQSIZE 100 typedef int QElemType; typedef enum Status { success, fail, fatal, rangeerror, overflow }Status; typedef struct { QElemType *base;//初始化动态分配存储空间 int front; int rear; }SqQueue; Status InitQueue(SqQueue &q) { q.base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType)); if (q.base == NULL) exit(OVERFLOW); q.rear = q.front = 0; return success; } int QueueLength(SqQueue q) { return (q.rear - q.front+ MAXQSIZE)% MAXQSIZE; } Status EnQueue(SqQueue &q, QElemType elem) { if ((q.rear + 1) % MAXQSIZE == q.front) return overflow; q.base[q.rear] = elem; q.rear = (q.rear + 1) % MAXQSIZE; return success; } Status DeQueue(SqQueue &q, QElemType &elem) { if (q.rear == q.front) return fail; elem = q.base[q.front]; q.front = (q.front + 1) % MAXQSIZE; return success; } void PrintQueue(SqQueue q) { for(int i=q.front;i<q.rear;i++) { cout << q.base[i] << " "; } cout << endl; } int main() { SqQueue q; InitQueue(q); int arr[] = { 2,5,3,7 }; int len = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < len; i++) { EnQueue(q, arr[i]); } PrintQueue(q); QElemType elem; DeQueue(q, elem); cout << "删除的元素为:" << elem <<endl; system("pause"); return 0; }
以上是关于循环队列的主要内容,如果未能解决你的问题,请参考以下文章