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