数据结构第三周项目--顺序表的基本运算

本文通过C++代码详细展示了顺序表的基本运算实现过程,包括创建、输出、判断空表、求长度等操作,并通过测试函数验证了算法的有效性和正确性。
  1. /*     
  2. *Copyright (c) 2017,烟台大学计算机与控制工程学院     
  3. *All rights reserved.     
  4. *文件名称:fd.cpp     
  5. *作    者:刘浩     
  6. *完成日期:2017年9月28日     
  7. *版 本 号:v1.0     
  8. *问题描述:实现顺序表基本运算有算法,依据“最小化”的原则进行测试。   
  9.            所谓最小化原则,指的是利用尽可能少的基本运算,   
  10.            组成一个程序,并设计main函数完成测试。     
  11. */   

(1)目的是要测试“建立线性表”的算法CreateList,为查看建表的结果,需要实现“输出线性表”的算法DispList。在研习DispList中发现,要输出线性表,还要判断表是否为空,这样,实现判断线性表是否为空的算法ListEmpty成为必要。这样,再加上main函数,这个程序由4个函数构成。main函数用于写测试相关的代码。

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3.   
  4. #define MaxSize 50    //Maxsize将用于后面定义存储空间的大小  
  5. typedef int ElemType;    
  6. typedef struct  
  7. {  
  8.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
  9.     int length;  
  10. } SqList;  
  11.   
  12. //自定义函数声明部分  
  13. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  14. void DispList(SqList *L);//输出线性表DispList(L)  
  15. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  16.   
  17. //实现测试函数  
  18. int main()  
  19. {  
  20.     SqList *sq;  
  21.     ElemType x[6]= {5,8,7,2,4,9};  
  22.     CreateList(sq, x, 6);  
  23.     DispList(sq);  
  24.     return 0;  
  25. }  
  26.   
  27. //下面实现要测试的各个自定义函数  
  28. //用数组创建线性表  
  29. void CreateList(SqList *&L, ElemType a[], int n)  
  30. {  
  31.     int i;  
  32.     L=(SqList *)malloc(sizeof(SqList));  
  33.     for (i=0; i<n; i++)  
  34.         L->data[i]=a[i];  
  35.     L->length=n;  
  36. }  
  37.   
  38. //输出线性表DispList(L)  
  39. void DispList(SqList *L)  
  40. {  
  41.     int i;  
  42.     if (ListEmpty(L))  
  43.         return;  
  44.     for (i=0; i<L->length; i++)  
  45.         printf("%d ",L->data[i]);  
  46.     printf("\n");  
  47. }  
  48.   
  49. //判定是否为空表ListEmpty(L)  
  50. bool ListEmpty(SqList *L)  
  51. {  
  52.     return(L->length==0);  
  53. }  

运行结果:

