第三周 项目2-建设“顺序表”算法库

来源:互联网 发布:zabbix windows 安装 编辑:程序博客网 时间:2024/05/21 09:18
  1. /*  
  2. *Copyright (c) 2017,烟台大学计算机与控制工程学院  
  3. *All rights reserved.  
  4. *文件名称:  
  5. *作    者:刘浩
  6. *完成日期:2017年10月12日  
  7. *版 本 号:v1.0  
  8.  
  9. *问题描述: 
  10. */    
    [html] view plain copy
    1. #ifndef LIST_H_INCLUDED  
    2. #define LIST_H_INCLUDED  
    3.   
    4. #define MaxSize 50  
    5. typedef int ElemType;<pre name="code" class="cpp">#include "list.h"  
    6. #include <stdio.h>  
    7. #include <malloc.h>  
    8. void CreateList(SqList *&L, ElemType a[], int n)  
    9. {  
    10.     int i;  
    11.     L=(SqList *)malloc(sizeof(SqList));  
    12.     for (i=0; i<n; i++)  
    13.         L->data[i]=a[i];  
    14.     L->length=n;  
    15. }  
    16.   
    17. //初始化线性表InitList(L)  
    18. void InitList(SqList *&L)   //引用型指针  
    19. {  
    20.     L=(SqList *)malloc(sizeof(SqList));  
    21.     //分配存放线性表的空间  
    22.     L->length=0;  
    23. }  
    24.   
    25. //销毁线性表DestroyList(L)  
    26. void DestroyList(SqList *&L)  
    27. {  
    28.     free(L);  
    29. }  
    30.   
    31. //判定是否为空表ListEmpty(L)  
    32. bool ListEmpty(SqList *L)  
    33. {  
    34.     return(L->length==0);  
    35. }  
    36.   
    37. //求线性表的长度ListLength(L)  
    38. int ListLength(SqList *L)  
    39. {  
    40.     return(L->length);  
    41. }  
    42.   
    43. //输出线性表DispList(L)  
    44. void DispList(SqList *L)  
    45. {  
    46.     int i;  
    47.     if (ListEmpty(L)) return;  
    48.     for (i=0; i<L->length; i++)  
    49.         printf("%d ",L->data[i]);  
    50.     printf("\n");  
    51. }  
    52.   
    53. //求某个数据元素值GetElem(L,i,e)  
    54. bool GetElem(SqList *L,int i,ElemType &e)  
    55. {  
    56.     if (i<1 || i>L->length)  return false;  
    57.     e=L->data[i-1];  
    58.     return true;  
    59. }  
    60.   
    61. //按元素值查找LocateElem(L,e)  
    62. int LocateElem(SqList *L, ElemType e)  
    63. {  
    64.     int i=0;  
    65.     while (i<L->length && L->data[i]!=e) i++;  
    66.     if (i>=L->length)  return 0;  
    67.     else  return i+1;  
    68. }  
    69.   
    70. //插入数据元素ListInsert(L,i,e)  
    71. bool ListInsert(SqList *&L,int i,ElemType e)  
    72. {  
    73.     int j;  
    74.     if (i<1 || i>L->length+1)  
    75.         return false;   //参数错误时返回false  
    76.     i--;            //将顺序表逻辑序号转化为物理序号  
    77.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置  
    78.         L->data[j]=L->data[j-1];  
    79.     L->data[i]=e;           //插入元素e  
    80.     L->length++;            //顺序表长度增1  
    81.     return true;            //成功插入返回true  
    82. }  
    83.   
    84. //删除数据元素ListDelete(L,i,e)  
    85. bool ListDelete(SqList *&L,int i,ElemType &e)  
    86. {  
    87.     int j;  
    88.     if (i<1 || i>L->length)  //参数错误时返回false  
    89.         return false;  
    90.     i--;        //将顺序表逻辑序号转化为物理序号  
    91.     e=L->data[i];  
    92.     for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移  
    93.         L->data[j]=L->data[j+1];  
    94.     L->length--;              //顺序表长度减1  
    95.     return true;              //成功删除返回true  
    96. }  
    97.   
    98. int main()  
    99. {  
    100.     SqList *sq;  
    101.     ElemType x[6]= {5,8,7,2,4,9};  
    102.     CreateList(sq, x, 6);  
    103.     DispList(sq);  
    104.     return 0;  
    105. }  
    106. </pre><br>  
    107. typedef struct{ ElemType data[MaxSize]; int length;} SqList;void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表void InitList(SqList *&L);//初始化线性表InitList(L)void DestroyList(SqList *&L);//销毁线性表DestroyList(L)bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)int  
    108.  ListLength(SqList *L);//求线性表的长度ListLength(L)void DispList(SqList *L);//输出线性表DispList(L)bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)bool ListInsert(SqList *&L,int i,ElemType  
    109.  e);//插入数据元素ListInsert(L,i,e)bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)#endif // LIST_H_INCLUDED#endif  
    110. <pre></pre>  
    111. <br>