zoukankan      html  css  js  c++  java
  • 【动手敲代码】:循环队列(C++)

    
    
    #include<iostream>
     template <class T>
     class Queue
     {
     private:
         T rear;
         T front;
         int maxSize;
         T* head;
     public:
         Queue(int size):rear(0),front(0),maxSize(size)
         {
             head = new T(size);
             if(!head)
                 std::cout << "memoryAllocationError!" << std::endl;
         }
         ~Queue()
         {
             maxSize = 0;
             delete[] head;
         }
         bool IsEmpty()
         {
             return front == rear;
         }
         bool IsFull()
         {
             return rear - front == maxSize;
         }
         void inQueue(T item)
         {
             if(IsFull())
                 std::cout << "Queue is full!" << std::endl;
             *(head + rear) = item;
             rear ++;
         }
         T outQueue()
         {
             if(IsEmpty())
                 std::cout << "Queue is empty!" << std::endl;
             T item = *(head + front);
             front ++;
             return item;
         }
         void Destroy()
         {
             ~Queue();
         }
         
     };
    
    
    

      

     
  • 相关阅读:
    concurrent-锁
    字符串查找字符串
    指针作为函数返回值
    数组名作为函数参数
    指针和函数
    多级指针
    指针数组
    指针运算
    指针和数组
    const修饰的指针类型
  • 原文地址:https://www.cnblogs.com/VortexPiggy/p/2472125.html
Copyright © 2011-2022 走看看