• 数据结构-双向链表操作


    先创建好一个双向链表:

    代码: 

    1. #include
    2. #include
    3. //定义结点信息
    4. typedef struct Line{
    5. struct Line*prior;
    6. int data;
    7. struct Line*next;
    8. }line;
    9. //函数声明
    10. line*initLine(line*head);//初始化
    11. void display(line*head);//打印链表
    12. int main()
    13. {
    14. line*p=NULL;
    15. p=initLine(p);
    16. display(p);
    17. return 0;
    18. }
    19. line*initLine(line*head){
    20. //创建首元结点
    21. head=(line*)malloc(sizeof(line));
    22. head->prior=NULL;
    23. head->data=1;
    24. head->next=NULL;
    25. line*temp=head;
    26. for(int i=2;i<4;i++)
    27. {
    28. line*body=(line*)malloc(sizeof(line));
    29. body->prior=NULL;
    30. body->data=i;
    31. body->next=NULL;
    32. temp->next=body;
    33. body->prior=temp;
    34. temp=body;
    35. }
    36. return head;
    37. }
    38. void display(line*head){
    39. line*t=head;
    40. while(t){
    41. if(t->next==NULL){
    42. printf("%d\n",t->data);
    43. }else{
    44. printf("%d <-> ",t->data);
    45. }
    46. t=t->next;
    47. }
    48. }

     结果:

    1、双向链表增加节点

    (1)加表头

    假设新元素节点为 temp,表头节点为 head,则需要做以下 2 步操作即可:

    1. temp->next=head; head->prior=temp;
    2. 将 head 移至 temp,重新指向新的表头;
    1. //新建数据域为data的结点
    2. line * temp=(line*)malloc(sizeof(line));
    3. temp->data=data;
    4. temp->prior=NULL;
    5. temp->next=NULL;
    6. //插入到链表头,要特殊考虑
    7. if (add==1) {
    8. temp->next=head;
    9. head->prior=temp;
    10. head=temp;

    (2)加表中

    1. 新节点先与其直接后继节点建立双层逻辑关系;
    2. 新节点的直接前驱节点与之建立双层逻辑关系;
    1. line * body=head;
    2. //找到要插入位置的前一个结点
    3. for (int i=1; i-1; i++) {
    4. body=body->next;
    5. }
    6. body->next->prior=temp;
    7. temp->next=body->next;
    8. body->next=temp;
    9. temp->prior=body;

    (3)加表尾

    1. 找到双链表中最后一个节点;
    2. 让新节点与最后一个节点进行双层逻辑关系;
    1. //判断条件为真,说明插入位置为链表尾
    2. if (body->next==NULL) {
    3. body->next=temp;
    4. temp->prior=body;
    5. }

    2、双向链表删除节点

     只需遍历链表找到要删除的结点,然后将该节点从表中摘除即可

    1. //删除结点的函数,data为要删除结点的数据域的值
    2. line * delLine(line * head,int data){
    3. line * temp=head;
    4. //遍历链表
    5. while (temp) {
    6. //判断当前结点中数据域和data是否相等,若相等,摘除该结点
    7. if (temp->data==data) {
    8. temp->prior->next=temp->next;
    9. temp->next->prior=temp->prior;
    10. free(temp);
    11. return head;
    12. }
    13. temp=temp->next;
    14. }
    15. printf("链表中无该数据元素");
    16. return head;
    17. }

    3、双向链表查找节点

    双向链表同单链表一样,都仅有一个头指针。因此,双链表查找指定元素的实现同单链表类似,都是从表头依次遍历表中元素。

    1. //head为原双链表,elem表示被查找元素
    2. int selectElem(line * head,int elem){
    3. //新建一个指针t,初始化为头指针 head
    4. line * t=head;
    5. int i=1;
    6. while (t) {
    7. if (t->data==elem) {
    8. return i;
    9. }
    10. i++;
    11. t=t->next;
    12. }
    13. //程序执行至此处,表示查找失败
    14. return -1;
    15. }

    4、双向链表修改节点

    通过遍历找到存储有该数据元素的结点,直接更改其数据域即可。

    1. //更新函数,其中,add 表示更改结点在双链表中的位置,newElem 为新数据的值
    2. line *amendElem(line * p,int add,int newElem){
    3. line * temp=p;
    4. //遍历到被删除结点
    5. for (int i=1; i
    6. temp=temp->next;
    7. }
    8. temp->data=newElem;
    9. return p;
    10. }

    完整代码:

    1. #include
    2. #include
    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. head=initLine(head);
    24. display(head);
    25. //在表中第 3 的位置插入元素 7
    26. head=insertLine(head, 7, 3);
    27. display(head);
    28. //表中删除元素 2
    29. head=delLine(head, 2);
    30. display(head);
    31. printf("元素 3 的位置是:%d\n",selectElem(head,3));
    32. //表中第 3 个节点中的数据改为存储 6
    33. head = amendElem(head,3,6);
    34. display(head);
    35. return 0;
    36. }
    37. line* initLine(line * head){
    38. head=(line*)malloc(sizeof(line));
    39. head->prior=NULL;
    40. head->next=NULL;
    41. head->data=1;
    42. line * list=head;
    43. for (int i=2; i<=5; i++) {
    44. line * body=(line*)malloc(sizeof(line));
    45. body->prior=NULL;
    46. body->next=NULL;
    47. body->data=i;
    48. list->next=body;
    49. body->prior=list;
    50. list=list->next;
    51. }
    52. return head;
    53. }
    54. line * insertLine(line * head,int data,int add){
    55. //新建数据域为data的结点
    56. line * temp=(line*)malloc(sizeof(line));
    57. temp->data=data;
    58. temp->prior=NULL;
    59. temp->next=NULL;
    60. //插入到链表头,要特殊考虑
    61. if (add==1) {
    62. temp->next=head;
    63. head->prior=temp;
    64. head=temp;
    65. }else{
    66. line * body=head;
    67. //找到要插入位置的前一个结点
    68. for (int i=1; i-1; i++) {
    69. body=body->next;
    70. }
    71. //判断条件为真,说明插入位置为链表尾
    72. if (body->next==NULL) {
    73. body->next=temp;
    74. temp->prior=body;
    75. }else{
    76. body->next->prior=temp;
    77. temp->next=body->next;
    78. body->next=temp;
    79. temp->prior=body;
    80. }
    81. }
    82. return head;
    83. }
    84. line * delLine(line * head,int data){
    85. line * temp=head;
    86. //遍历链表
    87. while (temp) {
    88. //判断当前结点中数据域和data是否相等,若相等,摘除该结点
    89. if (temp->data==data) {
    90. temp->prior->next=temp->next;
    91. temp->next->prior=temp->prior;
    92. free(temp);
    93. return head;
    94. }
    95. temp=temp->next;
    96. }
    97. printf("链表中无该数据元素");
    98. return head;
    99. }
    100. //head为原双链表,elem表示被查找元素
    101. int selectElem(line * head,int elem){
    102. //新建一个指针t,初始化为头指针 head
    103. line * t=head;
    104. int i=1;
    105. while (t) {
    106. if (t->data==elem) {
    107. return i;
    108. }
    109. i++;
    110. t=t->next;
    111. }
    112. //程序执行至此处,表示查找失败
    113. return -1;
    114. }
    115. //更新函数,其中,add 表示更改结点在双链表中的位置,newElem 为新数据的值
    116. line *amendElem(line * p,int add,int newElem){
    117. line * temp=p;
    118. //遍历到被删除结点
    119. for (int i=1; i
    120. temp=temp->next;
    121. }
    122. temp->data=newElem;
    123. return p;
    124. }
    125. //输出链表的功能函数
    126. void display(line * head){
    127. line * temp=head;
    128. while (temp) {
    129. if (temp->next==NULL) {
    130. printf("%d\n",temp->data);
    131. }else{
    132. printf("%d->",temp->data);
    133. }
    134. temp=temp->next;
    135. }
    136. }

    输出结果:

  • 相关阅读:
    spring7:总结56
    Curve 块存储应用实践 -- iSCSI
    实验四 OR指令设计实验【计算机组成原理】
    VBA 浏览文件夹对话框调用的几种方法
    08_一句话让你人间清醒
    【RabbitMQ 实战】12 镜像队列
    蓝桥杯 枚举算法 (c++)
    后门程序3(补充)
    ubuntu22.04 耳机没声音 [已解决]
    谷粒商城--整合Elasticsearch和商品的上架
  • 原文地址:https://blog.csdn.net/qq_51701007/article/details/126171418