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

来源:互联网 发布:自己写小说的软件 编辑:程序博客网 时间:2024/05/22 00:33
  1. *Copyright (c)2016,烟台大学计算机与控制工程学院   
  2. *All rights reserved.   
  3. *文件名称:项目2.cbp   
  4. *作    者:陈鹏鹏  
  5. *完成日期:2016年9月18日   
  6. *版 本 号:v1.0   
  7. *问题描述:请采用程序的多文件组织形式,在项目1的基础上,建立 
  8.            如上的两个文件,另外再建立一个源文件,编制main函 
  9.            数,完成相关的测试工作。   
  10. *输入描述:无   
  11. *程序输出:依据各个函数而定  
  12. */   
    • list.h文件代码
    [cpp] view plain copy
    1. #ifndef LIST_H_INCLUDED  
    2. #define LIST_H_INCLUDED  
    3.   
    4. #define MaxSize 50  
    5. #include <stdio.h>  
    6. #include <malloc.h>  
    7. typedef int ElemType;  
    8. typedef struct  
    9. {  
    10.     ElemType data[MaxSize];  
    11.     int length;  
    12. } SqList;  
    13. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
    14. void InitList(SqList *&L);//初始化线性表InitList(L)  
    15. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)  
    16. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
    17. int ListLength(SqList *L);//求线性表的长度ListLength(L)  
    18. void DispList(SqList *L);//输出线性表DispList(L)  
    19. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)  
    20. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)  
    21. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)  
    22. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)#endif // LIST_H_INCLUDED  
    23. #endif  
    list.h是顺序表的一个算法库集合,里面声明了常用到的各个功能函数。
    • list.cpp文件代码
    [cpp] view plain copy
    1. #include "list.h"  
    2.   
    3. //用数组创建线性表  
    4. void CreateList(SqList *&L, ElemType a[], int n)  
    5. {  
    6.     int i;  
    7.     L=(SqList *)malloc(sizeof(SqList));  
    8.     for (i=0; i<n; i++)  
    9.         L->data[i]=a[i];  
    10.     L->length=n;  
    11. }  
    12.   
    13. //初始化线性表InitList(L)  
    14. void InitList(SqList *&L)   //引用型指针  
    15. {  
    16.     L=(SqList *)malloc(sizeof(SqList));  
    17.     //分配存放线性表的空间  
    18.     L->length=0;  
    19. }  
    20.   
    21. //销毁线性表DestroyList(L)  
    22. void DestroyList(SqList *&L)  
    23. {  
    24.     free(L);  
    25. }  
    26.   
    27. //判定是否为空表ListEmpty(L)  
    28. bool ListEmpty(SqList *L)  
    29. {  
    30.     return(L->length==0);  
    31. }  
    32.   
    33. //求线性表的长度ListLength(L)  
    34. int ListLength(SqList *L)  
    35. {  
    36.     return(L->length);  
    37. }  
    38.   
    39. //输出线性表DispList(L)  
    40. void DispList(SqList *L)  
    41. {  
    42.     int i;  
    43.     if (ListEmpty(L)) return;  
    44.     for (i=0; i<L->length; i++)  
    45.         printf("%d ",L->data[i]);  
    46.     printf("\n");  
    47. }  
    48.   
    49. //求某个数据元素值GetElem(L,i,e)  
    50. bool GetElem(SqList *L,int i,ElemType &e)  
    51. {  
    52.     if (i<1 || i>L->length)  return false;  
    53.     e=L->data[i-1];  
    54.     return true;  
    55. }  
    56.   
    57. //按元素值查找LocateElem(L,e)  
    58. int LocateElem(SqList *L, ElemType e)  
    59. {  
    60.     int i=0;  
    61.     while (i<L->length && L->data[i]!=e) i++;  
    62.     if (i>=L->length)  return 0;  
    63.     else  return i+1;  
    64. }  
    65.   
    66. //插入数据元素ListInsert(L,i,e)  
    67. bool ListInsert(SqList *&L,int i,ElemType e)  
    68. {  
    69.     int j;  
    70.     if (i<1 || i>L->length+1)  
    71.         return false;   //参数错误时返回false  
    72.     i--;            //将顺序表逻辑序号转化为物理序号  
    73.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置  
    74.         L->data[j]=L->data[j-1];  
    75.     L->data[i]=e;           //插入元素e  
    76.     L->length++;            //顺序表长度增1  
    77.     return true;            //成功插入返回true  
    78. }  
    79.   
    80. //删除数据元素ListDelete(L,i,e)  
    81. bool ListDelete(SqList *&L,int i,ElemType &e)  
    82. {  
    83.     int j;  
    84.     if (i<1 || i>L->length)  //参数错误时返回false  
    85.         return false;  
    86.     i--;        //将顺序表逻辑序号转化为物理序号  
    87.     e=L->data[i];  
    88.     for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移  
    89.         L->data[j]=L->data[j+1];  
    90.     L->length--;              //顺序表长度减1  
    91.     return true;              //成功删除返回true  
    92. }  
    list.cpp对应list.h中声明的各个功能函数,给出了各个功能函数的实现方法。
    • main.cpp文件代码
    [cpp] view plain copy
    1. #include "list.h"  
    2. int main()  
    3. {  
    4.     SqList *sq;  
    5.     ElemType E;  
    6.   
    7.     printf("初始化线性表\n");  
    8.     InitList(sq);  
    9.   
    10.     printf("在第1个位置插入元素1\n");  
    11.     ListInsert(sq, 1, 1);  
    12.     DispList(sq);  
    13.   
    14.     printf("在第2个位置插入元素6\n");  
    15.     ListInsert(sq, 2, 6);  
    16.     DispList(sq);  
    17.   
    18.   
    19.     printf("在第1个位置插入元素9\n");  
    20.     ListInsert(sq, 1, 9);  
    21.     DispList(sq);  
    22.   
    23.     printf("删除第2个位置的元素\n");  
    24.     ListDelete(sq,2,E);  
    25.     DispList(sq);  
    26.   
    27.   
    28.     printf("销毁线性表\n");  
    29.     DestroyList(sq);  
    30.     DispList(sq);  
    31.   
    32. }  
    main.cpp中根据需要添加各个函数,以便实现相应功能。

    运行结果:
     
0 0
原创粉丝点击