C++数据结构之单链表

线性表包含数据域和指针域。单链表用一组地址任意的存储单元存放线性表中的数据元素。

线性表包含 数据域和指针域 其中,data存储数据本身的值,next存储后继元素的地址 下面的图表示的是一个数据节点

单链表的结构示意图(包括空的单链表):

单链表的节点类:

  1.  template<classT>  
  2. classNode  
  3. {  
  4. public:  
  5. T data;//数据  
  6. Node<T> *next;//next指针  
  7. Node()  
  8. {  
  9. this->next=NULL;//构造空的节点  
  10. }  
  11. Node(T data,Node<T> *next=NULL)//构造一个节点  
  12. {  
  13. this->data=data;  
  14. this->next=next;  
  15. }  
  16. }; 

 

单链表类声明如下:

 

  1. #include<iostream>  
  2. #include "Node.h"//单链表节点类  
  3. template<classT>  
  4. classSinglyLinkedList //单链表类  
  5. {  
  6. public:  
  7. Node<T> *head;//单链表的头指针。  
  8. SinglyLinkedList();//构造空的单链表。  
  9. SinglyLinkedList(T value[], intn);//构造由指定的数组提供的单链表  
  10. ~SinglyLinkedList();//析构  
  11. boolisEmpty();//判断是否为空。  
  12. intlength();//获取长度  
  13. Node<T>* getNode(inti);//返回第i(i>=0)个节点指针  
  14. get(inti);//返回第i个元素  
  15. boolset(inti,T x);//设置第i个元素为x  
  16. template<classT> friend std::ostream& operator<<(std::ostream& out,SinglyLinkedList<T> &list);  
  17. Node<T>* insert(inti,T x);//插入第I个节点并返回第i个节点的指针  
  18. boolremove(inti,T& old);//删除第i个元素,将删除的元素存放到old  
  19. voidclear();//清空单链表  
  20. voidconcat(SinglyLinkedList<T> &list);//将List链接在当前单链表之后  
  21. }; 

 

