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

来源:互联网 发布:mac 显卡驱动 编辑:程序博客网 时间:2024/05/17 17:40

问题及代码:

  1. * Copyright (c)2016,烟台大学计算机与控制工程学院  
  2. * All rights reserved.  
  3. * 文件名称:项目2.cpp  
  4. * 作    者:陈哲  
  5. * 完成日期:2016年9月17日  
  6. * 版 本 号:v1.0   
  7. *问题描述:请采用程序的多文件组织形式,在项目1的基础上,建立  
  8.            如上的两个文件,另外再建立一个源文件,编制main函  
  9.            数,完成相关的测试工作。    
  10. *输入描述:无    
  11. *程序输出:依据各个函数而定 

头文件:list.h

  1. #define MaxSize 50    
  2. #include <stdio.h>    
  3. #include <malloc.h>    
  4. typedef int ElemType;    
  5. typedef struct    
  6. {    
  7.     ElemType data[MaxSize];    
  8.     int length;    
  9. } SqList;    
  10. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表    
  11. void InitList(SqList *&L);//初始化线性表InitList(L)    
  12. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)    
  13. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)    
  14. int ListLength(SqList *L);//求线性表的长度ListLength(L)    
  15. void DispList(SqList *L);//输出线性表DispList(L)    
  16. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)    
  17. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)    
  18. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)    
  19. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)#endif // LIST_H_INCLUDED

源文件:list.cpp

  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. }    

源文件:main函数

  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. }    

运行结果:



知识点总结:
采用程序的多文件组织形式,建设了自己的算法库。
学习心得:
练习了顺序表的基本运算,运用算法库会解决接下来的很多问题。

0 0
原创粉丝点击