当前位置:   article > 正文

数据结构 - 反转单链表(C++)_单链表反转c++语言

单链表反转c++语言

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击人工智能教程

  1. /*
  2. * 反转单链表的循环算法(C++)- by Chimomo
  3. */
  4. #include <iostream>
  5. #define NULL 0
  6. using namespace std;
  7. struct Node {
  8. char data;
  9. Node *next;
  10. };
  11. /**
  12. * Create single linked list.
  13. * @return The head pointer.
  14. */
  15. Node *create() {
  16. Node *head = NULL;
  17. Node *rear = head;
  18. Node *p; // The pointer points to new created node.
  19. char tmp;
  20. do {
  21. cout << "Please input positive integer or char '#' to stop: ";
  22. cin >> tmp;
  23. if (tmp != '#') {
  24. p = new Node;
  25. p->data = tmp;
  26. p->next = NULL;
  27. if (head == NULL) {
  28. head = p;
  29. } else {
  30. rear->next = p;
  31. }
  32. rear = p;
  33. }
  34. } while (tmp != '#');
  35. return head;
  36. }
  37. /**
  38. * Print the single linked list.
  39. * @param head The head pointer.
  40. */
  41. void print(Node *head) {
  42. cout << "The current list is: ";
  43. Node *p = head;
  44. if (head != NULL) {
  45. do {
  46. cout << p->data;
  47. cout << ' ';
  48. p = p->next;
  49. } while (p != NULL);
  50. }
  51. cout << "\r\n";
  52. }
  53. /**
  54. * Reverse the single linked list circularly.
  55. * @param head The head pointer. Use & here since the function body changed the head pointer.
  56. */
  57. void reverse(Node *&head) {
  58. if (head == NULL) {
  59. return;
  60. }
  61. Node *pre, *cur, *ne;
  62. pre = head;
  63. cur = head->next;
  64. while (cur) {
  65. ne = cur->next; // Store next pointer.
  66. cur->next = pre; // Reverse the current code pointer.
  67. pre = cur;
  68. cur = ne;
  69. }
  70. head->next = NULL;
  71. head = pre;
  72. }
  73. int main() {
  74. Node *list = create();
  75. print(list);
  76. reverse(list);
  77. print(list);
  78. return 0;
  79. }
  80. // Output:
  81. /*
  82. Please input positive integer or char '#' to stop:1
  83. 1
  84. Please input positive integer or char '#' to stop:5
  85. 5
  86. Please input positive integer or char '#' to stop:8
  87. 8
  88. Please input positive integer or char '#' to stop:3
  89. 3
  90. Please input positive integer or char '#' to stop:2
  91. 2
  92. Please input positive integer or char '#' to stop:7
  93. 7
  94. Please input positive integer or char '#' to stop:9
  95. 9
  96. Please input positive integer or char '#' to stop:#
  97. #
  98. The current list is: 1 5 8 3 2 7 9
  99. The current list is: 9 7 2 3 8 5 1
  100. */
  1. /*
  2. * 反转单链表的递归算法(C++)- by Chimomo
  3. */
  4. #include <iostream>
  5. #define NULL 0
  6. using namespace std;
  7. struct Node {
  8. char data;
  9. Node *next;
  10. };
  11. /**
  12. * Create single linked list.
  13. * @return The single linked list.
  14. */
  15. Node *create() {
  16. Node *head = NULL;
  17. Node *rear = head;
  18. Node *p; // The pointer points to the new created node.
  19. char tmp;
  20. do {
  21. cout << "Please input positive integer or char '#' to stop: ";
  22. cin >> tmp;
  23. if (tmp != '#') {
  24. p = new Node;
  25. p->data = tmp;
  26. p->next = NULL;
  27. if (head == NULL) {
  28. head = p;
  29. } else {
  30. rear->next = p;
  31. }
  32. rear = p;
  33. }
  34. } while (tmp != '#');
  35. return head;
  36. }
  37. /**
  38. * Print the single linked list.
  39. * @param head The head pointer.
  40. */
  41. void print(Node *head) {
  42. cout << "The current list is: ";
  43. Node *p = head;
  44. if (head != NULL) {
  45. do {
  46. cout << p->data;
  47. cout << ' ';
  48. p = p->next;
  49. } while (p != NULL);
  50. }
  51. cout << "\r\n";
  52. }
  53. /**
  54. * Reverse the single linked list recursively.
  55. * @param p The pointer.
  56. * @param head The head pointer. Use & here since the function body changed the head pointer.
  57. * @return The reversed single linked list head pointer.
  58. */
  59. Node *reverse(Node *p, Node *&head) {
  60. if (p == NULL || p->next == NULL) {
  61. head = p;
  62. return p;
  63. }
  64. Node *tmp = reverse(p->next, head);
  65. tmp->next = p;
  66. p->next = NULL; // To prevent forming a ring.
  67. return p;
  68. }
  69. int main() {
  70. Node *list = create();
  71. print(list);
  72. reverse(list, list);
  73. print(list);
  74. return 0;
  75. }
  76. // Output:
  77. /*
  78. Please input positive integer or char '#':1
  79. 1
  80. Please input positive integer or char '#':2
  81. 2
  82. Please input positive integer or char '#':4
  83. 4
  84. Please input positive integer or char '#':6
  85. 6
  86. Please input positive integer or char '#':3
  87. 3
  88. Please input positive integer or char '#':8
  89. 8
  90. Please input positive integer or char '#':7
  91. 7
  92. Please input positive integer or char '#':9
  93. 9
  94. Please input positive integer or char '#':0
  95. 0
  96. Please input positive integer or char '#':#
  97. #
  98. The current list is: 1 2 4 6 3 8 7 9 0
  99. The current list is: 0 9 7 8 3 6 4 2 1
  100. */

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/1019631
推荐阅读
相关标签
  

闽ICP备14008679号