队列

#include <iostream>
using namespace std;
#define Maxsize 10

template <typename T>
class queue
{
      private:
              int front,rear;
              T elem[Maxsize];
      public:
             bool init();
             bool QueueEmpty();
             bool EnQueue(T e);
             T GetFront();
             T DeQueue();
};

template <typename T>
bool queue<T>::init()
{
     front = rear = -1;
     memset(elem, 0, sizeof(elem));
     return true;
}

template <typename T>
bool queue<T>::QueueEmpty()
{
     if (front == rear)
        return false;
     else
         return true;
}

template <typename T>
bool queue<T>::EnQueue(T e)
{
     if ((rear+1)%Maxsize == front)
        exit(1);
     rear = (rear+1)%Maxsize;
     elem[rear] = e;
     return true;
}

template <typename T>
T queue<T>::GetFront()
{
    if (!QueueEmpty())
        exit(-1);
    else
    {
        return elem[front];
    }
}

template <typename T>
T queue<T>::DeQueue()
{
   if (!QueueEmpty())
        exit(-1);
    else
    {
        front = (front+1)%Maxsize;
        return elem[front];
    }
}

int main(void)
{
    queue<int > qu;
    qu.init();
    qu.EnQueue(1);
    qu.EnQueue(2);
    cout<<qu.DeQueue()<<endl;
    cout<<qu.DeQueue()<<endl;
    return 0;
}

posted @ 2012-10-26 13:44  SA高处不胜寒  阅读(132)  评论(0编辑  收藏  举报