单链表部分如构造空的链表对象,析构,判断为空的实现,没有要讲的算法,实现如下:

 

  1. template<classT>  
  2. SinglyLinkedList<T>::SinglyLinkedList()//构造空的单链表  
  3. {  
  4. this->head=NULL;  
  5.  }  
  6. template<classT>  
  7. SinglyLinkedList<T>::~SinglyLinkedList()//析构  
  8. {  
  9. clear();  
  10. }  
  11. template<classT>  
  12. boolSinglyLinkedList<T>::isEmpty()//判断链表是否为空  
  13. {  
  14. returnthis->head==NULL;  

 

单链表的遍历操作,遍历单链表是指从第一个节点开始访问,沿着节点的Next可依次访问单链表中的各个节点,并且各个节点只被访问一次。实现的单链表遍历的基本算法如下:

 

  1. intj=0;  
  2. Node<T> *p=head;  
  3. while(p!=NULL&&j<i)  
  4. {  
  5. j++;  
  6. p=p->next;  

 

单链表的length(),get(),set(),clear()和输出等操作都基于以上算法。

  1. template<classT>  
  2. intSinglyLinkedList<T>::length()  
  3. {  
  4.  inti=0;  
  5. Node<T> *p=head;//创建一个用于遍的变量  
  6. while(p!=NULL)  
  7. {  
  8. i++;  
  9. std::cout<<p->data;  
  10. p=p->next;  
  11. }  
  12.  returni;  
  13. }  
  14. template<classT>  
  15. Node<T>* SinglyLinkedList<T>::getNode(inti)  
  16. {  
  17. if(i<0)  
  18. returnNULL;  
  19. intj=0;  
  20. Node<T> *p=head;  
  21. while(p!=NULL&&j<i)  
  22. {  
  23. j++;  
  24. p=p->next;  
  25. }  
  26. returnp;  
  27. }  
  28. template<classT>  
  29. T SinglyLinkedList<T>::get(inti)  
  30. {  
  31. Node<T> *p=getNode(i);  
  32. if(p!=NULL)  
  33. returnp->data;  
  34. T d;  
  35. returnd;  
  36. //throw "单链表为空或者参数指定的元素不存在";  
  37. }  
  38. template<classT>  
  39. boolSinglyLinkedList<T>::set(inti,T x)  
  40. {  
  41. Node<T> *p=getNode(i);  
  42. if(p!=NULL)  
  43. {  
  44. p->data=x;  
  45. returntrue;  
  46. }  
  47. returnfalse;  
  48. }  
  49. template<classT>  
  50. std::ostream& operator<<(std::ostream& out,SinglyLinkedList<T> &list)  
  51. {  
  52. Node<T> *p=list.head;  
  53. out<<"(";  
  54. while(p!=NULL)  
  55. {  
  56. out<<p->data;  
  57. p=p->next;  
  58. if(p!=NULL)  
  59. out<<",";  
  60. }  
  61. out<<") ";  
  62. returnout;  
  63. }  
  64. template<classT>  
  65. voidSinglyLinkedList<T>::clear()  
  66. {  
  67. Node<T> *p=head;  
  68. while(p!=NULL)  
  69. {  
  70. Node<T> *q=p;  
  71. p=p->next;  
  72. delete q;  
  73. }  
  74. head=NULL;  
  75.  } 

单链表的插入操作,单链表不像顺序表,对与表的插入和删除很简单:

空表插入/头插入

  1. Node<T> *q=NULL;  
  2. if(head==NULL||i<0)//头插入(单链表为空或者)  
  3. {  
  4. q=newNode<T>(x,head);  
  5. head=q;  
  6. }  
  7. 中间插入/尾插入  
  8. p->next=newNode<T>(x,p->next);  
  9. 单链表insert()以及参数构造函数:  
  10.  template<classT>  
  11. Node<T>* SinglyLinkedList<T>::insert(inti,T x)  
  12. {  
  13. Node<T> *q=NULL;  
  14. if(head==NULL||i<0)//头插入(单链表为空或者)  
  15. {  
  16. q=newNode<T>(x,head);  
  17. head=q;  
  18. }  
  19. else 
  20. {  
  21. intj=0;  
  22. Node<T> *p=head;  
  23. while(p->next!=NULL&&j<i-1)  
  24. {  
  25. j++;  
  26. p=p->next;  
  27. }  
  28. q=newNode<T>(x,p->next);  
  29.  p->next=q;  
  30. }  
  31. returnq;  
  32. }  
  33.  template<classT>  
  34. SinglyLinkedList<T>::SinglyLinkedList(T table[],intn)  
  35. {  
  36. head=NULL;  
  37. if(n>0)  
  38. {  
  39. head=newNode<T>(table[0]);//创建节点  
  40.  Node<T> *rear=head;//创建一个指向头节点的指针  
  41. inti=1;  
  42.  while(i<n)  
  43. {  
  44. rear->next=newNode<T>(table[i++]);  
  45. rear=rear->next;  
  46. }  
  47. }  

 单链表的删除操作也分两类:

头删除

  1. Node<T> *q=head;  
  2. head=head->next;  
  3. delete q; 

中间/尾删除

  1. Node<T> *q=p->next;  
  2. if(q!=NULL)//判断删除节点  
  3. {  
  4. p->next=q->next;//让删除节点的前驱Next指针下一节点  
  5. delete q;//删除该节点  

单链表的删除函数remove()实现:

  1. template<classT>  
  2. boolSinglyLinkedList<T>::remove(inti,T &old)  
  3. {  
  4. if(i<0||head==NULL)  
  5. {  
  6. Node<T> *q=head;  
  7. old=q->data;  
  8. head=head->next;  
  9. delete q;  
  10.  }  
  11. else 
  12. {  
  13. Node<T> *p=getNode(i-1);//获取删除节点的前驱  
  14.  if(p!=NULL&&p->next!=NULL)//判断删除节点和删除节点是否为空  
  15. {  
  16. Node<T> *q=p->next;//新建一个节点指针,将删除接点复制过去  
  17. old=q->data;  
  18. p->next=q->next;//让删除节点的前驱Next指针下一节点  
  19. delete q;//删除该节点  
  20.  returntrue;  
  21. }  
  22.  }  
  23. returnfalse;  
  24. }  
  25. 单链表的链接函数:concat()  
  26. template<classT>  
  27. voidSinglyLinkedList<T>::concat(SinglyLinkedList<T> &list)  
  28. {  
  29. if(this->head==NULL)  
  30. {  
  31. this->head=list->head;  
  32. }  
  33. else 
  34. {  
  35. Node<T> *p=head;  
  36. while(p->next!=NULL)  
  37. {  
  38. p=p->next;  
  39. }  
  40. p=list->head;  
  41. }  
  42. list->head=NULL;//设置单链表为空,否则运行出错  

以上对C++单链表的分析 添加一个学生结构和一个测试函数:

  1. Student.h  
  2. structStudent  
  3. {  
  4. charnumber[10]; //学号  
  5. charname[20]; //姓名  
  6. doublescore; //得分  
  7. friend std::ostream& operator<<(std::ostream& out,Student &stu)  
  8. {  
  9. out<<"学号:"<<stu.number<<"姓名:"<<stu.name<<"得分:"<<stu.score;  
  10. returnout;  
  11. }  
  12. };  
  13. 主函数:  
  14. #include<iostream>  
  15. #include "SinglyLinkedList.h" 
  16. #include "Student.h" 
  17. void_TestToSinglyLinkedList()  
  18. {  
  19. Student data[]={{"090313018","Silvester",45.4},{"090313018","捐赠",45.4},{"090313018","版主",45.6}};  
  20. SinglyLinkedList<Student> m(data,3);  
  21. Student t;  
  22. std::cout<<(m.isEmpty()?"不为空!":"该链表为空!")<<std::endl;  
  23. std::cout<<"长度:"<<m.length()<<std::endl;  
  24. std::cout<<"移除2个学生"<<m.remove(1,t)<<std::endl;  
  25. std::cout<<"t:"<<t<<std::endl;  
  26. std::cout<<"2个学生信息"<<m.getNode(1)<<std::endl;  
  27. Student s={"415646","fdsfs",453.1};  
  28. std::cout<<m.get(1)<<m.set(1,s)<<m.insert(5,s)<<std::endl;  
  29. }  
  30. voidmain()  
  31.  {  
  32. _TestToSinglyLinkedList();  
  33. system("pause");  

提供源代码下载地址:http://39327.42la.com.cn/DataFile/Code/C++/SinglyLinkedList.zip

原文地址:http://www.cnblogs.com/Arrays/archive/2012/02/01/2335164.html

posted on 2012-08-02 14:58 章涛 阅读(341) 评论(0)  编辑 收藏 引用


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


<2012年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