• 双向链表



    1. // list.cpp : Defines the entry point for the console application.
    2. //
    3. #include "stdafx.h"
    4. #include <iostream>
    5. using namespace std;
    6. typedef struct list_node {
    7. int data;
    8. list_node *left;
    9. list_node *right;
    10. } Node;
    11. Node *create_node(int data)
    12. {
    13. Node *node = new Node();
    14. node->data = data;
    15. node->left = node->right = NULL;
    16. return node;
    17. }
    18. void delete_node( Node * p )
    19. {
    20. delete p;
    21. p = NULL ;
    22. }
    23. void insert_tail(Node *root , int data)
    24. {
    25. Node *node = create_node( data );
    26. Node *last_node = root;
    27. while(last_node->right != NULL) last_node = last_node->right;
    28. last_node->right = node;
    29. node->left = last_node;
    30. }
    31. Node *find_node(Node *root , int data )
    32. {
    33. Node *node = root ;
    34. while(node != NULL) {
    35. if( data == node->data ) {
    36. return node;
    37. }
    38. node = node->right;
    39. }
    40. return NULL;
    41. }
    42. void insert_back_node(Node *root, int dat , int data )
    43. {
    44. Node *node = find_node(root,dat);
    45. if( node == NULL || node->left == NULL ) return;
    46. Node *new_node = create_node( data );
    47. node->left->right = new_node;
    48. new_node->right = node;
    49. }
    50. void insert_front_node(Node *root, int dat , int data )
    51. {
    52. Node *node = find_node(root,dat);
    53. if( node == NULL ) return;
    54. Node *new_node = create_node( data );
    55. new_node->right = node->right;
    56. new_node->left = node;
    57. node->right = new_node;
    58. }
    59. void remote_node ( Node *root,int data )
    60. {
    61. Node *node = find_node ( root , data );
    62. if( node == NULL ) {
    63. return ;
    64. } else if ( node->left == NULL ) {
    65. node->right->left = NULL;
    66. } else if ( node->right == NULL ) {
    67. node->left->right = NULL ;
    68. } else {
    69. node->left->right = node->right;
    70. node->right->left = node->left;
    71. }
    72. delete_node( node );
    73. }
    74. void traversal( Node *root )
    75. {
    76. Node *last_node = root;
    77. do{
    78. cout<<last_node->data<<endl ;
    79. last_node = last_node->right;
    80. } while(last_node != NULL ) ;
    81. }
    82. int main(int argc, char* argv[])
    83. {
    84. Node *root = create_node(1);
    85. insert_tail(root,4);
    86. insert_tail(root,5);
    87. insert_tail(root,800);
    88. insert_tail(root,6);
    89. insert_tail(root,7);
    90. Node *p = find_node(root,5);
    91. cout << p <<endl;
    92. if(p!=NULL) cout<<p->data <<endl;
    93. insert_back_node(root,5,20);
    94. insert_front_node(root,1,30);
    95. remote_node ( root , 6 ) ;
    96. traversal( root );
    97. return 0;
    98. }





  • 相关阅读:
    Firefox for macOS 标签关闭按钮设置在左侧
    Nginx 非编译安装 stream 模块实现四层负载均衡
    苹果手机卡死,强制关机方法
    Git安装完成,文件夹下右键菜单不出现出现Git Bash Here 和Git UI Here问题
    AESUtil
    若依框架清空select2选择
    RSAUtils
    springboot 延时任务
    Mqtt的坑,真的坑
    springboot 支付宝支付业务网页端扫码
  • 原文地址:https://www.cnblogs.com/fysola/p/4862563.html
Copyright © 2020-2023  润新知