第四周 建立单链表算法库

  1. 烟台大学计算机学院   
  2.    
  3. 文件名称:xm.cpp   
  4.    
  5. 作者:李浩南
  6.    
  7. 完成日期:2017年9月26日   
  8.     
  9.    
  10. 输入描述:无  
  11.    
  12. 输出描述:进行了各个的链表的值  
  13.    
  14. */     
  15. /*  
    烟台大学计算机学院  
      
    文件名称:xm.cpp  
      
    作者:范宝磊
      
    完成日期:2017年9月24日  
      
    问题描述:建立单链表算法库 
      
    输入描述:无 
      
    输出描述:进行了各个的链表的值 
      
    */   

list.cpp

 

  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.     
#include <stdio.h>  
#include <malloc.h>  
#include "list.h"  
void initList(Linklist *&L)//初始化链表  
{  
    L=(Linklist *)malloc(sizeof(Linklist));//动态开辟内存  
    L->next=NULL;//链表为空  
}  
  
bool ListInsert(Linklist *&L,int i,ElemType e)//链表插入  
{  
    int j=0;  
  
    Linklist *p=L,*s;  
  
    if(i<=0)//输入的i比0小不合法  
    {  
        return false;  
  
    }  
  
    while(j<i-1 && p!=NULL)//遍历  
    {  
        j++;  
  
        p=p->next;  
    }  
  
    if(p==NULL)//未找到i-1位置结点  
    {  
        return false;  
    }  
  
    else//找到  
    {  
  
        s=(Linklist *)malloc(sizeof(Linklist));  
  
        s->data=e;  
  
        s->next=p->next;  
  
        p->next=s;//插入操作  
  
        return true;  
    }  
}  
  
void DispList(Linklist *L)//输出链表的元素值  
{  
    Linklist*p=L->next;  
  
    while(p!=NULL)//输出+遍历  
    {  
        printf("%d ",p->data);  
  
        p=p->next;  
  
    }  
  
    printf("\n");  
}  
  
  
void  DestroyList(Linklist *&L)//销毁链表  
{  
    Linklist *pre=L,*p=L->next;  
  
    while(p!=NULL)  
    {  
        free(pre);  
  
        pre=p;  
  
        p=pre->next;  
    }  
    free(pre);  
}  
void  CreateListF(Linklist *&L,ElemType a[],int n)  
{  
    Linklist *s;  
  
    L=(Linklist *)malloc(sizeof(Linklist));//动态开辟内存空间  
  
    L->next=NULL;  
  
    for(int i=0;i<n;i++)//头插法  
    {  
        s=(Linklist *)malloc(sizeof(Linklist));  
        s->data=a[i];  
        s->next=L->next;  
        L->next=s;  
    }  
}  
  
void  CreateListR(Linklist *&L,ElemType a[],int n)//尾插法  
{  
     Linklist *s,*r;  
  
     L=(Linklist *)malloc(sizeof(Linklist));  
  
     r=L;  
  
     for(int i=0;i<n;i++)//尾插法  
     {  
         s=(Linklist *)malloc(sizeof(Linklist));  
  
         s->data=a[i];  
  
         r->next=s;  
  
         r=s;  
  
     }  
    r->next=NULL;  
}  
  
  
  
bool ListDelete(Linklist *&L,int i,ElemType &e)//删除链表元素  
{  
  
    int j=0;  
  
    Linklist *p=L,*q;  
  
    if(i<=0)return false;  
  
    while(j<i-1 && p!=NULL)//找到i的前一节点i-1  
    {  
        j++;  
  
        p=p->next;  
    }  
    if(p==NULL)//p为空,未找到元素  
    {  
        return false;  
    }  
  
    else  
    {  
        q=p->next;  
        if(q==NULL)//未找到元素  
        {  
            return false;  
        }  
        e=q->data;//删除的元素保留到e  
  
        p->next=q->next;  
  
        free(q);  
  
        return true;  
    }  
}  
  
      main函数:

     

  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. }    
#include <stdio.h>  
#include <malloc.h>  
#include "list.h"  
  
  
int main()  
{  
    Linklist *L1,*L2;  
  
    ElemType a[8]={7,9,8,2,0,4,6,3};  
  
    printf("头插法建表结果:");  
  
    CreateListF(L1,a,8);  
  
    DispList(L1);  
  
    printf("尾插法建表结果:");  
  
    CreateListR(L2,a,8);  
  
    DispList(L2);  
  
    DestoryList(L1);  
  
    DestoryList(L2);  
  
    int b;  
  
    Linklist *L3;  
  
    CreateListR(L3,a,8);  
  
    ListDelete(L3,4,b);  
  
    printf("删除a数组中的元素:");  
    printf("%d\n",b);  
    DispList(L3);  
  
  
  
  
  
  
  
  printf("插入验证:");  
  Linklist*L;  
  initList(L);  
  ListInsert(L,1,15);  
  ListInsert(L,1,10);  
  ListInsert(L,1,5);  
  ListInsert(L,1,20);  
  DispList(L);  
  DestroyList(L);  
 return 0;  
}  
  1.   
  1. list.h  :  
 
  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="https://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>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值