(2)在已经创建线性表的基础上,求线性表的长度ListLength、求线性表L中指定位置的某个数据元素GetElem、查找元素LocateElem的算法都可以实现了。就在原程序的基础上增加: 
  增加求线性表的长度ListLength的函数并测试; 
  增加求线性表L中指定位置的某个数据元素GetElem的函数并测试; 
  增加查找元素LocateElem的函数并测试;

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3.   
  4. #define MaxSize 50    //Maxsize将用于后面定义存储空间的大小  
  5. typedef int ElemType;  //ElemType在不同场合可以根据问题的需要确定,在此取简单的int  
  6. typedef struct  
  7. {  
  8.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
  9.     int length;  
  10. } SqList;  
  11.   
  12. //自定义函数声明部分  
  13. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  14. void DispList(SqList *L);//输出线性表DispList(L)  
  15. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  16. int ListLength(SqList *L); //求线性表的长度ListLength(L)  
  17. bool GetElem(SqList *L,int i,ElemType &e); //求某个数据元素值GetElem(L,i,e)  
  18. int LocateElem(SqList *L, ElemType e); //按元素值查找LocateElem(L,e)  
  19.   
  20. //实现测试函数  
  21. int main()  
  22. {  
  23.     SqList *sq;  
  24.     ElemType x[6]= {5,8,7,2,4,9};  
  25.     ElemType a;  
  26.     int loc;  
  27.     CreateList(sq, x, 6);  
  28.     DispList(sq);  
  29.   
  30.     printf("表长度:%d\n", ListLength(sq));  //测试求长度  
  31.   
  32.     if(GetElem(sq, 3, a))  //测试在范围内的情形  
  33.         printf("找到了第3个元素值为:%d\n", a);  
  34.     else  
  35.         printf("第3个元素超出范围!\n");  
  36.   
  37.     if(GetElem(sq, 15, a))  //测试不在范围内的情形  
  38.         printf("找到了第15个元素值为:%d\n", a);  
  39.     else  
  40.         printf("第15个元素超出范围!\n");  
  41.   
  42.     if((loc=LocateElem(sq, 8))>0)  //测试能找到的情形  
  43.         printf("找到了,值为8的元素是第 %d 个\n", loc);  
  44.     else  
  45.         printf("值为8的元素木有找到!\n");  
  46.   
  47.     if((loc=LocateElem(sq, 17))>0)  //测试不能找到的情形  
  48.         printf("找到了,值为17的元素是第 %d 个\n", loc);  
  49.     else  
  50.         printf("值为17的元素木有找到!\n");  
  51.   
  52.     return 0;  
  53. }  
  54.   
  55. //下面实现要测试的各个自定义函数  
  56. //用数组创建线性表  
  57. void CreateList(SqList *&L, ElemType a[], int n)  
  58. {  
  59.     int i;  
  60.     L=(SqList *)malloc(sizeof(SqList));  
  61.     for (i=0; i<n; i++)  
  62.         L->data[i]=a[i];  
  63.     L->length=n;  
  64. }  
  65.   
  66. //输出线性表DispList(L)  
  67. void DispList(SqList *L)  
  68. {  
  69.     int i;  
  70.     if (ListEmpty(L))  
  71.         return;  
  72.     for (i=0; i<L->length; i++)  
  73.         printf("%d ",L->data[i]);  
  74.     printf("\n");  
  75. }  
  76.   
  77. //判定是否为空表ListEmpty(L)  
  78. bool ListEmpty(SqList *L)  
  79. {  
  80.     return(L->length==0);  
  81. }  
  82.   
  83. //求线性表的长度ListLength(L)  
  84. int ListLength(SqList *L)  
  85. {  
  86.     return(L->length);  
  87. }  
  88.   
  89. //求某个数据元素值GetElem(L,i,e)  
  90. bool GetElem(SqList *L,int i,ElemType &e)  
  91. {  
  92.     if (i<1 || i>L->length)  
  93.         return false;  
  94.     e=L->data[i-1];  
  95.     return true;  
  96. }  
  97.   
  98. //按元素值查找LocateElem(L,e)  
  99. int LocateElem(SqList *L, ElemType e)  
  100. {  
  101.     int i=0;  
  102.     while (i<L->length && L->data[i]!=e) i++;  
  103.     if (i>=L->length)  
  104.         return 0;  
  105.     else  
  106.         return i+1;  
  107. }  

运行结果:

