Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7569465
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-04-04 15:18:41

  1. /******************************************************************************
  2. * 文件:SeQueue.h
  3. * 功能:线性表顺序存储
  4. * 说明:队列顺序存储结构的类定义
  5.         及类成员函数包括插入、删除等函数的实现
  6. * 时间:2011-4-3                                              
  7. Author: Lzy
  8. *******************************************************************************/
  9. #include <iostream.h>
  10. #include "LinearList.h"

  11. /********************************************************************
  12. * 类名: 类模板SeQueue
  13. * 功能: 队列顺序表类定义
  14. * 说明:队列顺序表相关操作函数的实现
  15. /********************************************************************/
  16. template<class T>
  17. class SeQueue
  18. {
  19. private:
  20.     int front;                                    
  21.     int rear;                            //指向后一个元素

  22.     int MaxSize;                        //队列数组的大小

  23.     T *queue;                            //数组


  24. public:
  25.     SeQueue(int MaxQueueSize = 10);
  26.     bool IsEmpty() const {return front == rear;}
  27.     bool IsFull() const {return ((rear+1)%MaxSize == front)? 1:0;}
  28.     T & First() const;                            //返回队首元素

  29.     T & Last() const;                            //返回队尾元素

  30.     bool Insert(const T &x);
  31.     bool Delete(T &x);
  32.     void visit();
  33. };

  34. /********************************************************************
  35. * 所属类名: SeQueue
  36. * 成员名称:SeQueue
  37. * 函数功能:构造函数
  38. * 说    明:用于初始化队列
  39. /********************************************************************/
  40. template<class T>
  41. SeQueue<T>::SeQueue(int MaxQueueSize)
  42. {
  43.     MaxSize = MaxQueueSize+1;
  44.     queue = new T[MaxSize];
  45.     front = rear = 0;
  46. }

  47. /********************************************************************
  48. * 所属类名: SeQueue
  49. * 成员名称:First
  50. * 函数功能:返回第一个队列元素
  51. * 说    明:
  52. /********************************************************************/
  53. template<class T>
  54. T & SeQueue<T>::First() const
  55. {
  56.     if(IsEmpty())
  57.         throw OutOfBounds();
  58.     return queue[(front+1)%MaxSize];
  59. }

  60. /********************************************************************
  61. * 所属类名: SeQueue
  62. * 成员名称:Last()
  63. * 函数功能:返回最后一个元素
  64. * 说    明:
  65. /********************************************************************/
  66. template<class T>
  67. T & SeQueue<T>::Last() const
  68. {
  69.     if(IsEmpty())
  70.         throw OutOfBounds();
  71.     return queue[rear];    
  72. }

  73. /********************************************************************
  74. * 所属类名: SeQueue
  75. * 成员名称:Insert
  76. * 函数功能:插入元素
  77. * 说    明:把X插入队尾
  78. /********************************************************************/
  79. template<class T>
  80. bool SeQueue<T>::Insert(const T &x)
  81. {
  82.     if(IsFull())
  83.         throw NoMem();
  84.     queue[++rear] = x;
  85.     return true;
  86. }

  87. /********************************************************************
  88. * 所属类名: SeQueue
  89. * 成员名称:Delete
  90. * 函数功能:删除元素
  91. * 说    明:删除队首元素,数据保存至x中
  92. /********************************************************************/
  93. template<class T>
  94. bool SeQueue<T>::Delete(T &x)
  95. {
  96.     if(IsEmpty())
  97.         throw OutOfBounds();
  98.     front = front +1;
  99.     x = queue[front];
  100.     return true;
  101. }

  102. /********************************************************************
  103. * 所属类名: SeQueue
  104. * 成员名称:<<
  105. * 函数功能:<<重载
  106. * 说    明:
  107. /********************************************************************/
  108. template<class T>
  109. ostream& operator<<(ostream& out, const SeQueue<T>& x)
  110. {
  111.     x.Output(out);
  112.     return out;
  113. }

  114. /********************************************************************
  115. * 所属类名: SeQueue
  116. * 成员名称:visit
  117. * 函数功能:队列遍历
  118. * 说    明:
  119. /********************************************************************/
  120. template<class T>
  121. void SeQueue<T>::visit()
  122. {
  123.     for(int i=front+1; i<=rear; i++)
  124.         cout<<queue[i]<<" ";
  125.     cout<<endl;
  126. }
  1. //简单测试程序

  2. void main()
  3. {
  4.     SeQueue<int>L(10);
  5.     for(int i = 0; i < 10; i++)
  6.         L.Insert(i);

  7.     L.visit();

  8.     L.Delete(i);
  9.     cout<<"删除元素:"<<i<<endl;

  10.     L.visit();
  11. }
阅读(2374) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~