• 头插法 尾插法建立链表(带头节点、不带头节点)


    头插法建立带头节点链表

    1. void Createhead(linklist& l)//头插法带头节点
    2. {
    3. l = new list;
    4. linklist p ;
    5. l->next = NULL;
    6. cout << "输入链表长度:"<
    7. int n;
    8. cin >> n;
    9. for (int i = 0; i < n; i++)
    10. {
    11. p = new list;
    12. cin >> p->data;
    13. p->next = l->next;
    14. l->next = p;
    15. }
    16. }

    尾插法建立带头节点链表

    1. void Createrear(linklist& l)//尾插法
    2. {
    3. l = new list;
    4. linklist p,r;
    5. l->next = NULL;
    6. r = l;
    7. cout << "输入链表长度:" << endl;
    8. int n;
    9. cin >> n;
    10. for (int i = 0; i < n; i++)
    11. {
    12. p = new list;
    13. cin >> p->data;
    14. p->next = NULL;
    15. r->next = p;
    16. r = p;
    17. }
    18. }

    头插法建立不带头节点链表

    1. void Createlist1(linklist& l)//头插法不带头节点
    2. {
    3. l = new list;
    4. linklist p;
    5. l->next = NULL;
    6. cout << "输入链表长度:" << endl;
    7. int n;
    8. cin >> n;
    9. cin >> l->data;
    10. for (int i = 1; i < n; i++)
    11. {
    12. p = new list;
    13. cin >> p->data;
    14. p->next = l;
    15. l = p;
    16. }
    17. }

    尾插法建立不带头节点链表

    1. void Createlist2(linklist& l)//尾插法不带头节点
    2. {
    3. l = new list;
    4. linklist p,q;
    5. l->next = NULL;
    6. cout << "输入链表长度:" << endl;
    7. int n;
    8. cin >> n;
    9. cin >> l->data;
    10. q = l;
    11. for (int i = 1; i < n; i++)
    12. {
    13. p = new list;
    14. cin >> p->data;
    15. q->next = p;
    16. q = p;
    17. }
    18. }

    完整代码:

    1. #include
    2. using namespace std;
    3. typedef struct list
    4. {
    5. int data;
    6. list* next;
    7. }list, * linklist;
    8. void Createhead(linklist& l)//头插法带头节点
    9. {
    10. l = new list;
    11. linklist p ;
    12. l->next = NULL;
    13. cout << "输入链表长度:"<
    14. int n;
    15. cin >> n;
    16. for (int i = 0; i < n; i++)
    17. {
    18. p = new list;
    19. cin >> p->data;
    20. p->next = l->next;
    21. l->next = p;
    22. }
    23. }
    24. void Createrear(linklist& l)//尾插法
    25. {
    26. l = new list;
    27. linklist p,r;
    28. l->next = NULL;
    29. r = l;
    30. cout << "输入链表长度:" << endl;
    31. int n;
    32. cin >> n;
    33. for (int i = 0; i < n; i++)
    34. {
    35. p = new list;
    36. cin >> p->data;
    37. p->next = NULL;
    38. r->next = p;
    39. r = p;
    40. }
    41. }
    42. void Createlist1(linklist& l)//头插法不带头节点
    43. {
    44. l = new list;
    45. linklist p;
    46. l->next = NULL;
    47. cout << "输入链表长度:" << endl;
    48. int n;
    49. cin >> n;
    50. cin >> l->data;
    51. for (int i = 1; i < n; i++)
    52. {
    53. p = new list;
    54. cin >> p->data;
    55. p->next = l;
    56. l = p;
    57. }
    58. }
    59. void Createlist2(linklist& l)//尾插法不带头节点
    60. {
    61. l = new list;
    62. linklist p,q;
    63. l->next = NULL;
    64. cout << "输入链表长度:" << endl;
    65. int n;
    66. cin >> n;
    67. cin >> l->data;
    68. q = l;
    69. for (int i = 1; i < n; i++)
    70. {
    71. p = new list;
    72. cin >> p->data;
    73. q->next = p;
    74. q = p;
    75. }
    76. }
    77. void Printlist1(linklist l)
    78. {
    79. linklist p = l->next;
    80. while (p!=NULL)
    81. {
    82. cout << p->data << " ";
    83. p = p->next;
    84. }
    85. cout << endl;
    86. }
    87. void Printlist2(linklist l)
    88. {
    89. linklist p = l;
    90. while (p != NULL)
    91. {
    92. cout << p->data << " ";
    93. p = p->next;
    94. }
    95. cout << endl;
    96. }
    97. int main()
    98. {
    99. linklist L1,L2,L3,L4;
    100. cout << "头插法(带头节点)建立链表:" << endl;
    101. Createhead(L1);
    102. cout << "尾插法(带头节点)建立链表:" << endl;
    103. Createrear(L2);
    104. cout << "头插法(带头节点)链表:" << endl;
    105. Printlist1(L1);
    106. cout << "尾插法(带头节点)链表:" << endl;
    107. Printlist1(L2);
    108. cout << "头插法(不带头节点)建立链表:" << endl;
    109. Createlist1(L3);
    110. cout << "尾插法(不带头节点)建立链表:" << endl;
    111. Createlist2(L4);
    112. cout << "头插法(不带头节点)链表:" << endl;
    113. Printlist2(L3);
    114. cout << "尾插法(不带头节点)链表:" << endl;
    115. Printlist2(L4);
    116. }

     

  • 相关阅读:
    唐朝边戍 NFT 作品集 2.0 上线市场平台,体验中国古代文化的魅力
    logstash安装zabbix插件报错解决
    哈希/散列--哈希表[思想到结构]
    MindSpore能把两个实数组合成复数的操作算子么
    dll文件【C#】
    VLAN 配置案例
    【python入门】函数,类和对象
    JS defineProperty详解
    分销系统功能有哪些?好的分销比例如何设定?
    为什么ArcGIS添加的TIFF栅格数据是一片纯色
  • 原文地址:https://blog.csdn.net/qq_74156152/article/details/133048023