第三周项目1-顺序表的基本运算

  1. 问题描述及代码:   
  2. 1.  /*      
  3. 2.  *烟台大学计控学院       
  4. 3.  *作    者:陈晓琳      
  5. 4.  *完成日期:2016年9月20日   
  6. 5.  *问题描述:在数据结构的学习中,掌握基本运算是一个基础性的工作。这种“抽象”级别的成果,适用于各种应用场合,也是训练计算思维的根本依托之一。   
  7. 6.             (1)初始化线性表InitList(&L):构造一个空的线性表L   
  8. 7.  (2)销毁线性表DestroyList(&L):释放线性表L占用的内存空间   
  9. 8.  (3)判线性表是否为空表ListEmpty(L):若L为空表,则返回真,否则返回假   
  10. 9.  (4)求线性表的长度ListLength(L):返回L中元素个数   
  11. 10. (5)输出线性表DispList(L):当线性表L不为空时,顺序显示L中各节点的值域   
  12. 11. (6)求线性表L中指定位置的某个数据元素GetElem(L,i,&e):用e返回L中第 i 个元素的值   
  13. 12. (7)查找元素LocateElem(L,e):返回线性表L中第1个与e相等的序号,找不到返回0   
  14. 13. (8)插入元素ListInsert(&L, i, &e):在线性表L中的第i个位置插入元素e;   
  15. 14. (9)删除元素ListDelete(&L, i, &e):在线性表L中删除第i个元素,有e返回删除的值;   
  16. 15. */      
  17. (1)目的是要测试“建立线性表”的算法CreateList,为查看建表的结果,需要实现“输出线性表”的算法DispList。在研习DispList中发现,要输出线性表,还要判断表是否为空,这样,实现判断线性表是否为空的算法ListEmpty成为必要。这样,再加上main函数,这个程序由4个函数构成。main函数用于写测试相关的代码。  
  18. [cpp] view plain copy  
  19. 1.  #include<stdio.h>    
  20. 2.  #include<malloc.h>    
  21. 3.  #define Maxsize 50    
  22. 4.  typedef int ElemType;    
  23. 5.  typedef struct    
  24. 6.  {    
  25. 7.      ElemType data[Maxsize];    
  26. 8.      int length;    
  27. 9.  }SqList;    
  28. 10. //自定义函数声明部分    
  29. 11. void CreatList(SqList *&L,ElemType a[],int n);//用数组创建线性表    
  30. 12. void DispList(SqList *L);//输出线性表DisList(L)    
  31. 13. bool ListEmpty(SqList *L);//判断是否为空表ListEmpty(L)    
  32. 14. //实现测试函数    
  33. 15. int main()    
  34. 16. {    
  35. 17.     SqList *sq;    
  36. 18.     ElemType x[6]={5,8,7,2,4,9};    
  37. 19.     CreatList(sq,x,6);    
  38. 20.     DispList(sq);    
  39. 21.     return 0;    
  40. 22. }    
  41. 23. //实现自定义函数    
  42. 24. void CreatList(SqList *&L,ElemType a[],int n)//用数组创建线性表    
  43. 25. {    
  44. 26.     int i;    
  45. 27.     L=(SqList * )malloc(sizeof(SqList));    
  46. 28.     for(i=0;i<n;i++)    
  47. 29.         L->data[i]=a[i];    
  48. 30.     L->length=n;     
  49. 31. }    
  50. 32. void DispList(SqList *L)//输出线性表DisList(L)    
  51. 33. {    
  52. 34.     int i;    
  53. 35.     for(i=0;i<L->length;i++)    
  54. 36.         printf("%d ",L->data[i]);    
  55. 37.     printf("\n");    
  56. 38. }    
  57. 39. bool ListEmpty(SqList *L)//判断是否为空表ListEmpty(L)    
  58. 40. {    
  59. 41.     return(L->length==0);    
  60. 42. }    
  61.   
  62. 运行结果:  

  63. (2)在已经创建线性表的基础上,求线性表的长度ListLength、求线性表L中指定位置的某个数据元素GetElem、查找元素LocateElem的算法都可以实现了。就在原程序的基础上增加:  
  64.  增加求线性表的长度ListLength的函数并测试;     
  65.  增加求线性表L中指定位置的某个数据元素GetElem的函数并测试;     
  66.  增加查找元素LocateElem的函数并测试    
  67. 1.head.h的代码  
  68. [cpp] view plain copy  
  69. 1.  #include<stdio.h>    
  70. 2.  #include<malloc.h>    
  71. 3.  #define Maxsize 50    
  72. 4.  typedef int ElemType;    
  73. 5.  typedef struct    
  74. 6.  {    
  75. 7.      ElemType data[Maxsize];    
  76. 8.      int length;    
  77. 9.  }SqList;    
  78. 10. //自定义函数声明部分    
  79. 11. void CreatList(SqList *&L,ElemType a[],int n);//用数组创建线性表    
  80. 12. void DispList(SqList *L);//输出线性表DisList(L)    
  81. 13. bool ListEmpty(SqList *L);//判断是否为空表ListEmpty(L)    
  82. 14. int ListLength(SqList *L);//求线性表的长度ListLength(L)    
  83. 15. bool GetElem(SqList *L,int i,ElemType &e); //求某个数据元素值GetElem(L,i,e)    
  84. 16. int LocateElem(SqList *L, ElemType e); //按元素值查找LocateElem(L,e)    
  85.   
  86. 2.main.cpp中的代码  
  87. [cpp] view plain copy  
  88. 1.  #include"head.h"    
  89. 2.  //实现测试函数    
  90. 3.  int main()    
  91. 4.  {    
  92. 5.      SqList *sq;    
  93. 6.      ElemType x[6]={5,8,7,2,4,9};    
  94. 7.      CreatList(sq,x,6);    
  95. 8.      DispList(sq);    
  96. 9.      ElemType a;    
  97. 10.     int loc;    
  98. 11.     
  99. 12.      printf("表长度:%d\n", ListLength(sq));  //测试求长度    
  100. 13.     
  101. 14.     if(GetElem(sq, 3, a))  //测试在范围内的情形    
  102. 15.         printf("找到了第3个元素值为:%d\n", a);    
  103. 16.     else    
  104. 17.         printf("第3个元素超出范围!\n");    
  105. 18.     
  106. 19.     if(GetElem(sq, 15, a))  //测试不在范围内的情形    
  107. 20.         printf("找到了第15个元素值为:%d\n", a);    
  108. 21.     else    
  109. 22.         printf("第15个元素超出范围!\n");    
  110. 23.     
  111. 24.     if((loc=LocateElem(sq, 8))>0)  //测试能找到的情形    
  112. 25.         printf("找到了,值为8的元素是第 %d 个\n", loc);    
  113. 26.     else    
  114. 27.         printf("值为8的元素木有找到!\n");    
  115. 28.     
  116. 29.     if((loc=LocateElem(sq, 17))>0)  //测试不能找到的情形    
  117. 30.         printf("找到了,值为17的元素是第 %d 个\n", loc);    
  118. 31.     else    
  119. 32.         printf("值为17的元素木有找到!\n");    
  120. 33.     
  121. 34.     return 0;    
  122. 35.     
  123. 36. }    
  124. 3.创建线性表的基础代码  
  125. [cpp] view plain copy  
  126. 1.  //自定义函数声明部分    
  127. 2.  void CreatList(SqList *&L,ElemType a[],int n);//用数组创建线性表    
  128. 3.  void DispList(SqList *L);//输出线性表DisList(L)    
  129. 4.  bool ListEmpty(SqList *L);//判断是否为空表ListEmpty(L)    
  130. 5.      
  131. 6.  //实现自定义函数    
  132. 7.  void CreatList(SqList *&L,ElemType a[],int n)//用数组创建线性表    
  133. 8.  {    
  134. 9.      int i;    
  135. 10.     L=(SqList * )malloc(sizeof(SqList));    
  136. 11.     for(i=0;i<n;i++)    
  137. 12.         L->data[i]=a[i];    
  138. 13.     L->length=n;     
  139. 14. }    
  140. 15. void DispList(SqList *L)//输出线性表DisList(L)    
  141. 16. {    
  142. 17.     int i;    
  143. 18.     for(i=0;i<L->length;i++)    
  144. 19.         printf("%d ",L->data[i]);    
  145. 20.     printf("\n");    
  146. 21. }    
  147. 22. bool ListEmpty(SqList *L)//判断是否为空表ListEmpty(L)    
  148. 23. {    
  149. 24.     return(L->length==0);    
  150. 25. }    
  151. 26.     
  152. 27.         
  153.   
  154.    
  155. 4.查找,求长度等的代码  
  156. [cpp] view plain copy  
  157. 1.  #include"head.h"    
  158. 2.  //实现测试函数    
  159. 3.  int ListLength(SqList *L)//求线性表的长度ListLength(L)    
  160. 4.  {    
  161. 5.      return (L->length);    
  162. 6.  }    
  163. 7.  bool GetElem(SqList *L,int i,ElemType &e) //求某个数据元素值GetElem(L,i,e)    
  164. 8.  {    
  165. 9.      if(i<1||i>L->length)    
  166. 10.         return false;    
  167. 11.     e=L->data[i-1];    
  168. 12.     return true;    
  169. 13. }    
  170. 14. int LocateElem(SqList *L, ElemType e) //按元素值查找LocateElem(L,e)    
  171. 15. {    
  172. 16.     int i=0;    
  173. 17.     while(i<L->length &&L->data[i]!=e)    
  174. 18.         i++;    
  175. 19.     if(i>=L->length)    
  176. 20.         return 0;    
  177. 21.     else    
  178. 22.         return i+1;    
  179. 23. }    
  180.   
  181. 运行结果:  

  1.   
  2. (3)其余的4个基本运算:插入数据元素ListInsert、删除数据元素ListDelete、初始化线性表InitList、销毁线性表DestroyList都可以同法完成。  
  3. 1.初始化及插入的代码  
  4. [cpp] view plain copy  
  5. 1.  #include<stdio.h>    
  6. 2.  #include<malloc.h>    
  7. 3.  #define Maxsize 50    
  8. 4.  typedef int ElemType;    
  9. 5.  typedef struct    
  10. 6.  {    
  11. 7.      ElemType data[Maxsize];    
  12. 8.      int length;    
  13. 9.  }SqList;    
  14. 10. //自定义函数声明部分    
  15. 11. void CreatList(SqList *&L,ElemType a[],int n);//用数组创建线性表    
  16. 12. void DispList(SqList *L);//输出线性表DisList(L)    
  17. 13. bool ListEmpty(SqList *L);//判断是否为空表ListEmpty(L)    
  18. 14. void InitList(SqList *&L);//初始化线性表    
  19. 15. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素    
  20. 16. void CreatList(SqList *&L,ElemType a[],int n)//用数组创建线性表    
  21. 17. {    
  22. 18.     int i;    
  23. 19.     L=(SqList * )malloc(sizeof(SqList));    
  24. 20.     for(i=0;i<n;i++)    
  25. 21.         L->data[i]=a[i];    
  26. 22.     L->length=n;     
  27. 23. }    
  28. 24. void DispList(SqList *L)//输出线性表DisList(L)    
  29. 25. {    
  30. 26.     int i;    
  31. 27.     for(i=0;i<L->length;i++)    
  32. 28.         printf("%d ",L->data[i]);    
  33. 29.     printf("\n");    
  34. 30. }    
  35. 31. bool ListEmpty(SqList *L)//判断是否为空表ListEmpty(L)    
  36. 32. {    
  37. 33.     return(L->length==0);    
  38. 34. }    
  39. 35.     
  40. 36. void InitList(SqList *&L)//初始化线性表    
  41. 37. {    
  42. 38.     L=(SqList *)malloc(sizeof(SqList));    
  43. 39.     L->length=0;    
  44. 40. }    
  45. 41. bool ListInsert(SqList *&L,int i,ElemType e)//插入数据元素    
  46. 42. {    
  47. 43.     int j;    
  48. 44.     if(i<1||i>L->length+1)    
  49. 45.         return false;    
  50. 46.     i--;    
  51. 47.     for(j=L->length;j>i;j--)    
  52. 48.         L->data[j]=L->data[j-1];    
  53. 49.     L->data[i]=e;    
  54. 50.     L->length++;    
  55. 51.     return true;    
  56. 52. }    
  57. 53. int main()    
  58. 54. {    
  59. 55.     SqList *sq;    
  60. 56.     InitList(sq);    
  61. 57.     ListInsert(sq, 1, 5);    
  62. 58.     ListInsert(sq, 2, 3);    
  63. 59.     ListInsert(sq, 1, 4);    
  64. 60.     DispList(sq);    
  65. 61.     return 0;    
  66. 62. }    
  67.   
  68. 运行结果:  
