Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7563385
  • 博文数量: 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 16:08:21

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

  11. template<class T>
  12. class LinkedQueue;        //声明


  13. /********************************************************************
  14. * 类名: 类模板Node
  15. * 功能: 节点定义
  16. * 说明:
  17. /********************************************************************/
  18. template<class T>
  19. class Node
  20. {
  21.     friend class LinkedQueue<T>;
  22. private:
  23.     T data;
  24.     Node<T> *link;
  25. };


  26. /********************************************************************
  27. * 类名: LinkedQueue
  28. * 功能: 队列链表类定义
  29. * 说明:队列链表相关操作函数的实现
  30. /********************************************************************/
  31. template<class T>
  32. class LinkedQueue
  33. {
  34. private:
  35.     Node<T> *front;                //指向队头节点

  36.     Node<T> *rear;                //指向队尾节点

  37.     
  38. public:
  39.     LinkedQueue() {front = rear = 0;}
  40.     ~LinkedQueue();
  41.     bool IsEmpty() const {return front ? false:true;}
  42.     T & First() const;                            //返回队首元素

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

  44.     bool Insert(const T &x);
  45.     bool Delete(T &x);
  46.     void visit();
  47. };

  48. /********************************************************************
  49. * 所属类名: LinkedQueue
  50. * 成员名称:~LinkedQueue
  51. * 函数功能:析构函数
  52. * 说    明:删除所有节点
  53. /********************************************************************/
  54. template<class T>
  55. LinkedQueue<T>::~LinkedQueue()
  56. {                        
  57.     Node<T> *next;
  58.     while(front)
  59.     {
  60.         next = front->link;
  61.         delete front;
  62.         front = next;
  63.     }
  64. }

  65. /********************************************************************
  66. * 所属类名: LinkedQueue
  67. * 成员名称:First()
  68. * 函数功能:返回第一个队列元素
  69. * 说    明:
  70. /********************************************************************/
  71. template<class T>
  72. T & LinkedQueue<T>::First() const
  73. {                            
  74.     if(IsEmpty())
  75.         throw OutOfBounds();
  76.     return front->data;
  77. }

  78. /********************************************************************
  79. * 所属类名: LinkedQueue
  80. * 成员名称:Last()
  81. * 函数功能:返回最后一个元素
  82. * 说    明:
  83. /********************************************************************/
  84. template<class T>
  85. T & LinkedQueue<T>::Last() const
  86. {                            
  87.     if(IsEmpty())
  88.         throw OutOfBounds();
  89.     return rear->data;
  90. }

  91. /********************************************************************
  92. * 所属类名: LinkedQueue
  93. * 成员名称:Insert
  94. * 函数功能:插入元素
  95. * 说    明:把X插入队尾
  96. /********************************************************************/
  97. template<class T>
  98. bool LinkedQueue<T>::Insert(const T &x)        
  99. {
  100.     Node<T> *p = new Node<T>;
  101.     p->data = x;
  102.     p->link = 0;
  103.     if(front)
  104.         rear->link = p;                //队尾添加新节点

  105.     else
  106.         front = p;                    //插入第一个元素

  107.     rear = p;                        //队尾节点后移

  108.     return true;
  109. }

  110. /********************************************************************
  111. * 所属类名: LinkedQueue
  112. * 成员名称:Delete
  113. * 函数功能:删除元素
  114. * 说    明:删除队首元素,数据保存至x中
  115. /********************************************************************/
  116. template<class T>
  117. bool LinkedQueue<T>::Delete(T &x)
  118. {
  119.     if(IsEmpty())
  120.         throw OutOfBounds();        //检查队列是否为空

  121.     x = front->data;                //数据弹出

  122.     Node<T> *p = front;
  123.     front = front->link;            //队首指针后移

  124.     delete p;                        //删除第一个节点

  125.     return true;
  126. }

  127. /********************************************************************
  128. * 所属类名: SeQueue
  129. * 成员名称:<<
  130. * 函数功能:<<重载
  131. * 说    明:
  132. /********************************************************************/
  133. template<class T>
  134. ostream& operator<<(ostream& out, const SeQueue<T>& x)
  135. {
  136.     x.Output(out);
  137.     return out;
  138. }

  139. /********************************************************************
  140. * 所属类名: SeQueue
  141. * 成员名称:visit
  142. * 函数功能:队列遍历
  143. * 说    明:
  144. /********************************************************************/
  145. template<class T>
  146. void LinkedQueue<T>::visit()
  147. {
  148.     Node<T> *p = front;
  149.     while(p)                //遍历队列

  150.     {
  151.         cout<<p->data<<" ";
  152.         p = p->link;
  153.     }
  154.     cout<<endl;
  155. }
  1. //测试程序
  2. void main()
  3. {
  4.     LinkedQueue<int>L;
  5.     for(int i = 1; i < 16; i++)                
  6.         L.Insert(i);                    //插入15个元素
  7.     L.visit();                            //全部显示
  8.     int z;
  9.     for(i = 1; i < 6; i++)
  10.     {    
  11.         L.Delete(z);                    //删除5个元素
  12.         cout<<z<<" ";
  13.     }
  14.     cout<<endl;
  15.     L.visit();

  16. }
阅读(1792) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~