Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23436
  • 博文数量: 8
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 105
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-07 16:18
文章分类

全部博文(8)

文章存档

2008年(8)

我的朋友
最近访客

分类: C/C++

2008-07-22 11:27:48

用数组实现的环状队列。

#ifndef __QUEUE_H__
#define __QUEUE_H__

template <class T>
class Queue
{
public:
    Queue(int MaxQueueSize=10);
    ~Queue()
    {
        delete [] queue;
    }

    bool IsEmpty() const
    {
        return front == rear;
    }

    bool IsFull()
    {
        return ((rear+1) % MaxSize == front ? true : false);
    }

    T First() const;
    T Last() const;

    Queue<T>& Add(const T&x);
    Queue<T>& Delete(T& x);
private:
    int front;
    int rear;
    int MaxSize;
    T *queue;
};

template <class T>
Queue<T>::Queue(int MaxQueueSize)
{
    MaxSize = MaxQueueSize + 1;
    queue = new T[MaxSize];
    front = rear = 0;
}

template <class T>
T Queue<T>::First() const
{
    if (IsEmpty())
        throw;
    return queue[(front+1) % MaxSize];
}

template <class T>
T Queue<T>::Last() const
{
    if (IsEmpty())
        throw;
    return queue[rear];
}

template <class T>

Queue<T>& Queue<T>::Add(const T& x)
{
    if (IsFull())
        throw;
    rear = (rear+1) % MaxSize;
    queue[rear] = x;
    return *this;
}

template<class T>
Queue<T>& Queue<T>::Delete(T& x)
{
    if (IsEmpty())
        throw;
    front = (front + 1) % MaxSize;
    x = queue[front];
    return *this;
}

#endif

阅读(613) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~