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

问题描述及代码:
[cpp] view plain copy
1.	/*    
2.	*烟台大学计控学院     
3.	*作    者:朱建豪    
4.	*完成日期:2016年9月17日 
5.	*问题描述:本文为算法库中的第一个,针对线性表中的顺序存储结构,实现各种基本运算。  
6.	  算法库包括两个文件:  
7.	   头文件:list.h,包含定义顺序表数据结构的代码、宏定义、要实现算法的函数的声明;  
8.	   源文件:list.cpp,包含实现各种算法的函数的定义  
9.	*/  
1.list.h的代码
[cpp] view plain copy
1.	#include<stdio.h>  
2.	#include<malloc.h>  
3.	#define MaxSize 50  
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)  

2.list.cpp的代码
[cpp] view plain copy
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.	}  

3.各种main的测试函数的代码
(1)测试“建立线性表”的算法CreateList,为查看建表的结果,需要实现“输出线性表”的算法DispList。
[cpp] view plain copy
1.	int main()  
2.	{  
3.	    SqList *sq;  
4.	    ElemType x[6]={5,8,7,2,4,9};  
5.	    CreateList(sq,x,6);  
6.	    DisList (sq);  
7.	    return 0;  
8.	}  
(2)在已经创建线性表的基础上,求线性表的长度ListLength、求线性表L中指定位置的某个数据元素GetElem、查找元素LocateElem的算法都可以实现了。
[html] view plain copy
1.	int main()  
2.	{  
3.	    SqList *sq;  
4.	    ElemType x[6]= {5,8,7,2,4,9};  
5.	    ElemType a;  
6.	    int loc;  
7.	    CreateList(sq, x, 6);  
8.	    DisList(sq);  
9.	  
10.	    printf("表长度:%d\n", ListLength(sq));  //测试求长度  
11.	  
12.	    if(GetElem(sq, 3, a))  //测试在范围内的情形  
13.	        printf("找到了第3个元素值为:%d\n", a);  
14.	    else  
15.	        printf("第3个元素超出范围!\n");  
16.	  
17.	    if(GetElem(sq, 15, a))  //测试不在范围内的情形  
18.	        printf("找到了第15个元素值为:%d\n", a);  
19.	    else  
20.	        printf("第15个元素超出范围!\n");  
21.	  
22.	    if((loc=LocateElem(sq, 8))>0)  //测试能找到的情形  
23.	        printf("找到了,值为8的元素是第 %d 个\n", loc);  
24.	    else  
25.	        printf("值为8的元素木有找到!\n");  
26.	  
27.	    if((loc=LocateElem(sq, 17))>0)  //测试不能找到的情形  
28.	        printf("找到了,值为17的元素是第 %d 个\n", loc);  
29.	    else  
30.	        printf("值为17的元素木有找到!\n");  
31.	  
32.	    return 0;  
33.	}  

(3)插入数据元素ListInsert、删除数据元素ListDelete、初始化线性表InitList、销毁线性表DestroyList
[cpp] view plain copy
1.	int main()  
2.	{  
3.	    SqList *sq;  
4.	    InitList(sq);  
5.	    printf("构造的新的:\n");  
6.	    DisList(sq);  
7.	    ListInsert(sq, 1, 5);  
8.	    ListInsert(sq, 2, 3);  
9.	    ListInsert(sq, 1, 4);  
10.	    printf("插入后的:\n");  
11.	    DisList(sq);  
12.	    ListDelete(sq, 1, 5);  
13.	    ListDelete(sq, 2, 3);  
14.	    printf("删除后的:\n");  
15.	    DisList(sq);  
16.	  
17.	    DestroyList(sq);  
18.	    printf("销毁后的:\n");  
19.	    DisList(sq);  
20.	    return 0;  
21.	  
22.	}  

运行结果上个博文已展示..
知识点总结同上博文
学习心得:以后用算法就简单多了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值