第四周项目二

来源:互联网 发布:剑网三重制版 知乎 编辑:程序博客网 时间:2024/05/16 12:25
  1. /*  
  2. 烟台大学计算机学院  
  3.   
  4. 文件名称:xm.cpp  
  5.   
  6. 作者:刘照京  
  7.   
  8. 完成日期:2017年10月11日  
  9.   
  10. 问题描述:建立单链表算法库 
  11.   
  12. 输入描述:无 
  13.   
  14. 输出描述:进行了各个的链表的值 
  15.   
  16. */   
  17.   
  18.   
  19.   
  20. list.cpp:  
  21.   
  22.   
  23.   
  24. #include <stdio.h>  
  25. #include <malloc.h>  
  26. #include "list.h"  
  27. void initList(Linklist *&L)//初始化链表  
  28. {  
  29.     L=(Linklist *)malloc(sizeof(Linklist));//动态开辟内存  
  30.     L->next=NULL;//链表为空  
  31. }  
  32.   
  33. bool ListInsert(Linklist *&L,int i,ElemType e)//链表插入  
  34. {  
  35.     int j=0;  
  36.   
  37.     Linklist *p=L,*s;  
  38.   
  39.     if(i<=0)//输入的i比0小不合法  
  40.     {  
  41.         return false;  
  42.   
  43.     }  
  44.   
  45.     while(j<i-1 && p!=NULL)//遍历  
  46.     {  
  47.         j++;  
  48.   
  49.         p=p->next;  
  50.     }  
  51.   
  52.     if(p==NULL)//未找到i-1位置结点  
  53.     {  
  54.         return false;  
  55.     }  
  56.   
  57.     else//找到  
  58.     {  
  59.   
  60.         s=(Linklist *)malloc(sizeof(Linklist));  
  61.   
  62.         s->data=e;  
  63.   
  64.         s->next=p->next;  
  65.   
  66.         p->next=s;//插入操作  
  67.   
  68.         return true;  
  69.     }  
  70. }  
  71.   
  72. void DispList(Linklist *L)//输出链表的元素值  
  73. {  
  74.     Linklist*p=L->next;  
  75.   
  76.     while(p!=NULL)//输出+遍历  
  77.     {  
  78.         printf("%d ",p->data);  
  79.   
  80.         p=p->next;  
  81.   
  82.     }  
  83.   
  84.     printf("\n");  
  85. }  
  86.   
  87.   
  88. void  DestroyList(Linklist *&L)//销毁链表  
  89. {  
  90.     Linklist *pre=L,*p=L->next;  
  91.   
  92.     while(p!=NULL)  
  93.     {  
  94.         free(pre);  
  95.   
  96.         pre=p;  
  97.   
  98.         p=pre->next;  
  99.     }  
  100.     free(pre);  
  101. }  
  102. void  CreateListF(Linklist *&L,ElemType a[],int n)  
  103. {  
  104.     Linklist *s;  
  105.   
  106.     L=(Linklist *)malloc(sizeof(Linklist));//动态开辟内存空间  
  107.   
  108.     L->next=NULL;  
  109.   
  110.     for(int i=0;i<n;i++)//头插法  
  111.     {  
  112.         s=(Linklist *)malloc(sizeof(Linklist));  
  113.         s->data=a[i];  
  114.         s->next=L->next;  
  115.         L->next=s;  
  116.     }  
  117. }  
  118.   
  119. void  CreateListR(Linklist *&L,ElemType a[],int n)//尾插法  
  120. {  
  121.      Linklist *s,*r;  
  122.   
  123.      L=(Linklist *)malloc(sizeof(Linklist));  
  124.   
  125.      r=L;  
  126.   
  127.      for(int i=0;i<n;i++)//尾插法  
  128.      {  
  129.          s=(Linklist *)malloc(sizeof(Linklist));  
  130.   
  131.          s->data=a[i];  
  132.   
  133.          r->next=s;  
  134.   
  135.          r=s;  
  136.   
  137.      }  
  138.     r->next=NULL;  
  139. }  
  140.   
  141.   
  142.   
  143. bool ListDelete(Linklist *&L,int i,ElemType &e)//删除链表元素  
  144. {  
  145.   
  146.     int j=0;  
  147.   
  148.     Linklist *p=L,*q;  
  149.   
  150.     if(i<=0)return false;  
  151.   
  152.     while(j<i-1 && p!=NULL)//找到i的前一节点i-1  
  153.     {  
  154.         j++;  
  155.   
  156.         p=p->next;  
  157.     }  
  158.     if(p==NULL)//p为空,未找到元素  
  159.     {  
  160.         return false;  
  161.     }  
  162.   
  163.     else  
  164.     {  
  165.         q=p->next;  
  166.         if(q==NULL)//未找到元素  
  167.         {  
  168.             return false;  
  169.         }  
  170.         e=q->data;//删除的元素保留到e  
  171.   
  172.         p->next=q->next;  
  173.   
  174.         free(q);  
  175.   
  176.         return true;  
  177.     }  
  178. }  
  179.   
  180.   
  181.   
  182. main:  
  183.   
  184. #include <stdio.h>  
  185. #include <malloc.h>  
  186. #include "list.h"  
  187.   
  188.   
  189. int main()  
  190. {  
  191.     Linklist *L1,*L2;  
  192.   
  193.     ElemType a[8]={7,9,8,2,0,4,6,3};  
  194.   
  195.     printf("头插法建表结果:");  
  196.   
  197.     CreateListF(L1,a,8);  
  198.   
  199.     DispList(L1);  
  200.   
  201.     printf("尾插法建表结果:");  
  202.   
  203.     CreateListR(L2,a,8);  
  204.   
  205.     DispList(L2);  
  206.   
  207.     DestoryList(L1);  
  208.   
  209.     DestoryList(L2);  
  210.   
  211.     int b;  
  212.   
  213.     Linklist *L3;  
  214.   
  215.     CreateListR(L3,a,8);  
  216.   
  217.     ListDelete(L3,4,b);  
  218.   
  219.     printf("删除a数组中的元素:");  
  220.     printf("%d\n",b);  
  221.     DispList(L3);  
  222.   
  223.   
  224.   
  225.   
  226.   
  227.   
  228.   
  229.   printf("插入验证:");  
  230.   Linklist*L;  
  231.   initList(L);  
  232.   ListInsert(L,1,15);  
  233.   ListInsert(L,1,10);  
  234.   ListInsert(L,1,5);  
  235.   ListInsert(L,1,20);  
  236.   DispList(L);  
  237.   DestroyList(L);  
  238.  return 0;  
  239. }  
  240.   
  241. list.h:  
  242.   
  243. #include <stdio.h>  
  244. #include <malloc.h>  
  245. #include <stdio.h>  
  246. #include <malloc.h>  
  247. typedef int ElemType;  
  248.   
  249. typedef struct LNode  
  250. {  
  251.     ElemType data;  
  252.   
  253.     struct LNode *next;  
  254. }Linklist;  
  255. void initList(Linklist *&L);//初始化链表  
  256. bool ListInsert(Linklist *&L,int i,ElemType e);//链表插入  
  257. void DispList(Linklist *L);//输出  
  258. void  DestroyList(Linklist *&L);//销毁  
  259. void  CreateListF(Linklist *&L,ElemType a[],int n);//头插法  
  260. void  CreateListR(Linklist *&L,ElemType a[],int n);//尾插法  
  261. bool ListDelete(Linklist *&L,int i,ElemType &e);//元素删除  

运行结果:

原创粉丝点击