随笔 - 8  文章 - 26  trackbacks - 0
<2024年7月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用链接

留言簿(4)

随笔档案

文章分类

文章档案

相册

C++语言

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 1//数据结构队列简单实现(循环队列)
 2#ifndef QUEUE_H
 3#define QUEUE_H
 4
 5template<class T>
 6class Queue
 7{
 8public:
 9    Queue(int maxsize=50); 
10    virtual ~Queue();
11    bool IsFull(){return (rear+1)%MaxSize==front?true:false;}//满?
12    bool IsEmpty(){return front==rear?true:false;};//空?
13    Queue<T>& Push(const T& val);//向队尾插入元素
14    Queue<T>& Pop(T& e);//从堆头删除元素
15    int Size(){return _Size;};//返回队列元素个数
16    T& Front();//返回对头元素
17    T& Back();//返回队尾元素
18    
19private:
20    T *data;
21    int front,rear;
22    int MaxSize;
23    int _Size;
24}
;
25//------------------------------------------------
26template<class T>
27Queue<T>::Queue(int maxsize)
28{
29    data=new T[maxsize+1];
30    MaxSize=maxsize+1;
31    front=rear=_Size=0;
32    
33}

34//------------------------------------------------
35template<class T>
36Queue<T>::~Queue()
37{
38    delete[] data;
39}

40//------------------------------------------------
41template<class T>
42Queue<T>& Queue<T>::Push(const T& val)
43{
44    if(IsFull()) throw exception("队列已满");
45    rear=(rear+1)%MaxSize;
46    data[rear]=val;
47    _Size++;
48    return *this;
49}

50//------------------------------------------------
51template<class T>
52Queue<T>& Queue<T>::Pop(T& e)
53{
54    if(IsEmpty()) throw exception("队列已空");
55    front=(front+1)%MaxSize;
56    e=data[front];
57    _Size--;
58    return *this;
59}

60//------------------------------------------------
61template<class T>
62T& Queue<T>::Front()
63{
64    if(IsEmpty()) throw exception("队列已空");
65    return data[(front+1)%MaxSize];
66    
67}

68//------------------------------------------------
69template<class T>
70T& Queue<T>::Back()
71{
72    if(IsEmpty()) throw exception("队列已空");
73    return data[rear];
74    
75}

76#endif
posted on 2008-09-19 19:52 杨彬彬 阅读(985) 评论(0)  编辑 收藏 引用 所属分类: 数据结构

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理