标签:style blog color io os ar for div sp
#include<iostream> using namespace std; struct Queue { int maxCnt; int* elements; int front,rear; }; Queue* createQueue(int max=7) { Queue* queue = (Queue*)malloc(sizeof(Queue)); queue->front=0; queue->rear=0; queue->maxCnt=max+1;//预留一个空间来预防假溢出的情况 queue->elements = new int[ queue->maxCnt]; return queue; } bool isFull(Queue* queue) { return ((queue->rear+1)%queue->maxCnt)==queue->front; } bool isEmpty(Queue* queue) { return queue->rear==queue->front; } bool InQueue(Queue* queue,int value) { if(isFull(queue)) return 0; queue->elements[queue->rear++]=value; queue->rear%=queue->maxCnt; return 1; } bool DeQueue(Queue* queue,int* ans) { if(isEmpty(queue)) return 0; *ans = queue->elements[queue->front++]; queue->front%=queue->maxCnt; return 1; } bool front(Queue* queue,int* ans) { if(isEmpty(queue)) return 0; *ans = queue->elements[queue->front]; return 1; } void clearQueue(Queue* queue) { int x; while(!isEmpty(queue)) { DeQueue(queue,&x); cout<<x<<" "; } cout<<endl; } void outPut(Queue* queue) { int st = queue->front; int ed = queue->rear; while(st!=ed) { cout<<queue->elements[st]<<" "; st++; st%=queue->maxCnt; } cout<<endl; } void main() { int len=4; Queue* queue = createQueue(); int v; for(int i=0;i<len;i++) { v = rand() % 100; cout<<v<<" "; InQueue(queue,v); } cout<<endl; outPut(queue); clearQueue(queue); for(int i=0;i<5;i++) { v = rand() % 100; cout<<v<<" "; InQueue(queue,v); } cout<<endl; outPut(queue); DeQueue(queue,&v); cout<<v<<endl; outPut(queue); front(queue,&v); cout<<v<<endl; outPut(queue); DeQueue(queue,&v); cout<<v<<endl; outPut(queue); DeQueue(queue,&v); cout<<v<<endl; outPut(queue); for(int i=0;i<5;i++) { v = rand() % 100; cout<<v<<" "; InQueue(queue,v); } cout<<endl; outPut(queue); if(!InQueue(queue,v)) cout<<"fail"<<endl; else outPut(queue); DeQueue(queue,&v); cout<<v<<endl; outPut(queue); DeQueue(queue,&v); cout<<v<<endl; outPut(queue); DeQueue(queue,&v); cout<<v<<endl; outPut(queue); DeQueue(queue,&v); cout<<v<<endl; outPut(queue); for(int i=0;i<3;i++) { v = rand() % 100; cout<<v<<" "; InQueue(queue,v); } cout<<endl; outPut(queue); cin>>len; }
标签:style blog color io os ar for div sp
原文地址:http://www.cnblogs.com/kbyd/p/3991674.html