标签:exit maxsize als put clu code enqueue cpp max
Queue.h
#pragma once #include<iostream> using namespace std; class Queue { public: int front; int rear; int maxSize; int* elements; Queue(int size=20){ maxSize = size; elements = new int[maxSize]; front = 0; rear = 0; } bool IsFull() { return (rear + 1) % maxSize == front; } bool IsEmpty() { return rear == front; } bool EnQueue(int elem) { bool res = true; if (IsFull() == true) { res = false; } else { elements[rear] = elem; rear = (rear + 1) % maxSize; } return res; } bool DeQueue(int& x) { bool res = true; if (IsEmpty() == true) { res = false; } else { x = elements[front]; front = (front + 1) % maxSize; } return res; } };
main.cpp
#include"Queue.h" int main() { Queue q; int i = 0; int temp; bool bl; while (q.EnQueue(i)) { i++; } cout << "Output 5 elements & input 5 elements!" << endl; cout << "There are the 5 elements output:" << endl; for (int index = 0; index < 5; index++) { if (q.DeQueue(temp)) { cout << temp << " "; q.EnQueue(i++); } else { cerr << "OutputError!!!" << endl; exit(1); } } cout << endl; cout << "The left-over elements output:" << endl; while (q.DeQueue(temp)) { cout << temp << " "; } cout << endl; return 0; }
标签:exit maxsize als put clu code enqueue cpp max
原文地址:https://www.cnblogs.com/SlowIsFast/p/12603368.html