zoukankan      html  css  js  c++  java
  • 循环队列

    #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;
    }
  • 相关阅读:
    读Android之大话设计模式--前言和说明
    把二叉树打印成多行
    按之字形顺序打印二叉树
    对称的二叉树
    二叉树的下一个结点
    链表中环的入口结点
    字符流中第一个不重复的字符
    表示数值的字符串
    构建乘积数组
    数组中重复的数字
  • 原文地址:https://www.cnblogs.com/linkmust/p/10926056.html
Copyright © 2011-2022 走看看