码迷,mamicode.com
首页 > 其他好文 > 详细

循环队列

时间:2019-05-26 15:47:52      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:stat   分配   amp   stream   name   oid   end   pre   sys   

#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;
}

 

循环队列

标签:stat   分配   amp   stream   name   oid   end   pre   sys   

原文地址:https://www.cnblogs.com/linkmust/p/10926056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!