Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7565330
  • 博文数量: 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 19:03:46

  1. /******************************************************************************
  2. * 文件:LinearList.h
  3. * 功能:链式存储
  4. * 说明:单链表操作的相关函数
  5. * 时间:2011-4-3                                              
  6. Author: Lzy
  7. *******************************************************************************/

  8. #include <iostream.h>
  9. #include "LinearList.h"

  10. template<class T>
  11. class LinearListLink;                //声明


  12. /********************************************************************
  13. * 类名: LinkNode
  14. * 功能:
  15. * 说明:链式存储节点、与指针域
  16. /********************************************************************/
  17. template<class T>
  18. class LinkNode
  19. {
  20. public:
  21.     friend class LinearListLink<T>;                //LinearListLink定义为友元类

  22. private:
  23.     T data;
  24.     LinkNode<T> *Link;
  25. };

  26. /********************************************************************
  27. * 类名: LinearListLink
  28. * 功能: 链式存储
  29. * 说明:链表的相关操作函数
  30. /********************************************************************/
  31. template<class T>
  32. class LinearListLink : public LinearList<T>                //表头从LinearList派生

  33. {
  34. public:
  35.     LinearListLink(){first = 0;}
  36.     virtual ~LinearListLink();
  37.     virtual bool IsEmpty() const {return first == 0;}
  38.     virtual int Length() const;
  39.     virtual bool Find(int k, T &x) const;
  40. //    virtual int Search(const T &x) const;    

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

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

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

  44. //    virtual LinkNode * GetNode(long i);            //返回第i个结点

  45. //    virtual bool Get(LinkNode *node, T &x);        //获取结点值

  46. //    virtual bool Set(LinkNode *node, const T &x);//设置结点值

  47.     virtual LinkNode<T> * GetHead(){return first;}            //获得头结点指针


  48. protected:
  49.     LinkNode<T> *first;                                //指向第一个节点的指针

  50. };

  51. /********************************************************************
  52. * 所属类名: LinearListLink
  53. * 成员名称:~LinearListLink()
  54. * 函数功能:析构函数
  55. * 说    明:用于删除链表中所有节点
  56. /********************************************************************/
  57. template<class T>
  58. LinearListLink<T>::~LinearListLink()
  59. {
  60.     LinkNode<T> *next;    
  61.     while(first)
  62.     {
  63.         next = first->Link;                            //下一个结点

  64.         delete first;
  65.         first = next;
  66.     }
  67. }

  68. /********************************************************************
  69. * 所属类名: LinearListLink
  70. * 成员名称:Length()
  71. * 函数功能:返回链表中元素总数
  72. * 说    明:
  73. /********************************************************************/
  74. template<class T>
  75. int LinearListLink<T>::Length() const
  76. {
  77.     LinkNode<T> *current = first;
  78.     int len = 0;
  79.     while(current)
  80.     {
  81.         len++;
  82.         current = current->Link;
  83.     }
  84.     return len;
  85. }

  86. /********************************************************************
  87. * 所属类名: LinearListLink
  88. * 成员名称:Find()
  89. * 函数功能:查表
  90. * 说    明:寻找链表中第K个元素,并将其值送至x
  91. /********************************************************************/
  92. template<class T>
  93. bool LinearListLink<T>::Find(int k, T &x) const
  94. {
  95.     if(k<1)
  96.         return false;
  97.     LinkNode<T> *current = first;
  98.     int index = 0;
  99.     while(index < k && current)        //查找第K个元素,且节点不为空

  100.     {
  101.         index++;
  102.         current = current->Link;
  103.     }

  104.     if(current)
  105.     {
  106.         x = current->data;            //返回第K个元素的值

  107.         return true;    
  108.     }
  109.     return false;
  110. }

  111. /********************************************************************
  112. * 所属类名: LinearListLink
  113. * 成员名称:Output
  114. * 函数功能:输出表节点
  115. * 说    明:将链表元素送至输出流
  116. /********************************************************************/
  117. template<class T>
  118. void LinearListLink<T>::Output(ostream &out) const
  119. {
  120.     LinkNode<T> *current;
  121.     for(current = first; current; current = current->Link)    //遍历所有节点

  122.         out<<current->data<<" ";                    //输出

  123. }

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

  136. /********************************************************************
  137. * 所属类名: LinearListLink
  138. * 成员名称:Insert
  139. * 函数功能:元素插入
  140. * 说    明:在第K个节点之后插入x
  141. /********************************************************************/
  142. template<class T>
  143. bool LinearListLink<T>::Insert(int k, const T &x)
  144. {
  145.     if(k < 0)
  146.         throw OutOfBounds();

  147.     LinkNode<T> *p = first;
  148.     for(int index = 1; index < k && p; index++)
  149.         p = p->Link;            //p最终指向第K个节点


  150.     LinkNode<T> *y = new LinkNode<T>;
  151.     y->data = x;                //插入数据


  152.     if(k)
  153.     {
  154.         y->Link = p->Link;
  155.         p->Link = y;            //插在P节点之后

  156.     }
  157.     else
  158.     {
  159.         y->Link = first;
  160.         first = y;                //作为第一个元素插入

  161.     }

  162.     return true;    
  163. }

  164. /********************************************************************
  165. * 所属类名: LinearListLink
  166. * 成员名称:Delete
  167. * 函数功能:删除元素
  168. * 说    明:把第K个节点值取至x,然后删除第K个节点
  169. /********************************************************************/
  170. template<class T>
  171. bool LinearListLink<T>::Delete(int k, T &x)
  172. {
  173.     if(k < 1 || !first)                //K>=0,必须不是空表

  174.         throw OutOfBounds();
  175.     
  176.     LinkNode<T> *p = first;
  177.     if(k == 1)
  178.         first = first->Link;        //p指向第1个元素删除

  179.     else
  180.     {
  181.         LinkNode<T> *q = first;
  182.         for(int index = 1; index < k-1 && q; index++)
  183.             q = q->Link;            //指向第K个节点

  184.         if(!q || !q->Link)            //不存在第K个元素

  185.             throw OutOfBounds();

  186.         p = q->Link;                
  187.         q->Link = p->Link;            //从链表中删除该元素

  188.     }
  189.     x = p->data;
  190.     delete p;
  191.     return true;
  192. }
  1. /******************************************************************************
  2. * 文件:单链表测试程序
  3. * 功能:测试程序
  4. * 说明:测试单链表操作的相关函数
  5. * 时间:2011-4-3                                              
  6. Author: Lzy
  7. *******************************************************************************/

  8. #include <iostream.h>
  9. #include "LinearListLink.h"

  10. void main()
  11. {
  12.     LinearListLink<int>L;
  13.     L.Insert(0, 1);            //插入第一个元素

  14.     cout<<"链表长度:"<<L.Length()<<endl<<"数据元素:"<<L<<endl;

  15.     L.Insert(1, 2);
  16.     L.Insert(2, 3);
  17.     L.Insert(3, 4);
  18.     cout<<"链表长度:"<<L.Length()<<endl<<"数据元素:"<<L<<endl;

  19.     int z;
  20.     L.Delete(2, z);
  21.     cout<<"删除数据元素:"<<z<<endl;
  22.     cout<<"链表长度:"<<L.Length()<<endl<<"数据元素:"<<L<<endl;

  23.     L.Find(2,z);
  24.     cout<<"找到的数据元素:"<<z<<endl;
  25. }
阅读(1867) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~