第四周项目二

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