第四周 建立单链表算法库

来源:互联网 发布:美股行情软件下载 编辑:程序博客网 时间:2024/06/16 16:56
  1. 烟台大学计算机学院   
  2.    
  3. 文件名称:xm.cpp   
  4.    
  5. 作者:李浩南
  6.    
  7. 完成日期:2017年9月26日   
  8.     
  9.    
  10. 输入描述:无  
  11.    
  12. 输出描述:进行了各个的链表的值  
  13.    
  14. */     

list.cpp

 

[cpp] view plain copy
print?
  1. #include <stdio.h>    
  2. #include <malloc.h>    
  3. #include "list.h"    
  4. void initList(Linklist *&L)//初始化链表    
  5. {    
  6.     L=(Linklist *)malloc(sizeof(Linklist));//动态开辟内存    
  7.     L->next=NULL;//链表为空    
  8. }    
  9.     
  10. bool ListInsert(Linklist *&L,int i,ElemType e)//链表插入    
  11. {    
  12.     int j=0;    
  13.     
  14.     Linklist *p=L,*s;    
  15.     
  16.     if(i<=0)//输入的i比0小不合法    
  17.     {    
  18.         return false;    
  19.     
  20.     }    
  21.     
  22.     while(j<i-1 && p!=NULL)//遍历    
  23.     {    
  24.         j++;    
  25.     
  26.         p=p->next;    
  27.     }    
  28.     
  29.     if(p==NULL)//未找到i-1位置结点    
  30.     {    
  31.         return false;    
  32.     }    
  33.     
  34.     else//找到    
  35.     {    
  36.     
  37.         s=(Linklist *)malloc(sizeof(Linklist));    
  38.     
  39.         s->data=e;    
  40.     
  41.         s->next=p->next;    
  42.     
  43.         p->next=s;//插入操作    
  44.     
  45.         return true;    
  46.     }    
  47. }    
  48.     
  49. void DispList(Linklist *L)//输出链表的元素值    
  50. {    
  51.     Linklist*p=L->next;    
  52.     
  53.     while(p!=NULL)//输出+遍历    
  54.     {    
  55.         printf("%d ",p->data);    
  56.     
  57.         p=p->next;    
  58.     
  59.     }    
  60.     
  61.     printf("\n");    
  62. }    
  63.     
  64.     
  65. void  DestroyList(Linklist *&L)//销毁链表    
  66. {    
  67.     Linklist *pre=L,*p=L->next;    
  68.     
  69.     while(p!=NULL)    
  70.     {    
  71.         free(pre);    
  72.     
  73.         pre=p;    
  74.     
  75.         p=pre->next;    
  76.     }    
  77.     free(pre);    
  78. }    
  79. void  CreateListF(Linklist *&L,ElemType a[],int n)    
  80. {    
  81.     Linklist *s;    
  82.     
  83.     L=(Linklist *)malloc(sizeof(Linklist));//动态开辟内存空间    
  84.     
  85.     L->next=NULL;    
  86.     
  87.     for(int i=0;i<n;i++)//头插法    
  88.     {    
  89.         s=(Linklist *)malloc(sizeof(Linklist));    
  90.         s->data=a[i];    
  91.         s->next=L->next;    
  92.         L->next=s;    
  93.     }    
  94. }    
  95.     
  96. void  CreateListR(Linklist *&L,ElemType a[],int n)//尾插法    
  97. {    
  98.      Linklist *s,*r;    
  99.     
  100.      L=(Linklist *)malloc(sizeof(Linklist));    
  101.     
  102.      r=L;    
  103.     
  104.      for(int i=0;i<n;i++)//尾插法    
  105.      {    
  106.          s=(Linklist *)malloc(sizeof(Linklist));    
  107.     
  108.          s->data=a[i];    
  109.     
  110.          r->next=s;    
  111.     
  112.          r=s;    
  113.     
  114.      }    
  115.     r->next=NULL;    
  116. }    
  117.     
  118.     
  119.     
  120. bool ListDelete(Linklist *&L,int i,ElemType &e)//删除链表元素    
  121. {    
  122.     
  123.     int j=0;    
  124.     
  125.     Linklist *p=L,*q;    
  126.     
  127.     if(i<=0)return false;    
  128.     
  129.     while(j<i-1 && p!=NULL)//找到i的前一节点i-1    
  130.     {    
  131.         j++;    
  132.     
  133.         p=p->next;    
  134.     }    
  135.     if(p==NULL)//p为空,未找到元素    
  136.     {    
  137.         return false;    
  138.     }    
  139.     
  140.     else    
  141.     {    
  142.         q=p->next;    
  143.         if(q==NULL)//未找到元素    
  144.         {    
  145.             return false;    
  146.         }    
  147.         e=q->data;//删除的元素保留到e    
  148.     
  149.         p->next=q->next;    
  150.     
  151.         free(q);    
  152.     
  153.         return true;    
  154.     }    
  155. }    
  156.     
      main函数:

     

