Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7563408
  • 博文数量: 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-03 14:30:54

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

  11. /********************************************************************
  12. * 类名: 类模板LinearListSqu
  13. * 功能: 动态分配顺序存储结构的线性表类
  14. * 说明:线性表的长度可变
  15. /********************************************************************/
  16. template<class T>
  17. class LinearListSqu : public LinearList<T>            //从LinearList派生

  18. {
  19. public:
  20.     LinearListSqu(int MaxListSize = 10);
  21.     virtual ~LinearListSqu() {if(element) delete[]element;}
  22.     virtual bool IsEmpty() const {return length ==0;}
  23.     virtual int Length() const {return length;}
  24.     virtual bool Find(int k, T &x) const;            //返回第K个元素到x中,在具体类中完成

  25.     virtual bool Delete(int k, T &x);                //删除第K个元素并将它返回至x

  26.     virtual bool Insert(int k, const T &x);            //在第K个元素之后插入x

  27.     virtual void Output(ostream &out) const;        //输出


  28. protected:
  29.     long length;            //线性表长度

  30.     long MaxSize;            //表最大长度

  31.     T *element;                //动态数组

  32. };

  33. /********************************************************************
  34. * 所属类名: LinearListSqu
  35. * 成员名称:LinearListSqu
  36. * 函数功能:初始化线性表
  37. * 说    明:
  38. /********************************************************************/
  39. template<class T>
  40. LinearListSqu<T>::LinearListSqu(int MaxListSize)        
  41. {                                //线性表初始化

  42.     MaxSize = MaxListSize;    
  43.     element = new T[MaxSize];
  44.     length = 0;
  45. }

  46. /********************************************************************
  47. * 所属类名: LinearListSqu
  48. * 成员名称:Find
  49. * 函数功能:查找元素
  50. * 说    明:把第K个元素取至X中,如果不存大返回假,否则返回真
  51. /********************************************************************/
  52. template<class T>
  53. bool LinearListSqu<T>::Find(int k, T &x) const
  54. {
  55.     if(k < 1 || k > length)
  56.         return false;
  57.     x = element[k - 1];
  58.     return true;
  59. }

  60. /********************************************************************
  61. * 所属类名: LinearListSqu
  62. * 成员名称:Output
  63. * 函数功能:输出表中所有元素至&out
  64. * 说    明:
  65. /********************************************************************/
  66. template<class T>
  67. void LinearListSqu<T>::Output(ostream &out) const
  68. {
  69.     for(int i = 0; i < length; i++)
  70.         out<<element[i]<<" ";            //把表输送至输出流

  71. }

  72. /********************************************************************
  73. * 所属类名: LinearListSqu
  74. * 成员名称:<<
  75. * 函数功能:<<重载
  76. * 说    明:
  77. /********************************************************************/
  78. template<class T>
  79. ostream& operator<<(ostream& out, const LinearListSqu<T>& x)
  80. {
  81.     x.Output(out);
  82.     return out;
  83. }

  84. /********************************************************************
  85. * 所属类名: LinearListSqu
  86. * 成员名称:Insert
  87. * 函数功能:插入数据元素
  88. * 说    明:在第K元素之后插入X
  89. /********************************************************************/
  90. template<class T>
  91. bool LinearListSqu<T>::Insert(int k, const T &x)
  92. {
  93.     if(k < 0 || k > length)                    //判断插入位置是否合法

  94.         throw OutOfBounds();
  95.     if(MaxSize == length)
  96.         throw NoMem();

  97.     for(int i = length - 1; i >= k; i--)
  98.         element[i+1] = element[i];            //元素后移

  99.     element[k] = x;                            //数据插入

  100.     length++;                                //线性表长度加1

  101.     return true;
  102. }

  103. /********************************************************************
  104. * 所属类名: LinearListSqu
  105. * 成员名称:Delete
  106. * 函数功能:删除数据
  107. * 说    明:把第K个元素放入x中然后删除该元素
  108.             如果不存在,则收发异常OutOfBounds
  109. /********************************************************************/
  110. template<class T>
  111. bool LinearListSqu<T>::Delete(int k, T &x)
  112. {
  113.     if(Find(k, x))                        //找到第K个元素,放入X中

  114.     {
  115.         for(int i = k; i < length; i++)
  116.             element[i-1] = element[i];        //删除元素

  117.         length--;
  118.         return true;
  119.     }
  120.     else
  121.         throw OutOfBounds();
  122. }
  1. /******************************************************************************
  2. * 文件:
  3. * 功能:测试程序
  4. * 说明:测试线性表函数的操作
  5. * 时间:2011-4-3                                              
  6. Author: Lzy
  7. *******************************************************************************/
  8. #include <iostream.h>
  9. #include "LinearListSqu.h"

  10. void main()
  11. {try{
  12.     
  13.     LinearListSqu<int>L(5);                //初始化线性表

  14.     cout<<"Length="<<L.Length()<<endl;
  15.     cout<<"IsEmpty="<<L.IsEmpty()<<endl;

  16.     bool ok = L.Insert(0,2);            //插入元素

  17.     if(ok)
  18.         cout<<"ok"<<endl;
  19.     L.Insert(1,6);    L.Insert(2,8);
  20.     cout<<"List is "<<L<<endl;

  21.     int z;
  22.     L.Find(1, z);                        //查找元素

  23.     cout<<"第一个元素是:"<<z<<endl;

  24.     cout<<"Length="<<L.Length()<<endl;

  25.     L.Delete(1, z);                        //删除元素

  26.     cout<<"删除元素:"<<z<<endl;

  27.     cout<<"List is "<<L<<endl;
  28.     cout<<"Length="<<L.Length()<<endl;
  29. }

  30.     catch(...)
  31.     {
  32.         cerr<<"错误"<<endl;
  33.     }

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