首页 > 代码库 > 循环队列
循环队列
#include "stdafx.h"#include <iostream>using namespace std;typedef int DataType;#define MAXSIZE 5typedef struct{ DataType *base; int front; int rear;}CircleQueue;CircleQueue *init(CircleQueue *queue){ queue->base = (DataType*)malloc(MAXSIZE*sizeof(DataType)); if(!queue) exit(0); queue->rear = queue->front =0; return queue;}int getLength(CircleQueue *queue){ return (queue->rear - queue->front +MAXSIZE)%MAXSIZE;}void inQueue(CircleQueue *queue,DataType e){ if((queue->rear+1)%MAXSIZE ==queue->front) { cout<<"the queue is full"<<endl; exit(0); }else { queue->base[queue->rear] = e; queue->rear = (queue->rear+1)%MAXSIZE; }}void outQueue(CircleQueue *queue){ if(queue->front == queue->rear) { cout<<"the queue is empty"<<endl; exit(0); }else { cout<<"the out element is :"<<queue->base[queue->front]; queue->front= (queue->front+1)%MAXSIZE; }}void main(){ CircleQueue Q; Q = *init(&Q); cout<<"after init queue length:"<<getLength(&Q)<<endl; inQueue(&Q,1); cout<<"after input queue length:"<<getLength(&Q)<<endl; inQueue(&Q,2); cout<<"after input queue length:"<<getLength(&Q)<<endl; inQueue(&Q,3); cout<<"after input queue length:"<<getLength(&Q)<<endl; inQueue(&Q,4); cout<<"after input queue length:"<<getLength(&Q)<<endl; outQueue(&Q); cout<<"after output queue length:"<<getLength(&Q)<<endl; inQueue(&Q,5); cout<<"after input queue length:"<<getLength(&Q)<<endl; inQueue(&Q,6); cout<<"after input queue length:"<<getLength(&Q)<<endl; }
循环队列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。