1. //SqList.c
    2. #define ElemType int
    3. #define MaxSize 50
    4. #include"SqList.h"
    5. int main(){
    6. int i;
    7. ElemType a[] = {1,2,3,4,5,6,7,8};
    8. SqList * L;
    9. ElemType e;
    10. CreateList(&L,a,8);
    11. printf("打印顺序表:\n");
    12. DisplayList(L);
    13. printf("顺序表是否(1/0)为空:%d\n",ListEmpty(L));
    14. printf("顺序表长度:%d\n",ListLength(L));
    15. if(GetElem(L,4,&e)){
    16. printf("打印第4个元素:%d\n",e);
    17. }else{
    18. printf("输入序号不正确!\n");
    19. }
    20. if(LocateElem(L,7)){
    21. printf("第%d个元素为7\n",LocateElem(L,7));
    22. }else
    23. {
    24. printf("没有该元素!\n");
    25. }
    26. i = ListInsert(&L,4,0);
    27. if(i){
    28. printf("插入成功!\n");
    29. }else
    30. {
    31. printf("插入失败!\n");
    32. }
    33. printf("打印顺序表:\n");
    34. DisplayList(L);
    35. i = ListDelete(&L,4,&e);
    36. if(i){
    37. printf("删除成功!\n");
    38. }else
    39. {
    40. printf("删除失败!\n");
    41. }
    42. printf("打印顺序表:\n");
    43. DisplayList(L);
    44. return 0;
    45. }
    1. //顺序表
    2. //SqList.h
    3. #include<stdlib.h>
    4. #include<stdio.h>
    5. typedef struct{
    6. ElemType data[MaxSize];
    7. int length;
    8. }SqList;
    9. //0.整体创建
    10. void CreateList(SqList ** L,ElemType a[],int n){
    11. int i = 0;
    12. (*L) = (SqList *)malloc(sizeof(SqList));
    13. while(i<n){
    14. (*L)->data[i] = a[i];
    15. i++;
    16. }
    17. (*L)->length = i;
    18. }
    19. //1.初始化顺序表
    20. void InitList(SqList ** L){
    21. (*L) = (SqList *)malloc(sizeof(SqList));
    22. (*L)->length = 0;
    23. }
    24. //2.销毁顺序表
    25. void DestroyList(SqList ** L){
    26. free(*L);
    27. }
    28. //3.判断顺序表是否为空
    29. int ListEmpty(SqList * L){
    30. return (L->length == 0);
    31. }
    32. //4.返回顺序表长度
    33. int ListLength(SqList * L){
    34. return (L->length);
    35. }
    36. //5.输出顺序表
    37. void DisplayList(SqList * L){
    38. int i = 0;
    39. for(;i < L->length;i++){
    40. printf("%d\t",L->data[i]);//顺序表的元素类型不一定为int,故此处语句用 %d 并不合适,还需做处理,此处仅当演示作用,后面的判断对比处,也不要计较,理解思路即可
    41. }
    42. printf("\n");
    43. }
    44. //6.求顺序表中的某个元素
    45. int GetElem(SqList * L,int i,ElemType * e){
    46. if(i<1||i>L->length){
    47. return 0;
    48. }
    49. *e = L->data[i-1];
    50. return 1;
    51. }
    52. //7.找元素返回序号
    53. int LocateElem(SqList * L,ElemType e){
    54. int i = 0;
    55. for(;i < L->length-1;i++){
    56. if(L->data[i] == e){
    57. return i+1;
    58. }
    59. }
    60. return 0;
    61. }
    62. //8.插入元素
    63. int ListInsert(SqList ** L,int i,ElemType e){
    64. int j;
    65. if(i<1||i>(*L)->length+1){
    66. return 0;
    67. }
    68. i--;
    69. for(j = (*L)->length;j>i;j--){
    70. (*L)->data[j] = (*L)->data[j-1];
    71. }
    72. (*L)->data[i] = e;
    73. (*L)->length++;
    74. return 1;
    75. }
    76. //9.删除元素
    77. int ListDelete(SqList ** L,int i,ElemType *e){
    78. int j;
    79. if(i<1||i>(*L)->length+1){
    80. return 0;
    81. }
    82. i--;
    83. *e = (*L)->data[i];
    84. for(j = i;j<(*L)->length-1;j++){
    85. (*L)->data[j] = (*L)->data[j+1];
    86. }
    87. (*L)->length--;
    88. return 1;
    89. }