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

来源:互联网 发布:软件光盘品牌 编辑:程序博客网 时间:2024/05/29 16:32

/*   

* 烟台大学计算机与控制工程学院   

* 作者:王雪松 

* 完成日期:2016年9月22日   

* 问题及代码

* 问题描述:算法库包括两个文件:头文件:list.h,包含定义顺序表数据结构的代码、宏定义、要实现算法的函数的声明;源文件:list.cpp,包含实现各种算法的函数的定义请采用程序的多文件组织形式,在项目1的基础上,建立如上的两个文件,另外再建立一个源文件,编制main函数,完成相关的测试工作。

*代码:

  1. //list.h  
  2. #ifndef LIST_H_INCLUDED  
  3. #define LIST_H_INCLUDED  
  4.   
  5. #define MaxSize 50  
  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  

 

[cpp] view plain copy
  1. //list.cpp  
  2. #include <stdio.h>  
  3. #include <malloc.h>  
  4. #include "list.h"  
  5.   
  6. //用数组创建线性表  
  7. void CreateList(SqList *&L, ElemType a[], int n)  
  8. {  
  9.     int i;  
  10.     L=(SqList *)malloc(sizeof(SqList));  
  11.     for (i=0; i<n; i++)  
  12.         L->data[i]=a[i];  
  13.     L->length=n;  
  14. }  
  15.   
  16. //初始化线性表InitList(L)  
  17. void InitList(SqList *&L)   //引用型指针  
  18. {  
  19.     L=(SqList *)malloc(sizeof(SqList));  
  20.     //分配存放线性表的空间  
  21.     L->length=0;  
  22. }  
  23.   
  24. //销毁线性表DestroyList(L)  
  25. void DestroyList(SqList *&L)  
  26. {  
  27.     free(L);  
  28. }  
  29.   
  30. //判定是否为空表ListEmpty(L)  
  31. bool ListEmpty(SqList *L)  
  32. {  
  33.     return(L->length==0);  
  34. }  
  35.   
  36. //求线性表的长度ListLength(L)  
  37. int ListLength(SqList *L)  
  38. {  
  39.     return(L->length);  
  40. }  
  41.   
  42. //输出线性表DispList(L)  
  43. void DispList(SqList *L)  
  44. {  
  45.     int i;  
  46.     if (ListEmpty(L)) return;  
  47.     for (i=0; i<L->length; i++)  
  48.         printf("%d ",L->data[i]);  
  49.     printf("\n");  
  50. }  
  51.   
  52. //求某个数据元素值GetElem(L,i,e)  
  53. bool GetElem(SqList *L,int i,ElemType &e)  
  54. {  
  55.     if (i<1 || i>L->length)  return false;  
  56.     e=L->data[i-1];  
  57.     return true;  
  58. }  
  59.   
  60. //按元素值查找LocateElem(L,e)  
  61.   
  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. }  
[cpp] view plain copy
  1. //main.cpp  
  2. #include "list.h"  
  3. int main()  
  4. {  
  5.     SqList *sq;  
  6.     ElemType x[6]= {5,8,7,2,4,9};  
  7.     CreateList(sq, x, 6);  
  8.     DispList(sq);  
  9.     return 0;  
  10. }  

 

运行结果:

学习总结:

          加深了对项目一的理解,对于较大程序的处理,以及程序的多文件组织的处理。


l
i
s
t
.
h
l
i
s
t
.
c
p
p
1
m
a
i
n

l
i
s
t
.
h
l
i
s
t
.
c
p
p
1
m
a
i
n

0 0