1. #define ElemType int
    2. #include"LinkNode.h"
    3. int main(){
    4. int i;
    5. ElemType a[] = {1,2,3,4,5,6,7,8};
    6. LinkNode * L;
    7. ElemType e;
    8. CreateListF(&L,a,8);
    9. printf("打印顺序表:\n");
    10. DisplayList(L);
    11. printf("顺序表是否(1/0)为空:%d\n",ListEmpty(L));
    12. printf("顺序表长度:%d\n",ListLength(L));
    13. if(GetElem(L,4,&e)){
    14. printf("打印第4个元素:%d\n",e);
    15. }else{
    16. printf("输入序号不正确!\n");
    17. }
    18. if(LocateElem(L,7)){
    19. printf("第%d个元素为7\n",LocateElem(L,7));
    20. }else
    21. {
    22. printf("没有该元素!\n");
    23. }
    24. i = ListInsert(&L,4,0);
    25. if(i){
    26. printf("插入成功!\n");
    27. }else
    28. {
    29. printf("插入失败!\n");
    30. }
    31. printf("打印顺序表:\n");
    32. DisplayList(L);
    33. i = ListDelete(&L,4,&e);
    34. if(i){
    35. printf("删除成功!\n");
    36. }else
    37. {
    38. printf("删除失败!\n");
    39. }
    40. printf("打印顺序表:\n");
    41. DisplayList(L);
    42. return 0;
    43. }
    1. //链式表
    2. #include<stdlib.h>
    3. #include<stdio.h>
    4. typedef struct LNode{
    5. ElemType data;
    6. struct LNode * next;
    7. }LinkNode;
    8. //0.整体创建 头插法
    9. void CreateListF(LinkNode ** L,ElemType a[],int n){
    10. LinkNode * s;
    11. int i = 0;
    12. (*L) = (LinkNode *)malloc(sizeof(LinkNode));
    13. (*L)->next = NULL;
    14. while(i<n){
    15. s = (LinkNode *)malloc(sizeof(LinkNode));
    16. s->data = a[i];
    17. s->next = (*L)->next;
    18. (*L)->next = s;
    19. i++;
    20. }
    21. }
    22. //0.整体创建 尾插法
    23. void CreateListR(LinkNode ** L,ElemType a[],int n){
    24. LinkNode * s,* r;
    25. int i = 0;
    26. (*L) = (LinkNode *)malloc(sizeof(LinkNode));
    27. r = (*L);
    28. while(i<n){
    29. s = (LinkNode *)malloc(sizeof(LinkNode));
    30. s->data = a[i];
    31. r->next = s;
    32. r = s;
    33. i++;
    34. }
    35. r->next = NULL;
    36. }
    37. //1.初始化顺序表
    38. void InitList(LinkNode ** L){
    39. (*L) = (LinkNode *)malloc(sizeof(LinkNode));
    40. (*L)->next = NULL;
    41. }
    42. //2.销毁顺序表
    43. void DestroyList(LinkNode ** L){
    44. LinkNode * pre = *L,*p = (*L)->next;
    45. while(p != NULL){
    46. free(pre);
    47. pre = p;
    48. p = pre->next;
    49. }
    50. free(pre);
    51. }
    52. //3.判断顺序表是否为空
    53. int ListEmpty(LinkNode * L){
    54. return (L->next == NULL);
    55. }
    56. //4.返回顺序表长度
    57. int ListLength(LinkNode * L){
    58. int i = 0;
    59. LinkNode * p = L;
    60. while(p->next != NULL){
    61. i++;
    62. p = p->next;
    63. }
    64. return i;
    65. }
    66. //5.输出顺序表
    67. void DisplayList(LinkNode * L){
    68. LinkNode * p = L->next;
    69. while(p != NULL){
    70. printf("%d\t",p->data);//顺序表的元素类型不一定为int,故此处语句用 %d 并不合适,还需做处理,此处仅当演示作用,后面的判断对比处,也不要计较,理解思路即可
    71. p = p->next;
    72. }
    73. printf("\n");
    74. }
    75. //6.求顺序表中的某个元素
    76. int GetElem(LinkNode * L,int i,ElemType * e){
    77. int j = 0;
    78. LinkNode * p = L;
    79. if(i<1){
    80. return 0;
    81. }
    82. while(j<i && p->next != NULL){
    83. p = p->next;
    84. j++;
    85. }
    86. if(p == NULL){
    87. return 0;
    88. }else{
    89. *e = p->data;
    90. return 1;
    91. }
    92. }
    93. //7.找元素返回序号
    94. int LocateElem(LinkNode * L,ElemType e){
    95. int i = 0;
    96. LinkNode * p = L;
    97. while(p->next != NULL && p->data != e){
    98. p = p->next;
    99. i++;
    100. }
    101. if(p->next == NULL){
    102. return 0;
    103. }else{
    104. return i;
    105. }
    106. }
    107. //8.插入元素
    108. int ListInsert(LinkNode ** L,int i,ElemType e){
    109. int j = 0;
    110. LinkNode * p = (*L),*s;
    111. if(i<0){
    112. return 0;
    113. }
    114. while(j<i-1 && p->next != NULL){
    115. j++;
    116. p = p->next;
    117. }
    118. if(p->next == NULL){
    119. return 0;
    120. }else{
    121. s = (LinkNode *)malloc(sizeof(LinkNode));
    122. s->data = e;
    123. s->next = p->next;
    124. p->next = s;
    125. return 1;
    126. }
    127. }
    128. //9.删除元素
    129. int ListDelete(LinkNode ** L,int i,ElemType *e){
    130. int j = 0;
    131. LinkNode * p = *L, * s;
    132. if(i<1){
    133. return 0;
    134. }
    135. while(j < i-1 && p->next != NULL){
    136. j++;
    137. p = p->next;
    138. }
    139. if(p == NULL){
    140. return 0;
    141. }else{
    142. s = p->next;
    143. if(s == NULL){
    144. return 0;
    145. }
    146. *e = s->data;
    147. p->next =s->next;
    148. free(s);
    149. return 1;
    150. }
    151. }