[cpp] view plain copy
print?
  1. #include <stdio.h>    
  2. #include <malloc.h>    
  3. #include "list.h"    
  4.     
  5.     
  6. int main()    
  7. {    
  8.     Linklist *L1,*L2;    
  9.     
  10.     ElemType a[8]={7,9,8,2,0,4,6,3};    
  11.     
  12.     printf("头插法建表结果:");    
  13.     
  14.     CreateListF(L1,a,8);    
  15.     
  16.     DispList(L1);    
  17.     
  18.     printf("尾插法建表结果:");    
  19.     
  20.     CreateListR(L2,a,8);    
  21.     
  22.     DispList(L2);    
  23.     
  24.     DestoryList(L1);    
  25.     
  26.     DestoryList(L2);    
  27.     
  28.     int b;    
  29.     
  30.     Linklist *L3;    
  31.     
  32.     CreateListR(L3,a,8);    
  33.     
  34.     ListDelete(L3,4,b);    
  35.     
  36.     printf("删除a数组中的元素:");    
  37.     printf("%d\n",b);    
  38.     DispList(L3);    
  39.     
  40.     
  41.     
  42.     
  43.     
  44.     
  45.     
  46.   printf("插入验证:");    
  47.   Linklist*L;    
  48.   initList(L);    
  49.   ListInsert(L,1,15);    
  50.   ListInsert(L,1,10);    
  51.   ListInsert(L,1,5);    
  52.   ListInsert(L,1,20);    
  53.   DispList(L);    
  54.   DestroyList(L);    
  55.  return 0;    
  56. }    
[cpp] view plain copy
print?
  1.   
[cpp] view plain copy
print?
  1.   
[cpp] view plain copy
print?
  1.   
[cpp] view plain copy
print?
  1. list.h  :  
[cpp] view plain copy
print?
  1.   
[cpp] view plain copy
print?
  1. <pre class="cpp" name="code">#include <stdio.h>    
  2. #include <malloc.h>    
  3. #include <stdio.h>    
  4. #include <malloc.h>    
  5. typedef int ElemType;    
  6.     
  7. typedef struct LNode    
  8. {    
  9.     ElemType data;    
  10.     
  11.     struct LNode *next;    
  12. }Linklist;    
  13. void initList(Linklist *&L);//初始化链表    
  14. bool ListInsert(Linklist *&L,int i,ElemType e);//链表插入    
  15. void DispList(Linklist *L);//输出    
  16. void  DestroyList(Linklist *&L);//销毁    
  17. void  CreateListF(Linklist *&L,ElemType a[],int n);//头插法    
  18. void  CreateListR(Linklist *&L,ElemType a[],int n);//尾插法    
  19. bool ListDelete(Linklist *&L,int i,ElemType &e);//元素删除  </pre><br>  
  20. <p></p>  
  21. <pre></pre>  
  22. <pre class="cpp" name="code">   运行结果:</pre><pre class="cpp" name="code">  </pre><pre class="cpp" name="code">       <img alt="" src="http://img.blog.csdn.net/20170926205758728?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGZlbEw=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast"></pre><pre class="cpp" name="code"></pre><pre class="cpp" name="code"></pre><pre class="cpp" name="code"><pre class="cpp" name="code">学习心得:</pre><br>  
  23. <pre></pre>  
  24. <pre class="cpp" name="code">   通过这次的训练,<span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">学会了建立链表算法库,感觉好开心</span></pre><pre class="cpp" name="code">    </pre><pre class="cpp" name="code">      
  25.   </pre><br>  
  26. <p></p>  
  27.      
  28. </pre>  
原创粉丝点击