第三周项目2 建设“顺序表”算法库
问题及代码:
算法库包括两个文件:
头文件:list.h,包含定义顺序表数据结构的代码、宏定义、要实现算法的函数的声明;
源文件:list.cpp,包含实现各种算法的函数的定义
请采用程序的多文件组织形式,在项目1的基础上,建立如上的两个文件,另外再建立一个源文件,编制main函数,完成相关的测试工作。
list.h:
- #define Maxsize 100
- typedef int Elemtype; //自定义数据类型
- typedef struct list
- {
- Elemtype data[Maxsize]; //存顺序表元素
- int length; //存顺序表长度
- } Sqlist;
- void CreateList(Sqlist *&l,Elemtype a[],int n); //由a中的n个元素建立顺序表
- void DispList(Sqlist *l); //输出线性表
- bool ListEmpty(Sqlist *l); //布尔型函数判断顺序表是否为空表
- int ListLength(Sqlist *l); //求顺序表长度
- bool GetElem(Sqlist *l,int i,Elemtype &e); //求顺序表中某个数据元素值
- int LocateElem(Sqlist *l,Elemtype e); //按元素值查找顺序表中元素
- bool ListInsert(Sqlist *&l,int i,Elemtype e); //插入数据元素
- bool ListDelete(Sqlist *&l,int i,Elemtype &e); //删除数据元素
- void InitList(Sqlist *&l); //初始化线性表
- void DestroyList(Sqlist *&l); //销毁顺序表
- #include <stdio.h>
- #include <malloc.h>
- #include "list.h"
- void CreateList(Sqlist *&l,Elemtype a[],int n) //由a中的n个元素建立顺序表
- {
- int i;
- l=(Sqlist *)malloc(sizeof(Sqlist)); //分配存储空间
- for(i=0;i<n;i++)
- l->data[i]=a[i]; //存放元素
- l->length=n; //设置长度
- }
- void DispList(Sqlist *l) //输出线性表
- {
- int i;
- // if(ListEmpty(l))
- // return;
- for(i=0;i<l->length;i++)
- printf("%d ",l->data[i]);
- printf("\n");
- }
- bool ListEmpty(Sqlist *l) //布尔型函数判断顺序表是否为空表
- {
- return (l->length==0); //空返回0 非空返回1
- }
- int ListLength(Sqlist *l) //求顺序表长度
- {
- return (l->length);
- }
- bool GetElem(Sqlist *l,int i,Elemtype &e) //求顺序表中某个数据元素值
- {
- if(i<1 || i>l->length) //i为逻辑序号,注意参数错误的情况
- return false; //参数错误返回false
- e=l->data[i-1];
- return true; //找到指定下标元素返回true
- }
- int LocateElem(Sqlist *l,Elemtype e) //按元素值查找顺序表中元素
- {
- int i=0;
- while(i<l->length && l->data[i]!=e) //e不是要查找的元素,继续往下遍历
- i++;
- if(i>=l->length)
- return 0; //遍历一遍未找到返回false
- else
- return i+1; //否则找到返回true
- }
- bool ListInsert(Sqlist *&l,int i,Elemtype e) //插入数据元素
- {
- int j;
- if(i<1 || i>l->length+1 || l->length>=Maxsize)//i为逻辑序号,注意参数错误的情况
- return false; //参数错误返回false
- i--; //顺序表逻辑序号转化为物理序号
- for(j=l->length-1;j>i;j--)
- l->data[j]=l->data[j-1]; //"后者覆盖前者"
- l->data[i]=e; //找到插入位置插入
- l->length++;
- return true;
- }
- bool ListDelete(Sqlist *&l,int i,Elemtype &e) //删除数据元素
- {
- int j;
- if(i<1 || i>l->length+1) //i为逻辑序号,注意参数错误的情况
- return false; //参数错误返回false
- i--; //顺序表逻辑序号转化为物理序号
- e=l->data[i]; //被删除元素
- for(j=i;j<l->length-1;j++)
- l->data[j]=l->data[j+1]; //"后者覆盖前者"
- l->length--;
- return true;
- }
- void InitList(Sqlist *&l) //初始化线性表
- {
- l=(Sqlist *)malloc(sizeof(Sqlist)); //分配存储空间
- l->length=0; //置空线性表,其长度为0
- }
- void DestroyList(Sqlist *&l) //销毁顺序表
- {
- free(l);
- }
- #include <iostream>
- #include <malloc.h>
- #include <cstdio>
- #include "list.h"
- using namespace std;
- int main()
- {
- Sqlist *l;
- Elemtype a[10]={1,2,3,4,5,6,7,8,9,10};
- Elemtype b[5]={5,8,7,4,6};
- Elemtype e;
- int loc;
- CreateList(l,a,10);
- cout<<"建立的顺序表中各元素为:"<<endl;
- DispList(l);
- cout<<"此线性表长度为:"<<ListLength(l)<<endl;
- if(GetElem(l,3,e)) //测试在范围内的情形
- printf("找到了第3个元素值为:%d\n", e);
- else
- printf("第3个元素超出范围!\n");
- if(GetElem(l,15,e)) //测试不在范围内的情形
- printf("找到了第15个元素值为:%d\n",e);
- else
- printf("第15个元素超出范围!\n");
- if((loc=LocateElem(l,8))>0) //测试能找到的情形
- printf("找到了,值为8的元素是第 %d 个\n",loc);
- else
- printf("值为8的元素木有找到!\n");
- if((loc=LocateElem(l,17))>0) //测试不能找到的情形
- printf("找到了,值为17的元素是第 %d 个\n",loc);
- else
- printf("值为17的元素木有找到!\n");
- DestroyList(l);
- cout<<"此顺序表被销毁"<<endl;
- cout<<endl;
- CreateList(l,b,5);
- cout<<"建立的顺序表中各元素为:"<<endl;
- DispList(l);
- cout<<"此线性表长度为:"<<ListLength(l)<<endl<<endl;
- ListInsert(l,4,2);
- cout<<"在4位置插入元素2后的顺序表为:"<<endl;
- DispList(l);
- ListDelete(l,2,e);
- cout<<"在2位置删除元素8后的顺序表为:"<<endl;
- DispList(l);
- DestroyList(l);
- cout<<"此顺序表被销毁"<<endl;
- cout<<endl;
- return 0;
- }
知识点总结:
算法库,程序的多文件组织
学习心得:
建立算法库为做题带来了方便