Guest User

Untitled

a guest
Apr 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. struct Node {
  8. int value;
  9. shared_ptr<Node> next;
  10. explicit Node(int value) : value(value) {}
  11. };
  12.  
  13. /**
  14. * 构建单向链表
  15. * @return
  16. */
  17. decltype(auto) build() {
  18. auto head = make_shared<Node>(100);
  19. auto temp = head;
  20. for (int i = 0; i < 10; ++i) {
  21. temp->next = make_shared<Node>(i);
  22. temp = temp->next;
  23. }
  24. return head;
  25. }
  26.  
  27. /**
  28. * 构建交叉链表
  29. * @return
  30. */
  31. decltype(auto) build_intersect() {
  32. auto one = build();
  33. auto two = build();
  34. two->next->next->next->next = one->next->next->next;
  35. return make_pair(one, two);
  36. }
  37.  
  38. /**
  39. * 构建循环链表
  40. * @return
  41. */
  42. decltype(auto) build_loop() {
  43. auto one = build();
  44. auto tail = one;
  45. auto get = [](int n, shared_ptr<Node> node) -> shared_ptr<Node> {
  46. for (int i = 1; i < n && node != nullptr; i++) {
  47. node = node->next;
  48. }
  49. return node;
  50. };
  51. while (tail->next != nullptr)tail = tail->next;
  52. tail->next = get(4, one);
  53. return one;
  54. }
  55.  
  56. /**
  57. * 递归逆序
  58. * @param node
  59. * @return
  60. */
  61. shared_ptr<Node> reverse_recursive(shared_ptr<Node> node) {
  62. if (node->next != nullptr) {
  63. shared_ptr<Node> head = reverse_recursive(node->next);
  64. node->next->next = node;
  65. node->next = nullptr;
  66. return head;
  67. }
  68. return node;
  69. }
  70.  
  71. /**
  72. * 递归逆序
  73. * @param node
  74. * @return
  75. */
  76. shared_ptr<Node> reverse(shared_ptr<Node> node) {
  77. shared_ptr<Node> head = nullptr;
  78. auto temp = node->next;
  79. while (temp != nullptr) {
  80. node->next = head;
  81. head = node;
  82. node = temp;
  83. temp = temp->next;
  84. }
  85. node->next = head;
  86. return node;
  87.  
  88. }
  89.  
  90. /**
  91. * 判断两条链表是否相交
  92. * @param t1
  93. * @param t2
  94. * @return
  95. */
  96. bool intersect(shared_ptr<Node> &t1, shared_ptr<Node> t2) {
  97. while (t1->next != nullptr)t1 = t1->next;
  98. while (t2->next != nullptr)t2 = t2->next;
  99. return t1 == t2;
  100. }
  101.  
  102. /**
  103. * 判断是否是循环链表
  104. * @param t 头
  105. * @return <是否是循环链表, 相遇点>
  106. */
  107. pair<bool, shared_ptr<Node>> has_loop(const shared_ptr<Node> &t) {
  108. auto t1 = t->next;
  109. auto t2 = t->next->next;
  110. while (t1 != nullptr && t2 != nullptr && t1 != t2) {
  111. t1 = t1->next;
  112. t2 = t2->next->next;
  113. }
  114. return make_pair(t1 == t2, t1);
  115. }
  116.  
  117. /**
  118. * 获取环入口
  119. * @param h 头
  120. * @param t 相交点
  121. * @return
  122. */
  123. decltype(auto) get_loop_node(const shared_ptr<Node> &h, const shared_ptr<Node> &t) {
  124. auto x = h, y = t;
  125. while (x != y) {
  126. x = x->next;
  127. y = y->next;
  128. }
  129. return x;
  130. }
  131.  
  132. void print(shared_ptr<Node> node) {
  133. while (node != nullptr) {
  134. cout << node->value << " ";
  135. node = node->next;
  136. }
  137. cout << endl;
  138. }
  139.  
  140.  
  141. int main() {
  142. auto common = build();
  143. print(common);
  144.  
  145. auto head = build_loop();
  146. auto result = has_loop(head);
  147. if (result.first) {
  148. cout << result.second->value << endl;
  149. cout << get_loop_node(head, result.second)->value << endl;
  150. }
  151. return 0;
  152. }
Add Comment
Please, Sign In to add comment