找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 2256|回复: 0

【原创】链表模板类

[复制链接]

7

主题

4

回帖

5154

积分

用户组: 技术宅的结界VIP成员

UID
641
精华
1
威望
13 点
宅币
5111 个
贡献
1 次
宅之契约
0 份
在线时间
11 小时
注册时间
2015-1-28
发表于 2015-1-28 17:15:55 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
  1. /*******************************************
  2. **名称:链表模板类
  3. **作者:吴泔游
  4. **用法:
  5.         LinkList<int> list;                                                        //创建
  6.         list.Create(10);                                                        //批量输入10个元素(cin)
  7.         list.GetLength();                                                        //返回链表长度
  8.         list.ShowAll();                                                                //打印所有元素(cout)
  9.         list.RemoveDuplicate();                                                //去掉重复的元素
  10.         list.Delete(2,(int)data);                                        //删除第2个元素,值用data返回,函数返回值代表是否成功
  11.         list.Insert(3,(int)data);                                        //插入data,成为第3个元素
  12.         list.InsertByOrder((int)data);                                //按升序插入data
  13.         list.Release();                                                                //清空链表元素
  14.         list.Reverse();                                                                //逆置链表
  15.         list.InsertSort();                                                        //直接插入排序(升序),链表基本升序时较慢,反之较快
  16.         list.MergeSort();                                                        //归并排序(升序)
  17.         list.GetNodeData(6,int(data));                                //用data返回第6个节点,函数返回值代表是否成功
  18.         list.GetNodeDataPointer(5,int*(p_data));        //用p_pata返回第5个节点的地址,用于直接修改节点等操作
  19.         list.Search((int)key);                                                //遍历查找元素,函数返回值为位置
  20.         list.SetSortMethod(SortInt);                                //指定排序函数


  21.         list_1+list_2                                                                //返回两者元素集合,list_1元素在前
  22.         list_1=list_2                                                                //赋值
  23.         list_1+=list_2                                                                //list_1=list_1+list_2
  24. **最后修改:2015/1/26
  25. 1.添加RemoveDuplicate函数
  26. 2.添加SetCompareMethod函数
  27. ********************************************/
  28. #ifndef _LINK_LIST_H_
  29. #define  _LINK_LIST_H_



  30. #include <iostream>
  31. #include <iomanip>
  32. using namespace std;
  33. #define NULL 0

  34. template <class ElemType>
  35. class ListNode
  36. {
  37. public:
  38.         ElemType data;
  39.         ListNode *next;
  40.         ListNode(){next=NULL;}
  41.         ListNode(ElemType data)
  42.         {
  43.                 this->data=data;
  44.                 next=NULL;
  45.         }
  46. };
  47. //------------------------------------------------------------
  48. template <class ElemType>
  49. class LinkList
  50. {
  51. private:
  52.         ListNode<ElemType> *head;
  53.         int length;
  54.         bool (*SortMethod)(const ElemType& a,const ElemType& b);
  55.         bool (*CompareMethod)(const ElemType& a,const ElemType& b);
  56.         ListNode<ElemType>* Merge(ListNode<ElemType>*& a,ListNode<ElemType>*& b);
  57. public:
  58.         LinkList();
  59.         LinkList(LinkList &b);
  60.         ~LinkList();
  61.         int GetLength();

  62.         void ShowAll();

  63.         void Create(int n);                                                //创建

  64.         void RemoveDuplicate();
  65.         bool Delete(int n,ElemType &data);                //删除
  66.         bool Insert(int n,ElemType data);                        //插入
  67.         bool InsertByOrder(ElemType data);
  68.         void Release();

  69.         void Reverse();                                                        //转置
  70.         void InsertSort();                                                                        //升序
  71.        
  72.         void MergeSort();
  73.         bool GetNodeData(int n,ElemType &data);
  74.         bool GetNodeDataPointer(int n,ElemType*& p_data);
  75.         int  Search(ElemType key);                        //查找
  76.         void SetSortMethod(bool (*fun)(const ElemType& a,const ElemType& b));        //设置排序比较方法(<)
  77.         void SetCompareMethod(bool (*fun)(const ElemType& a,const ElemType& b));        //设置比较方法(==)

  78.         LinkList operator+(const LinkList &b);
  79.         LinkList operator=(const LinkList &b);
  80.         LinkList& operator+=(const LinkList &b);
  81. };
  82. //-----------------------------------------------------------------------------

  83. template <class ElemType>
  84. LinkList<ElemType>::LinkList():head(new ListNode<ElemType>),length(0),SortMethod(NULL),CompareMethod(NULL)
  85. {
  86.         head->next=NULL;
  87. }

  88. template <class ElemType>
  89. LinkList<ElemType>::LinkList(LinkList &b):head(new ListNode<ElemType>),length(b.length),SortMethod(b.SortMethod),CompareMethod(b.CompareMethod)
  90. {
  91.         ListNode<ElemType> *p=this->head;
  92.         ListNode<ElemType> *q;
  93.         ListNode<ElemType> *pb=b.head->next;

  94.         while(pb)
  95.         {
  96.                 q=new ListNode<ElemType>();
  97.                 q->data=pb->data;
  98.                 p->next=q;
  99.                 p=q;
  100.                 pb=pb->next;
  101.         }
  102.         p->next=NULL;
  103. }

  104. template <class ElemType>
  105. int LinkList<ElemType>::GetLength()
  106. {
  107.         return length;
  108. }

  109. template <class ElemType>
  110. void LinkList<ElemType>::ShowAll()
  111. {
  112.         cout<<"共"<<length<<"个元素:"<<endl;
  113.         ListNode<ElemType> *p=this->head->next;
  114.         int i=0;
  115.         while(p)
  116.         {
  117.                 cout<<left<<setw(7)<<p->data<<" ";
  118.                 p=p->next;
  119.                 i++;
  120.                 if(i%10==0)
  121.                         cout<<endl;
  122.         }
  123.         cout<<endl;
  124. }
  125. //创建

  126. template <class ElemType>
  127. void LinkList<ElemType>::Create(int n)
  128. {
  129.         this->length=n;
  130.         ListNode<ElemType> *p=this->head;
  131.         ListNode<ElemType> *q;
  132.         for(int i=0;i!=n;i++)
  133.         {
  134.                 q=new ListNode<ElemType>();
  135.                 cin>>q->data;
  136.                 p->next=q;
  137.                 p=q;
  138.         }
  139.         p->next=NULL;
  140. }

  141. //插入
  142. template <class ElemType>
  143. bool LinkList<ElemType>::Insert(int n,ElemType data)
  144. {
  145.         ListNode<ElemType> *p=this->head;
  146.         ListNode<ElemType> *q;
  147.         int j=0;
  148.         while(p&&j<n-1)
  149.         {
  150.                 p=p->next;
  151.                 j++;
  152.         }
  153.         if(!p||j>n-1)return 0;
  154.         q=new ListNode<ElemType>(data);
  155.         q->next=p->next;
  156.         p->next=q;

  157.         this->length++;
  158.         return 1;
  159. }

  160. template <class ElemType>
  161. bool LinkList<ElemType>::InsertByOrder(ElemType data)
  162. {
  163.         ListNode<ElemType> *p=this->head;
  164.         ListNode<ElemType> *q;
  165.         while(p->next)
  166.         {
  167.                 if(SortMethod==NULL)
  168.                 {
  169.                         if(data < p->next->data)
  170.                                 break;
  171.                 }
  172.                 else
  173.                         if(SortMethod(data,p->next->data))
  174.                                 break;
  175.                 p=p->next;
  176.         }
  177.         q=new ListNode<ElemType>(data);
  178.         q->next        = p->next;
  179.         p->next = q;

  180.         this->length++;
  181.         return 1;
  182. }

  183. template <class ElemType>
  184. void LinkList<ElemType>::RemoveDuplicate()
  185. {
  186.         if(!this->head->next || !this->head->next->next)
  187.                 return;
  188.         ListNode<ElemType> *p=this->head->next;
  189.         ListNode<ElemType> *q,*temp;
  190.         if(CompareMethod==NULL)
  191.         {
  192.                 while(p)
  193.                 {
  194.                         q=p;
  195.                         while(q->next)
  196.                         {
  197.                                 if(p->data == q->next->data)
  198.                                 {
  199.                                         temp=q->next;
  200.                                         q->next=temp->next;
  201.                                         delete temp;
  202.                                         length--;
  203.                                 }
  204.                                 else
  205.                                         q=q->next;

  206.                         }
  207.                         p=p->next;
  208.                 }
  209.         }
  210.         else
  211.         {
  212.                 while(p)
  213.                 {
  214.                         q=p;
  215.                         while(q->next)
  216.                         {
  217.                                 if(CompareMethod(p->data , q->next->data))
  218.                                 {
  219.                                         temp=q->next;
  220.                                         q->next=temp->next;
  221.                                         delete temp;
  222.                                         length--;
  223.                                 }
  224.                                 else
  225.                                         q=q->next;

  226.                         }
  227.                         p=p->next;
  228.                 }
  229.         }
  230.        
  231. }

  232. template <class ElemType>
  233. bool LinkList<ElemType>::Delete(int n,ElemType &data)
  234. {
  235.         ListNode<ElemType> *p=this->head;
  236.         ListNode<ElemType> *q;
  237.         int j=0;
  238.         while(p->next&&j<n-1)
  239.         {
  240.                 p=p->next;
  241.                 j++;
  242.         }
  243.         if(!(p->next)||j>n-1)return 0;
  244.         q=p->next;
  245.         p->next=q->next;
  246.         data=q->data;
  247.         delete q;
  248.         this->length--;
  249.         return 1;

  250. }

  251. template <class ElemType>
  252. void LinkList<ElemType>::Release()
  253. {
  254.         length        =        0;
  255.         ListNode<ElemType> *p=head->next;
  256.         while(p)
  257.         {
  258.                 ListNode<ElemType> *q=p->next;
  259.                 delete p;
  260.                 p=q;
  261.         }
  262.         head->next = NULL;
  263. }
  264. //逆置
  265. template <class ElemType>
  266. void LinkList<ElemType>::Reverse()
  267. {
  268.         ListNode<ElemType> *p=this->head->next;//p为待处理节点,q为下一个
  269.         this->head->next=NULL;
  270.         ListNode<ElemType> *q;
  271.         while(p)
  272.         {
  273.                 q=p->next;//q保存下一个位置
  274.                 p->next=this->head->next;
  275.                 this->head->next=p;
  276.                 p=q;
  277.         }
  278. }

  279. template <class ElemType>
  280. void LinkList<ElemType>::InsertSort()
  281. {
  282.         if(!head->next || !head->next->next)
  283.                 return;
  284.         ListNode<ElemType> *p,*q=head->next->next;
  285.         ListNode<ElemType> *temp;
  286.         head->next->next=NULL;
  287.         while(q)
  288.         {
  289.                 p=head;
  290.                 while(p&&p->next)
  291.                 {
  292.                         if(SortMethod==NULL)
  293.                         {
  294.                                 if(p->next->data < q->data)
  295.                                         break;
  296.                         }
  297.                         else
  298.                         {
  299.                                 if(SortMethod(p->next->data,q->data))
  300.                                         break;
  301.                         }
  302.                         p=p->next;
  303.                 }
  304.                 temp=q->next;
  305.                 q->next=p->next;
  306.                 p->next=q;
  307.                 q=temp;
  308.         }
  309.         this->Reverse();
  310. }

  311. template <class ElemType>
  312. ListNode<ElemType>* LinkList<ElemType>::Merge(ListNode<ElemType>*& a,ListNode<ElemType>*& b)
  313. {
  314.         if(a==NULL)
  315.                 return b;
  316.         if(b==NULL)
  317.                 return a;
  318.         ListNode<ElemType> *r=new ListNode<ElemType>();
  319.         ListNode<ElemType> *c;
  320.         ListNode<ElemType> *p;
  321.         ListNode<ElemType> *q;
  322.        
  323.         c=r;
  324.         q=a->next;
  325.         p=b->next;
  326.         while(q&&p)
  327.         {
  328.                 if(SortMethod==NULL)
  329.                 {
  330.                         if(p->data < q->data)
  331.                         {
  332.                                 c->next=p;
  333.                                 p=p->next;
  334.                         }
  335.                         else
  336.                         {
  337.                                 c->next=q;
  338.                                 q=q->next;
  339.                         }
  340.                 }
  341.                 else
  342.                 {
  343.                         if(SortMethod(p->data,q->data))
  344.                         {
  345.                                 c->next=p;
  346.                                 p=p->next;
  347.                         }
  348.                         else
  349.                         {
  350.                                 c->next=q;
  351.                                 q=q->next;
  352.                         }
  353.                 }
  354.                 c=c->next;
  355.         }

  356.         if(q==NULL)
  357.                 c->next=p;
  358.         else if(p==NULL)
  359.                 c->next=q;

  360.         delete a,b;
  361.         a=NULL;
  362.         b=NULL;
  363.         return r;

  364.        
  365. }


  366. template <class ElemType>
  367. void LinkList<ElemType>::MergeSort()
  368. {
  369.         if(length <= 1)
  370.                 return;
  371.         int l=(length+1)/2*2;
  372.         ListNode<ElemType>** r=new ListNode<ElemType>*[l];
  373.         ListNode<ElemType> *p=head->next;

  374.         for(int i=0;i!=l;i++)
  375.                 r[i]=new ListNode<ElemType>();

  376.         int i=0;
  377.         while(p)
  378.         {
  379.                 r[i]->next=p;
  380.                 if(p->next ==NULL)
  381.                         break;
  382.                 p=p->next;
  383.                 r[i]->next->next=NULL;
  384.                
  385.                 i++;
  386.         }
  387.         for(int d=l;d!=1;d=(d+1)/2)
  388.                 for(int i=0,j=0;j <=d-1;i++,j+=2)
  389.                         r[i]=Merge(r[j],r[j+1]);
  390.         head=r[0];
  391. }

  392. template <class ElemType>
  393. bool LinkList<ElemType>::GetNodeData(int n,ElemType &data)
  394. {
  395.         if(n<=0)
  396.                 return 0;
  397.         ListNode<ElemType> *p=this->head;
  398.         int j=0;
  399.         while(p&&j<n)
  400.         {
  401.                 p=p->next;
  402.                 j++;
  403.         }
  404.         if(!p||j>n)
  405.                 return 0;
  406.         else
  407.         {
  408.                 data=p->data;
  409.                 return 1;
  410.         }


  411. }

  412. template <class ElemType>
  413. bool LinkList<ElemType>::GetNodeDataPointer(int n,ElemType*& p_data)
  414. {
  415.         if(n<=0)
  416.                 return 0;
  417.         ListNode<ElemType> *p=this->head;
  418.         int j=0;
  419.         while(p&&j<n)
  420.         {
  421.                 p=p->next;
  422.                 j++;
  423.         }
  424.         if(!p||j>n)
  425.                 return 0;
  426.         else
  427.         {
  428.                 p_data=&p->data;
  429.                 return 1;
  430.         }


  431. }

  432. template <class ElemType>
  433. int LinkList<ElemType>::Search(ElemType key)
  434. {
  435.         ListNode<ElemType> *p=this->head->next;
  436.         int i=1;
  437.         if(CompareMethod==NULL)
  438.         {
  439.                 while(p)
  440.                 {
  441.                         if(p->data == key)
  442.                                 return i;
  443.                         i++;
  444.                         p=p->next;
  445.                 }
  446.         }
  447.         else
  448.         {
  449.                 while(p)
  450.                 {
  451.                         if(CompareMethod(p->data,key))
  452.                                 return i;
  453.                         i++;
  454.                         p=p->next;
  455.                 }
  456.         }
  457.         return 0;
  458. }

  459. //设置排序比较方法(<)
  460. template <class ElemType>
  461. void LinkList<ElemType>::SetSortMethod(bool (*fun)(const ElemType& a,const ElemType& b))
  462. {
  463.         SortMethod = fun;
  464. }

  465. //设置排序比较方法(==)
  466. template <class ElemType>
  467. void LinkList<ElemType>::SetCompareMethod(bool (*fun)(const ElemType& a,const ElemType& b))
  468. {
  469.         CompareMethod = fun;
  470. }

  471. template <class ElemType>
  472. LinkList<ElemType> LinkList<ElemType>::operator+(const LinkList<ElemType> &b)
  473. {
  474.         LinkList<ElemType> c(*this);
  475.         ListNode<ElemType> *p=b.head->next;
  476.         ListNode<ElemType> *q;
  477.         ListNode<ElemType> *pc=c.head;
  478.         while(pc->next)
  479.         {
  480.                 pc=pc->next;
  481.         }
  482.         while(p)
  483.         {
  484.                 q=new ListNode<ElemType>();
  485.                 q->data=p->data;
  486.                 pc->next=q;
  487.                 pc=q;
  488.                 p=p->next;
  489.         }
  490.         pc->next=NULL;

  491.         c.length+=b.length;
  492.         return c;

  493. }
  494. template <class ElemType>
  495. LinkList<ElemType> LinkList<ElemType>::operator=(const LinkList<ElemType> &b)
  496. {
  497.         this->Release();
  498.         ListNode<ElemType> *p=this->head;
  499.         ListNode<ElemType> *q;
  500.         ListNode<ElemType> *pb=b.head->next;

  501.         while(pb)
  502.         {
  503.                 q=new ListNode<ElemType>();
  504.                 q->data=pb->data;
  505.                 p->next=q;
  506.                 p=q;
  507.                 pb=pb->next;
  508.         }
  509.         p->next=NULL;
  510.         this->length=b.length;
  511.         this->SortMethod=b.SortMethod;
  512.         this->CompareMethod=b.CompareMethod;
  513.         return *this;
  514. }
  515. template <class ElemType>
  516. LinkList<ElemType>& LinkList<ElemType>::operator+=(const LinkList<ElemType> &b)
  517. {
  518.         *this=*this+b;
  519.         return *this;
  520. }
  521. template <class ElemType>
  522. LinkList<ElemType>::~LinkList()
  523. {
  524.         ListNode<ElemType> *p=head->next;
  525.         if(head)
  526.         {
  527.                 delete head;
  528.         }
  529.         while(p)
  530.         {
  531.                 ListNode<ElemType> *q=p->next;
  532.                 delete p;
  533.                 p=q;
  534.         }
  535. }


  536. #endif
复制代码





例子和源码在附件内

链表模板类.rar

10 KB, 下载次数: 2

回复

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-29 21:33 , Processed in 0.046887 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表