1. #include <stdio.h>
    2. #include <stdlib.h>
    3. typedef struct line {
    4. struct line * prior;
    5. int data;
    6. struct line * next;
    7. }line;
    8. //双链表的创建
    9. line* initLine(line * head);
    10. //双链表插入元素,add表示插入位置
    11. line * insertLine(line * head, int data, int add);
    12. //双链表删除指定元素
    13. line * delLine(line * head, int data);
    14. //双链表中查找指定元素
    15. int selectElem(line * head, int elem);
    16. //双链表中更改指定位置节点中存储的数据,add表示更改位置
    17. line *amendElem(line * p, int add, int newElem);
    18. //输出双链表的实现函数
    19. void display(line * head);
    20. int main() {
    21. line * head = NULL;
    22. //创建双链表
    23. printf("初始链表为:\n");
    24. head = initLine(head);
    25. display(head);
    26. //在表中第 3 的位置插入元素 7
    27. printf("在表中第 3 的位置插入新元素 7:\n");
    28. head = insertLine(head, 7, 3);
    29. display(head);
    30. //表中删除元素 2
    31. printf("删除元素 2:\n");
    32. head = delLine(head, 2);
    33. display(head);
    34. printf("元素 3 的位置是:%d\n", selectElem(head, 3));
    35. //表中第 3 个节点中的数据改为存储 6
    36. printf("将第 3 个节点存储的数据改为 6:\n");
    37. head = amendElem(head, 3, 6);
    38. display(head);
    39. return 0;
    40. }
    41. line* initLine(line * head) {
    42. int i = 0;
    43. line * list = NULL;
    44. //从内存中申请一片内存
    45. head = (line*)malloc(sizeof(line));
    46. //初始化
    47. head->prior = NULL;
    48. head->next = NULL;
    49. head->data = 1;
    50. //我们需要保证head的位置不变,所以连接链表由list执行
    51. list = head;
    52. for (i = 2; i <= 3; i++) {
    53. //创建链表体
    54. line * body = (line*)malloc(sizeof(line));
    55. body->prior = NULL;
    56. body->next = NULL;
    57. //i=2, 3
    58. body->data = i;
    59. //让链表的头下一个元素指向体
    60. list->next = body;
    61. body->prior = list;
    62. list = list->next;
    63. /*
    64. list = head;
    65. head ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    66. create body
    67. list->next = body;
    68. head
    69. list -> body ~~~~~~~~~~~~~~~~~~~~~~~~
    70. body->prior = list'
    71. head
    72. list <- -> body ~~~~~~~~~~~~~~~
    73. list - >next = body;
    74. list
    75. head -> body ~~~~~~~~~~~~~~~~
    76. */
    77. }
    78. return head;
    79. }
    80. line * insertLine(line * head, int data, int add) {
    81. //新建数据域为data的结点
    82. line * temp = (line*)malloc(sizeof(line));
    83. temp->data = data;
    84. temp->prior = NULL;
    85. temp->next = NULL;
    86. //插入到链表头,要特殊考虑
    87. if (add == 1) {
    88. temp->next = head;
    89. head->prior = temp;
    90. head = temp;
    91. }
    92. else {
    93. int i = 0;
    94. line * body = head;
    95. //找到要插入位置的前一个结点
    96. for (i = 1; i < add - 1; i++) {
    97. body = body->next;
    98. if (body == NULL) {
    99. printf("插入位置有误\n");
    100. break;
    101. }
    102. }
    103. if (body) {
    104. //判断条件为真,说明插入位置为链表尾
    105. if (body->next == NULL) {
    106. body->next = temp;
    107. temp->prior = body;
    108. }
    109. else {
    110. body->next->prior = temp;
    111. temp->next = body->next;
    112. body->next = temp;
    113. temp->prior = body;
    114. }
    115. }
    116. }
    117. return head;
    118. }
    119. line * delLine(line * head, int data) {
    120. line * temp = head;
    121. //遍历链表
    122. while (temp) {
    123. //判断当前结点中数据域和data是否相等,若相等,摘除该结点
    124. if (temp->data == data) {
    125. temp->prior->next = temp->next;
    126. temp->next->prior = temp->prior;
    127. free(temp);
    128. return head;
    129. }
    130. temp = temp->next;
    131. }
    132. printf("链表中无该数据元素\n");
    133. return head;
    134. }
    135. //head为原双链表,elem表示被查找元素
    136. int selectElem(line * head, int elem) {
    137. //新建一个指针t,初始化为头指针 head
    138. line * t = head;
    139. int i = 1;
    140. while (t) {
    141. if (t->data == elem) {
    142. return i;
    143. }
    144. i++;
    145. t = t->next;
    146. }
    147. //程序执行至此处,表示查找失败
    148. return -1;
    149. }
    150. //更新函数,其中,add 表示更改结点在双链表中的位置,newElem 为新数据的值
    151. line *amendElem(line * p, int add, int newElem) {
    152. int i = 0;
    153. line * temp = p;
    154. //遍历到被删除结点
    155. for (i = 1; i < add; i++) {
    156. temp = temp->next;
    157. if (temp == NULL) {
    158. printf("更改位置有误!\n");
    159. break;
    160. }
    161. }
    162. if (temp) {
    163. temp->data = newElem;
    164. }
    165. return p;
    166. }
    167. //输出链表的功能函数
    168. void display(line * head) {
    169. line * temp = head;
    170. while (temp) {
    171. if (temp->next == NULL) {
    172. printf("%d\n", temp->data);
    173. }
    174. else {
    175. printf("%d->", temp->data);
    176. }
    177. temp = temp->next;
    178. }
    179. }