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

/* 
 
copyright (c) 2016,烟台大学计算机学院 
 
*All rights reserved. 
 
*文件名称:1.cpp 
 
*作者:孟令群 
 
*完成日期:2016年9月17日 
 
*版本号:v1.0 
 
*问题描述:用函数实现顺序表的10个基本运算,并用main函数完成调试工作(建立算法库,多文件组织)
*输入描述:无 
 
*程序输出:无 
 
*/  

list.h:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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);                      //销毁顺序表  

fun.cpp:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值