[html]  view plain  copy

[html]  view plain  copy
  1. 2.删除及销毁的代码  
  2. [cpp] view plain copy  
  3. 1.  #include<stdio.h>    
  4. 2.  #include<malloc.h>    
  5. 3.  #define Maxsize 50    
  6. 4.  typedef int ElemType;    
  7. 5.  typedef struct    
  8. 6.  {    
  9. 7.      ElemType data[Maxsize];    
  10. 8.      int length;    
  11. 9.  }SqList;    
  12. 10. //自定义函数声明部分    
  13. 11. void CreatList(SqList *&L,ElemType a[],int n);//用数组创建线性表    
  14. 12. void DispList(SqList *L);//输出线性表DisList(L)    
  15. 13. bool ListEmpty(SqList *L);//判断是否为空表ListEmpty(L)    
  16. 14. void InitList(SqList *&L);//初始化线性表    
  17. 15. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素    
  18. 16. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素    
  19. 17. void DestroyList(SqList *&L);//销毁线性表    
  20. 18. void CreatList(SqList *&L,ElemType a[],int n)//用数组创建线性表    
  21. 19. {    
  22. 20.     int i;    
  23. 21.     L=(SqList * )malloc(sizeof(SqList));    
  24. 22.     for(i=0;i<n;i++)    
  25. 23.         L->data[i]=a[i];    
  26. 24.     L->length=n;     
  27. 25. }    
  28. 26. void DispList(SqList *L)//输出线性表DisList(L)    
  29. 27. {    
  30. 28.     int i;    
  31. 29.     for(i=0;i<L->length;i++)    
  32. 30.         printf("%d ",L->data[i]);    
  33. 31.     printf("\n");    
  34. 32. }    
  35. 33. bool ListEmpty(SqList *L)//判断是否为空表ListEmpty(L)    
  36. 34. {    
  37. 35.     return(L->length==0);    
  38. 36. }    
  39. 37.     
  40. 38. void InitList(SqList *&L)//初始化线性表    
  41. 39. {    
  42. 40.     L=(SqList *)malloc(sizeof(SqList));    
  43. 41.     L->length=0;    
  44. 42. }    
  45. 43. bool ListInsert(SqList *&L,int i,ElemType e)//插入数据元素    
  46. 44. {    
  47. 45.     int j;    
  48. 46.     if(i<1||i>L->length+1)    
  49. 47.         return false;    
  50. 48.     i--;    
  51. 49.     for(j=L->length;j>i;j--)    
  52. 50.         L->data[j]=L->data[j-1];    
  53. 51.     L->data[i]=e;    
  54. 52.     L->length++;    
  55. 53.     return true;    
  56. 54. }    
  57. 55. bool ListDelete(SqList *&L,int i,ElemType e)//删除数据元素    
  58. 56. {    
  59. 57.     int j;    
  60. 58.     if(i<1||i>L->length-1)    
  61. 59.         return false;    
  62. 60.     i--;    
  63. 61.     e=L->data[i];    
  64. 62.     for(j=i;j<L->length-1;j++)    
  65. 63.         L->data[j]=L->data[j+1];    
  66. 64.     L->length--;    
  67. 65.     return true;    
  68. 66. }    
  69. 67. void DestroyList(SqList *&L)    
  70. 68. {    
  71. 69.     free(L);    
  72. 70. }    
  73. 71. int main()    
  74. 72. {    
  75. 73.     SqList *sq;    
  76. 74.     InitList(sq);    
  77. 75.     ListInsert(sq, 1, 5);    
  78. 76.     ListInsert(sq, 2, 3);    
  79. 77.     ListInsert(sq, 1, 4);    
  80. 78.     printf("插入后的:\n");    
  81. 79.     DispList(sq);    
  82. 80.     ListDelete(sq, 1, 5);    
  83. 81.     ListDelete(sq, 2, 3);    
  84. 82.     printf("删除后的:\n");    
  85. 83.     DispList(sq);    
  86. 84.     
  87. 85.     DestroyList(sq);    
  88. 86.     printf("销毁后的:\n");    
  89. 87.     DispList(sq);    
  90. 88.     return 0;    
  91. 89.     
  92. 90. }    
  93.   
  94. 运行结果:  
[html]  view plain  copy

  1. 知识点总结:  
  2. 实现线性表的基本运算:(1)初始化线性表InitList(&L):构造一个空的线性表L(2)销毁线性表DestroyList(&L):释放线性表L占用的内存空间(3)判线性表是否为空表ListEmpty(L):若L为空表,则返回真,否则返回假(4)求线性表的长度ListLength(L):返回L中元素个数(5)输出线性表DispList(L):当线性表L不为空时,顺序显示L中各节点的值域(6)求线性表L中指定位置的某个数据元素GetElem(L,i,&e):用e返回L中第 i 个元素的值(7)查找元素LocateElem(L,e):返回线性表L中第1个与e相等的序号,找不到返回0。(8)插入数据元素ListInsert(SqList *&L,int i,ElemType e):在L的第i个元素之前插入新的元素e,L的长度增加1;(9)删除数据元素ListDele(&L,i,&e):删除L的第i个数据元素,并用e返回其值,L的长度减1;(10)销毁线性表DestroyList(SqList *&L):释放线性表L占用的内存空间;  
  3. 学习心得:  要多练习,在编写代码方面要加强
  4.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值