数据结构— 数组循环队列C++

来源:互联网 发布:电脑程序编程入门 编辑:程序博客网 时间:2024/06/07 16:04
#ifndef STL_STACK_SDJFKLSD#define STL_STACK_SDJFKLSD/************************************************************************//* 数组循环队列C++------空间利用率较高的/************************************************************************/template<class Elemplent>class CircleStack{protected:int front,rear;Elemplent * elemPtr;int maxTotal;int CountTotal;public:CircleStack(int size = 1000);~CircleStack();void InitCircleStack(int size );CircleStack(const  CircleStack<Elemplent>& tempCircle);CircleStack<Elemplent>& operator= (const const  CircleStack<Elemplent>& tempCircle);public:int Getlength();bool IsEmtpy();bool IsFull();void Clear();bool OutQueue();bool GetHead(Elemplent & tempElem)const;bool InQueue(const Elemplent& temElem);};template<class Elemplent>CircleStack<Elemplent>::CircleStack(int size){InitCircleStack(size);};template<class Elemplent>CircleStack<Elemplent>::~CircleStack(){};template<class Elemplent>void CircleStack<Elemplent>::InitCircleStack(int size){front = rear  = 0;elemPtr = new Elemplent[maxTotal];maxTotal =1000;}template<class Elemplent>int CircleStack<Elemplent>::Getlength(){return CountTotal;}template<class Elemplent>bool CircleStack<Elemplent>::IsEmtpy(){if ((front+1)%maxTotal == rear){return return}return false;}template<class Elemplent>bool CircleStack<Elemplent>::IsFull(){if ((rear+1)%maxTotal == front){return true;}return false;}template<class Elemplent>bool CircleStack<Elemplent>::GetHead(Elemplent & tempElem){if (IsEmtpy()){return false;}tempElem = tempElem[front];return true;}template<class Elemplent>bool CircleStack<Elemplent>::InQueue(const Elemplent& temElem){if (IsFull()){return false;}elemPtr[rear]=e;rear = (rear+1)%maxTotal;CountTotal++;return true;}template <class Elemplent>bool CircleStack<Elemplent>::OutQueue(){if (IsEmtpy()){return false;}front = (front+1)% maxTotal;CountTotal--;return true;}template <class Elemplent>void CircleStack<Elemplent>::Clear(){front = rear = 0;}template<class Elemplent>CircleStack<Elemplent>::CircleStack(const CircleStack<Elemplent>& tempCircle){elemPtr = NULL;front = tempCircle.front;rear = tempCircle.rear;//上面的三个给忘了初始化!int length = tempCircle.Getlength();InitCircleStack(tempCircle.maxTotal);for (int i = 0;i<length;i++){elemPtr[i]=tempCircle.elemPtr[i];}return *this;}template<class Elemplent>CircleStack<Elemplent>& CircleStack<Elemplent>::operator=(const CircleStack<Elemplent>& tempCircle){if (this == &tempCircle){return *this;}elemPtr = NULL;front = tempCircle.front;rear = tempCircle.rear;//上面的三个给忘了初始化!int length = tempCircle.Getlength();InitCircleStack(tempCircle.maxTotal);for (int i = 0;i<length;i++){elemPtr[i]=tempCircle.elemPtr[i];}return *this;}/************************************************************************//* 刚刚看了一个mi2的视频,心里很是感慨,一个十三岁的女孩,/*那么单纯,那么纯洁,哎!好像自己有一个miki/*想想她以后要面对社会的摧残,时间的毁灭性的打击,心里不住一阵心痛,/*生老病死,,,难道这就是人的一生,难道一个生命的这就这样悄无声息的来了一次地球?/*三个字:我脆了。。。/************************************************************************/#endif

原创粉丝点击