天天看点

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

  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. #define MaxSize 50  
    4. #include <stdio.h>  
    5. #include <malloc.h>  
    6. typedef int ElemType;  
    7. typedef struct  
    8. {  
    9.     ElemType data[MaxSize];  
    10.     int length;  
    11. } SqList;  
    12. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
    13. void InitList(SqList *&L);//初始化线性表InitList(L)  
    14. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)  
    15. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
    16. int ListLength(SqList *L);//求线性表的长度ListLength(L)  
    17. void DispList(SqList *L);//输出线性表DispList(L)  
    18. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)  
    19. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)  
    20. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)  
    21. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)#endif // LIST_H_INCLUDED  
    22. #endif  
    list.h是顺序表的一个算法库集合,里面声明了常用到的各个功能函数。
    • list.cpp文件代码
    [cpp]  view plain  copy
    1. #include "list.h"  
    2. //用数组创建线性表  
    3. void CreateList(SqList *&L, ElemType a[], int n)  
    4. {  
    5.     int i;  
    6.     L=(SqList *)malloc(sizeof(SqList));  
    7.     for (i=0; i<n; i++)  
    8.         L->data[i]=a[i];  
    9.     L->length=n;  
    10. }  
    11. //初始化线性表InitList(L)  
    12. void InitList(SqList *&L)   //引用型指针  
    13. {  
    14.     L=(SqList *)malloc(sizeof(SqList));  
    15.     //分配存放线性表的空间  
    16.     L->length=0;  
    17. }  
    18. //销毁线性表DestroyList(L)  
    19. void DestroyList(SqList *&L)  
    20. {  
    21.     free(L);  
    22. }  
    23. //判定是否为空表ListEmpty(L)  
    24. bool ListEmpty(SqList *L)  
    25. {  
    26.     return(L->length==0);  
    27. }  
    28. //求线性表的长度ListLength(L)  
    29. int ListLength(SqList *L)  
    30. {  
    31.     return(L->length);  
    32. }  
    33. //输出线性表DispList(L)  
    34. void DispList(SqList *L)  
    35. {  
    36.     int i;  
    37.     if (ListEmpty(L)) return;  
    38.     for (i=0; i<L->length; i++)  
    39.         printf("%d ",L->data[i]);  
    40.     printf("\n");  
    41. }  
    42. //求某个数据元素值GetElem(L,i,e)  
    43. bool GetElem(SqList *L,int i,ElemType &e)  
    44. {  
    45.     if (i<1 || i>L->length)  return false;  
    46.     e=L->data[i-1];  
    47.     return true;  
    48. }  
    49. //按元素值查找LocateElem(L,e)  
    50. int LocateElem(SqList *L, ElemType e)  
    51. {  
    52.     int i=0;  
    53.     while (i<L->length && L->data[i]!=e) i++;  
    54.     if (i>=L->length)  return 0;  
    55.     else  return i+1;  
    56. }  
    57. //插入数据元素ListInsert(L,i,e)  
    58. bool ListInsert(SqList *&L,int i,ElemType e)  
    59. {  
    60.     int j;  
    61.     if (i<1 || i>L->length+1)  
    62.         return false;   //参数错误时返回false  
    63.     i--;            //将顺序表逻辑序号转化为物理序号  
    64.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置  
    65.         L->data[j]=L->data[j-1];  
    66.     L->data[i]=e;           //插入元素e  
    67.     L->length++;            //顺序表长度增1  
    68.     return true;            //成功插入返回true  
    69. }  
    70. //删除数据元素ListDelete(L,i,e)  
    71. bool ListDelete(SqList *&L,int i,ElemType &e)  
    72. {  
    73.     int j;  
    74.     if (i<1 || i>L->length)  //参数错误时返回false  
    75.         return false;  
    76.     i--;        //将顺序表逻辑序号转化为物理序号  
    77.     e=L->data[i];  
    78.     for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移  
    79.         L->data[j]=L->data[j+1];  
    80.     L->length--;              //顺序表长度减1  
    81.     return true;              //成功删除返回true  
    82. }  
    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.     printf("初始化线性表\n");  
    7.     InitList(sq);  
    8.     printf("在第1个位置插入元素1\n");  
    9.     ListInsert(sq, 1, 1);  
    10.     DispList(sq);  
    11.     printf("在第2个位置插入元素6\n");  
    12.     ListInsert(sq, 2, 6);  
    13.     DispList(sq);  
    14.     printf("在第1个位置插入元素9\n");  
    15.     ListInsert(sq, 1, 9);  
    16.     DispList(sq);  
    17.     printf("删除第2个位置的元素\n");  
    18.     ListDelete(sq,2,E);  
    19.     DispList(sq);  
    20.     printf("销毁线性表\n");  
    21.     DestroyList(sq);  
    22.     DispList(sq);  
    23. }  

    main.cpp中根据需要添加各个函数,以便实现相应功能。

    运行结果:

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