3)其余的4个基本运算:插入数据元素ListInsert、删除数据元素ListDelete、初始化线性表InitList、销毁线性表DestroyList都可以同法完成。 刚才的测试函数已经变得庞大。基本运算的模块保留,用于测试的main函数可以改变。main函数的针对性越强,实践越有效。

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3.   
  4. #define MaxSize 50    //Maxsize将用于后面定义存储空间的大小  
  5. typedef int ElemType;  //ElemType在不同场合可以根据问题的需要确定,在此取简单的int  
  6. typedef struct  
  7. {  
  8.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
  9.     int length;  
  10. } SqList;  
  11.   
  12. //自定义函数声明部分  
  13. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  14. void DispList(SqList *L);//输出线性表DispList(L)  
  15. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  16. int ListLength(SqList *L); //求线性表的长度ListLength(L)  
  17. bool GetElem(SqList *L,int i,ElemType &e); //求某个数据元素值GetElem(L,i,e)  
  18. int LocateElem(SqList *L, ElemType e); //按元素值查找LocateElem(L,e)  
  19. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)  
  20. bool ListDelete(SqList*&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)  
  21. void InitList(SqList *&L);//初始化线性表  
  22. void DestroyList(SqList *&L);//销毁线性表  
  23.   
  24. //实现测试函数  
  25. int main()  
  26. {  
  27.     SqList *sq;  
  28.     ElemType x[6]= {5,8,7,2,4,9};  
  29.     ElemType a;  
  30.     int loc;  
  31.     CreateList(sq, x, 6);  
  32.     DispList(sq);  
  33.   
  34.     printf("表长度:%d\n", ListLength(sq));  //测试求长度  
  35.   
  36.     if(GetElem(sq, 3, a))  //测试在范围内的情形  
  37.         printf("找到了第3个元素值为:%d\n", a);  
  38.     else  
  39.         printf("第3个元素超出范围!\n");  
  40.   
  41.     if(GetElem(sq, 15, a))  //测试不在范围内的情形  
  42.         printf("找到了第15个元素值为:%d\n", a);  
  43.     else  
  44.         printf("第15个元素超出范围!\n");  
  45.   
  46.     if((loc=LocateElem(sq, 8))>0)  //测试能找到的情形  
  47.         printf("找到了,值为8的元素是第 %d 个\n", loc);  
  48.     else  
  49.         printf("值为8的元素木有找到!\n");  
  50.   
  51.     if((loc=LocateElem(sq, 17))>0)  //测试不能找到的情形  
  52.         printf("找到了,值为17的元素是第 %d 个\n", loc);  
  53.     else  
  54.         printf("值为17的元素木有找到!\n");  
  55.   
  56.     ElemType E;  
  57.   
  58.     InitList(sq);  
  59.   
  60.      printf("在第1个位置插入元素1\n");  
  61.     ListInsert(sq,1,1);  
  62.     DispList(sq);  
  63.   
  64.      printf("在第2个位置插入元素6\n");  
  65.     ListInsert(sq,2,6);  
  66.     DispList(sq);  
  67.   
  68.      printf("在第1个位置插入元素9\n");  
  69.     ListInsert(sq,1,9);  
  70.     DispList(sq);  
  71.   
  72.      printf("删除第2个位置的元素\n");  
  73.     ListDelete(sq,2,E);  
  74.     DispList(sq);  
  75.   
  76.     DestroyList(sq);  
  77.     DispList(sq);  
  78.     return 0;  
  79. }  
  80.   
  81. //下面实现要测试的各个自定义函数  
  82. //用数组创建线性表  
  83. void CreateList(SqList *&L, ElemType a[], int n)  
  84. {  
  85.     int i;  
  86.     L=(SqList *)malloc(sizeof(SqList));  
  87.     for (i=0; i<n; i++)  
  88.         L->data[i]=a[i];  
  89.     L->length=n;  
  90. }  
  91.   
  92. //输出线性表DispList(L)  
  93. void DispList(SqList *L)  
  94. {  
  95.     int i;  
  96.     if (ListEmpty(L))  
  97.         return;  
  98.     for (i=0; i<L->length; i++)  
  99.         printf("%d ",L->data[i]);  
  100.     printf("\n");  
  101. }  
  102.   
  103. //判定是否为空表ListEmpty(L)  
  104. bool ListEmpty(SqList *L)  
  105. {  
  106.     return(L->length==0);  
  107. }  
  108.   
  109. //求线性表的长度ListLength(L)  
  110. int ListLength(SqList *L)  
  111. {  
  112.     return(L->length);  
  113. }  
  114.   
  115. //求某个数据元素值GetElem(L,i,e)  
  116. bool GetElem(SqList *L,int i,ElemType &e)  
  117. {  
  118.     if (i<1 || i>L->length)  
  119.         return false;  
  120.     e=L->data[i-1];  
  121.     return true;  
  122. }  
  123.   
  124. //按元素值查找LocateElem(L,e)  
  125. int LocateElem(SqList *L, ElemType e)  
  126. {  
  127.     int i=0;  
  128.     while (i<L->length && L->data[i]!=e) i++;  
  129.     if (i>=L->length)  
  130.         return 0;  
  131.     else  
  132.         return i+1;  
  133. }  
  134. //插入数据元素ListInsert(L,i,e)  
  135. bool ListInsert(SqList *&L,int i,ElemType e)  
  136. {  
  137.     int j;  
  138.     if (i<1 || i>L->length+1)  
  139.         return false;//参数错误时返回false  
  140.         i--;//将顺序表逻辑序号转化为物理序号  
  141.     for(j=L->length;j>i;j--)//将data[i..n]元素后移一个位置  
  142.         L->data[j]=L->data[j-1];  
  143.     L->data[i]=e;//插入元素e  
  144.     L->length++;//顺序表长度增1  
  145.     return true;//成功插入返回true  
  146. }  
  147. //删除数据元素ListDelete(L,i,e)  
  148. bool ListDelete(SqList *&L,int i,ElemType &e)  
  149. {  
  150.     int j;  
  151.     if(i<1||i>L->length)//参数错误时返回false  
  152.     return false;  
  153.     i--;//将顺序表逻辑序号转化为物理序号  
  154.     e=L->data[i];  
  155.     for(j=i;j<L->length-1;j++)//将data[i..n-1]元素前移  
  156.         L->data[j]=L->data[j+1];  
  157.     L->length--;//顺序表长度减1  
  158.     return true;//成功删除返回true  
  159. }  
  160. void InitList(SqList *&L)//引用型指针  
  161. {  
  162.     L=(SqList *)malloc(sizeof(SqList));  
  163.     //分配存放线性表的空间  
  164.     L->length=0;  
  165. }  
  166. //销毁线性表DestroyList(L)  
  167. void DestroyList(SqList *&L)  
  168. {  
  169.     free(L);  
  170. }  

运行结果:



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值