当前位置:   article > 正文

数据结构中线性表的基本操作(c语言)_c语言线性表操作

c语言线性表操作

首先进行头文件的使用、宏定义以及使用typedef创建别名:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define OK 1
  4. #define ERROR 0
  5. #define LIST_INIT_SIZE 100
  6. #define LISTINCREAMENT 10
  7. typedef float ElemType;
  8. typedef int Status;
  9. typedef struct
  10. {
  11. ElemType *elem; /* 指向线性表占用的数组空间。*/
  12. int length; /*线性表的长度*/
  13. int listsize;
  14. } SqList;

五个包文件:

1.对空表的创建:

  1. //创建空表
  2. Status initlist_sq(SqList &L)
  3. {
  4. L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
  5. L.length=0;
  6. L.listsize=LIST_INIT_SIZE;
  7. return OK;
  8. }

2.插入:

  1. Status listinsert(SqList &L,int i,ElemType e)
  2. {
  3. int j;
  4. if(i<1||i>L.length+1)
  5. return ERROR;
  6. if(L.length==L.listsize)
  7. return ERROR;
  8. for(j=L.length-1;j>=i-1;j--)
  9. {
  10. L.elem[j+1]=L.elem[j];
  11. }
  12. L.elem[i-1]=e;
  13. ++L.length;
  14. return OK;
  15. }

3.删除:

  1. //删除
  2. Status listdelete(SqList &L,int i)
  3. //i代表的不是数组下标
  4. { int j;
  5. if((i<1)||(i>L.length))
  6. return ERROR;
  7. for(j=i;j<=L.length-1;j++)
  8. L.elem[j-1]=L.elem[j];
  9. --L.length;
  10. return OK;
  11. }

4.取值:

  1. //取值
  2. Status GetElem(SqList L,int i,ElemType &e){
  3. if(i<1||i>L.length)return ERROR;
  4. e=L.elem[i-1];
  5. return OK;
  6. }

5.查找:

  1. //查找
  2. Status LocateElem(SqList L,ElemType e)
  3. {
  4. //在顺序表中查询第一个满足判定条件的数据元素,若存在,则返回它的位序,若不存在,则返回
  5. //ERROR
  6. int i=1;
  7. while(i<=L.length&&L.elem[i-1]!=e)
  8. i++;
  9. if(i<=L.length) return i;
  10. else return ERROR;
  11. }

对包的使用:

  1. int main(){
  2. SqList L;
  3. int i,z;
  4. ElemType x,y;
  5. initlist_sq(L);
  6. for(i=1;i<=10;i++){
  7. scanf("%f",&x);
  8. listinsert(L,i,x);
  9. }
  10. GetElem(L,4,y);
  11. printf("%f\n",y);
  12. listdelete(L,3);
  13. for(i=1;i<10;i++){
  14. printf("%5.1f",L.elem[i-1]);
  15. }
  16. printf("\n");
  17. GetElem(L,4,y);
  18. printf("%f\n",y);
  19. z=LocateElem(L,5);
  20. printf("%d\n",z);
  21. return 0;
  22. }

基本代码如下:

  1. //线性表
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #define OK 1
  5. #define ERROR 0
  6. #define LIST_INIT_SIZE 100
  7. #define LISTINCREAMENT 10
  8. typedef float ElemType;
  9. typedef int Status;
  10. typedef struct
  11. {
  12. ElemType *elem; /* 指向线性表占用的数组空间。*/
  13. int length; /*线性表的长度*/
  14. int listsize;
  15. } SqList;
  16. //创建空表
  17. Status initlist_sq(SqList &L)
  18. {
  19. L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
  20. L.length=0;
  21. L.listsize=LIST_INIT_SIZE;
  22. return OK;
  23. }
  24. //插入
  25. Status listinsert(SqList &L,int i,ElemType e)
  26. {
  27. int j;
  28. if(i<1||i>L.length+1)
  29. return ERROR;
  30. if(L.length==L.listsize)
  31. return ERROR;
  32. for(j=L.length-1;j>=i-1;j--)
  33. {
  34. L.elem[j+1]=L.elem[j];
  35. }
  36. L.elem[i-1]=e;
  37. ++L.length;
  38. return OK;
  39. }
  40. //删除
  41. Status listdelete(SqList &L,int i)
  42. //i代表的不是数组下标
  43. { int j;
  44. if((i<1)||(i>L.length))
  45. return ERROR;
  46. for(j=i;j<=L.length-1;j++)
  47. L.elem[j-1]=L.elem[j];
  48. --L.length;
  49. return OK;
  50. }
  51. //取值
  52. Status GetElem(SqList L,int i,ElemType &e){
  53. if(i<1||i>L.length)return ERROR;
  54. e=L.elem[i-1];
  55. return OK;
  56. }
  57. //查找
  58. Status LocateElem(SqList L,ElemType e)
  59. {
  60. //在顺序表中查询第一个满足判定条件的数据元素,若存在,则返回它的位序,若不存在,则返回
  61. //ERROR
  62. int i=1;
  63. while(i<=L.length&&L.elem[i-1]!=e)
  64. i++;
  65. if(i<=L.length) return i;
  66. else return ERROR;
  67. }
  68. int main(){
  69. SqList L;
  70. int i,z;
  71. ElemType x,y;
  72. initlist_sq(L);
  73. for(i=1;i<=10;i++){
  74. scanf("%f",&x);
  75. listinsert(L,i,x);
  76. }
  77. GetElem(L,4,y);
  78. printf("%f\n",y);
  79. listdelete(L,3);
  80. for(i=1;i<10;i++){
  81. printf("%5.1f",L.elem[i-1]);
  82. }
  83. printf("\n");
  84. GetElem(L,4,y);
  85. printf("%f\n",y);
  86. z=LocateElem(L,5);
  87. printf("%d\n",z);
  88. return 0;
  89. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/529394
推荐阅读
相关标签
  

闽ICP备14008679号