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

来源:互联网 发布:矩阵ab相似的充要条件 编辑:程序博客网 时间:2024/06/07 01:03

问题及代码:

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

list.h:

[csharp] view plain copy
  1. #define Maxsize 100    
  2. typedef int Elemtype;          //自定义数据类型    
  3. typedef struct list    
  4. {    
  5.     Elemtype data[Maxsize];    //存顺序表元素    
  6.     int length;                //存顺序表长度    
  7. } Sqlist;    
  8.     
  9. void CreateList(Sqlist *&l,Elemtype a[],int n);    //由a中的n个元素建立顺序表    
  10. void DispList(Sqlist *l);                          //输出线性表    
  11. bool ListEmpty(Sqlist *l);                         //布尔型函数判断顺序表是否为空表    
  12. int ListLength(Sqlist *l);                         //求顺序表长度    
  13. bool GetElem(Sqlist *l,int i,Elemtype &e);         //求顺序表中某个数据元素值    
  14. int LocateElem(Sqlist *l,Elemtype e);              //按元素值查找顺序表中元素    
  15. bool ListInsert(Sqlist *&l,int i,Elemtype e);      //插入数据元素    
  16. bool ListDelete(Sqlist *&l,int i,Elemtype &e);     //删除数据元素    
  17. void InitList(Sqlist *&l);                         //初始化线性表    
  18. void DestroyList(Sqlist *&l);                      //销毁顺序表    
list.cpp:

[csharp] view plain copy
  1. #include <stdio.h>    
  2. #include <malloc.h>    
  3. #include "list.h"    
  4. void CreateList(Sqlist *&l,Elemtype a[],int n)    //由a中的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. void DispList(Sqlist *l)                          //输出线性表    
  13. {    
  14.     int i;    
  15.    // if(ListEmpty(l))    
  16.     //    return;    
  17.     for(i=0;i<l->length;i++)    
  18.         printf("%d ",l->data[i]);    
  19.     printf("\n");    
  20. }    
  21. bool ListEmpty(Sqlist *l)                        //布尔型函数判断顺序表是否为空表    
  22. {    
  23.     return (l->length==0);                       //空返回0 非空返回1    
  24. }    
  25. int ListLength(Sqlist *l)                        //求顺序表长度    
  26. {    
  27.     return (l->length);    
  28. }    
  29. bool GetElem(Sqlist *l,int i,Elemtype &e)        //求顺序表中某个数据元素值    
  30. {    
  31.     if(i<1 || i>l->length)                       //i为逻辑序号,注意参数错误的情况    
  32.         return false;                            //参数错误返回false    
  33.     e=l->data[i-1];    
  34.     return true;                                 //找到指定下标元素返回true    
  35. }    
  36. int LocateElem(Sqlist *l,Elemtype e)             //按元素值查找顺序表中元素    
  37. {    
  38.     int i=0;    
  39.     while(i<l->length && l->data[i]!=e)          //e不是要查找的元素,继续往下遍历    
  40.         i++;    
  41.     if(i>=l->length)    
  42.         return 0;                                //遍历一遍未找到返回false    
  43.     else    
  44.         return i+1;                              //否则找到返回true    
  45. }    
  46. bool ListInsert(Sqlist *&l,int i,Elemtype e)     //插入数据元素    
  47. {    
  48.     int j;    
  49.     if(i<1 || i>l->length+1 || l->length>=Maxsize)//i为逻辑序号,注意参数错误的情况    
  50.         return false;                             //参数错误返回false    
  51.     i--;                                          //顺序表逻辑序号转化为物理序号    
  52.     for(j=l->length-1;j>i;j--)    
  53.         l->data[j]=l->data[j-1];                  //"后者覆盖前者"    
  54.     l->data[i]=e;                                 //找到插入位置插入    
  55.     l->length++;    
  56.     return true;    
  57. }    
  58. bool ListDelete(Sqlist *&l,int i,Elemtype &e)     //删除数据元素    
  59. {    
  60.     int j;    
  61.     if(i<1 || i>l->length+1)                      //i为逻辑序号,注意参数错误的情况    
  62.         return false;                             //参数错误返回false    
  63.     i--;                                          //顺序表逻辑序号转化为物理序号    
  64.     e=l->data[i];                                 //被删除元素    
  65.     for(j=i;j<l->length-1;j++)    
  66.         l->data[j]=l->data[j+1];                  //"后者覆盖前者"    
  67.     l->length--;    
  68.     return true;    
  69. }    
  70. void InitList(Sqlist *&l)                        //初始化线性表    
  71. {    
  72.     l=(Sqlist *)malloc(sizeof(Sqlist));          //分配存储空间    
  73.     l->length=0;                                 //置空线性表,其长度为0    
  74. }    
  75. void DestroyList(Sqlist *&l)                     //销毁顺序表    
  76. {    
  77.     free(l);    
  78. }    
main:

[cpp] view plain copy
  1. #include <iostream>    
  2. #include <malloc.h>    
  3. #include <cstdio>    
  4. #include "list.h"    
  5. using namespace std;    
  6.     
  7. int main()    
  8. {    
  9.     Sqlist *l;    
  10.     Elemtype a[10]={1,2,3,4,5,6,7,8,9,10};    
  11.     Elemtype b[5]={5,8,7,4,6};    
  12.     Elemtype e;    
  13.     int loc;    
  14.     CreateList(l,a,10);    
  15.     cout<<"建立的顺序表中各元素为:"<<endl;    
  16.     DispList(l);    
  17.     cout<<"此线性表长度为:"<<ListLength(l)<<endl;    
  18.     if(GetElem(l,3,e))                             //测试在范围内的情形    
  19.         printf("找到了第3个元素值为:%d\n", e);    
  20.     else    
  21.         printf("第3个元素超出范围!\n");    
  22.     
  23.     if(GetElem(l,15,e))                            //测试不在范围内的情形    
  24.         printf("找到了第15个元素值为:%d\n",e);    
  25.     else    
  26.         printf("第15个元素超出范围!\n");    
  27.     
  28.     if((loc=LocateElem(l,8))>0)                    //测试能找到的情形    
  29.         printf("找到了,值为8的元素是第 %d 个\n",loc);    
  30.     else    
  31.         printf("值为8的元素木有找到!\n");    
  32.     
  33.     if((loc=LocateElem(l,17))>0)                 //测试不能找到的情形    
  34.         printf("找到了,值为17的元素是第 %d 个\n",loc);    
  35.     else    
  36.         printf("值为17的元素木有找到!\n");    
  37.     DestroyList(l);    
  38.     cout<<"此顺序表被销毁"<<endl;    
  39.     cout<<endl;    
  40.     
  41.     CreateList(l,b,5);    
  42.     cout<<"建立的顺序表中各元素为:"<<endl;    
  43.     DispList(l);    
  44.     cout<<"此线性表长度为:"<<ListLength(l)<<endl<<endl;    
  45.     
  46.     ListInsert(l,4,2);    
  47.     cout<<"在4位置插入元素2后的顺序表为:"<<endl;    
  48.     DispList(l);    
  49.     
  50.     ListDelete(l,2,e);    
  51.     cout<<"在2位置删除元素8后的顺序表为:"<<endl;    
  52.     DispList(l);    
  53.     
  54.     DestroyList(l);    
  55.     cout<<"此顺序表被销毁"<<endl;    
  56.     cout<<endl;    
  57.     
  58.     return 0;    
  59. }    
运行结果:


知识点总结:

算法库,程序的多文件组织

学习心得:

建立算法库为做题带来了方便

阅读全文
0 0
原创粉丝点击