成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

五個解決辦法教你C++中檢測鏈表中的循環

開發 后端
通過5個解決方案教你C++中檢測鏈表中的循環,快來看看,是否對你有幫助!

五個解決辦法教你C++中檢測鏈表中的循環

給定一個鏈表,檢查鏈表是否有循環。下圖顯示了帶有循環的鏈表。

五個解決辦法教你C++中檢測鏈表中的循環

 

以下是執行此操作的不同方法 

 

解決方案1:散列方法:

遍歷該列表,并將節點地址始終放在哈希表中。在任何時候,如果達到NULL,則返回false,如果當前節點的下一個指向Hash中先前存儲的任何節點,則返回true。

  1. #include <bits/stdc++.h> 
  2. using namespace std; 
  3. struct Node { 
  4.     int data; 
  5.     struct Node* next
  6. }; 
  7.   
  8. void push(struct Node** head_ref, int new_data) 
  9.     struct Node* new_node = new Node; 
  10.     new_node->data = new_data; 
  11.     new_node->next = (*head_ref); 
  12.     (*head_ref) = new_node; 
  13. bool detectLoop(struct Node* h) 
  14.     unordered_set<Node*> s; 
  15.     while (h != NULL) { 
  16.         if (s.find(h) != s.end()) 
  17.             return true
  18.         s.insert(h); 
  19.   
  20.         h = h->next
  21.     } 
  22.   
  23.     return false
  24. int main() 
  25.     struct Node* head = NULL
  26.   
  27.     push(&head, 20); 
  28.     push(&head, 4); 
  29.     push(&head, 15); 
  30.     push(&head, 10); 
  31.     head->next->next->next->next = head; 
  32.   
  33.     if (detectLoop(head)) 
  34.         cout << "Loop found"
  35.     else 
  36.         cout << "No Loop"
  37.   
  38.     return 0; 

復雜度分析:

時間復雜度: O(n)。
只需循環一次即可。

輔助空間: O(n)。
n是將值存儲在哈希圖中所需的空間。

解決方案2:通過修改鏈表數據結構,無需哈希圖即可解決此問題。
方法:此解決方案需要修改基本鏈表數據結構。

  • 每個節點都有一個訪問標志。
  • 遍歷鏈接列表并繼續標記訪問的節點。
  • 如果您再次看到一個訪問過的節點,那么就會有一個循環。該解決方案適用于O(n),但每個節點都需要其他信息。
  • 此解決方案的一種變體不需要修改基本數據結構,可以使用哈希來實現,只需將訪問的節點的地址存儲在哈希中,如果您看到哈希中已經存在的地址,則存在一個循環。

C++:

  1. #include <bits/stdc++.h> 
  2. using namespace std; 
  3. struct Node { 
  4.     int data; 
  5.     struct Node* next
  6.     int flag; 
  7. }; 
  8.   
  9. void push(struct Node** head_ref, int new_data) 
  10.     struct Node* new_node = new Node; 
  11.     new_node->data = new_data; 
  12.   
  13.     new_node->flag = 0; 
  14.     new_node->next = (*head_ref); 
  15.     (*head_ref) = new_node; 
  16. bool detectLoop(struct Node* h) 
  17.     while (h != NULL) { 
  18.         if (h->flag == 1) 
  19.             return true
  20.         h->flag = 1; 
  21.   
  22.         h = h->next
  23.     } 
  24.   
  25.     return false
  26. int main() 
  27.     struct Node* head = NULL
  28.   
  29.     push(&head, 20); 
  30.     push(&head, 4); 
  31.     push(&head, 15); 
  32.     push(&head, 10); 
  33.     head->next->next->next->next = head; 
  34.   
  35.     if (detectLoop(head)) 
  36.         cout << "Loop found"
  37.     else 
  38.         cout << "No Loop"
  39.   
  40.     return 0; 

復雜度分析:

時間復雜度: O(n)。
只需循環一次即可。

輔助空間: O(1)。
不需要額外的空間。

解決方案3:Floyd的循環查找算法
方法:這是最快的方法,下面進行了介紹:

  • 使用兩個指針遍歷鏈表。
  • 將一個指針(slow_p)移動一個,將另一個指針(fast_p)移動兩個。
  • 如果這些指針在同一節點相遇,則存在循環。如果指針不符合要求,則鏈接列表沒有循環。

Floyd的循環查找算法的實現:

  1. #include <bits/stdc++.h> 
  2. using namespace std; 
  3. class Node { 
  4. public
  5.     int data; 
  6.     Node* next
  7. }; 
  8.   
  9. void push(Node** head_ref, int new_data) 
  10.     Node* new_node = new Node(); 
  11.     new_node->data = new_data; 
  12.     new_node->next = (*head_ref); 
  13.     (*head_ref) = new_node; 
  14.   
  15. int detectLoop(Node* list) 
  16.     Node *slow_p = list, *fast_p = list; 
  17.   
  18.     while (slow_p && fast_p && fast_p->next) { 
  19.         slow_p = slow_p->next
  20.         fast_p = fast_p->next->next
  21.         if (slow_p == fast_p) { 
  22.             return 1; 
  23.         } 
  24.     } 
  25.     return 0; 
  26. int main() 
  27.     Node* head = NULL
  28.   
  29.     push(&head, 20); 
  30.     push(&head, 4); 
  31.     push(&head, 15); 
  32.     push(&head, 10); 
  33.     head->next->next->next->next = head; 
  34.     if (detectLoop(head)) 
  35.         cout << "Loop found"
  36.     else 
  37.         cout << "No Loop"
  38.     return 0; 

解決方案4:在不修改鏈接列表數據結構的情況下標記訪問的節點
在此方法中,將創建一個臨時節點。使遍歷的每個節點的下一個指針指向該臨時節點。這樣,我們將節點的下一個指針用作標志來指示該節點是否已遍歷。檢查每個節點以查看下一個節點是否指向臨時節點。在循環的第一個節點的情況下,第二次遍歷該條件將成立,因此我們發現該循環存在。如果遇到一個指向null的節點,則循環不存在。
下面是上述方法的實現:

  1. #include <bits/stdc++.h> 
  2. using namespace std; 
  3.   
  4. struct Node { 
  5.     int key
  6.     struct Node* next
  7. }; 
  8.   
  9. Node* newNode(int key
  10.     Node* temp = new Node; 
  11.     temp->key = key
  12.     temp->next = NULL
  13.     return temp
  14. void printList(Node* head) 
  15.     while (head != NULL) { 
  16.         cout << head->key << " "
  17.         head = head->next
  18.     } 
  19.     cout << endl; 
  20. bool detectLoop(Node* head) 
  21.     Node* temp = new Node; 
  22.     while (head != NULL) { 
  23.         if (head->next == NULL) { 
  24.             return false
  25.         } 
  26.         if (head->next == temp) { 
  27.             return true
  28.         } 
  29.         Node* nex = head->next
  30.         head->next = temp
  31.         head = nex; 
  32.     } 
  33.   
  34.     return false
  35. int main() 
  36.     Node* head = newNode(1); 
  37.     head->next = newNode(2); 
  38.     head->next->next = newNode(3); 
  39.     head->next->next->next = newNode(4); 
  40.     head->next->next->next->next = newNode(5); 
  41.     head->next->next->next->next->next = head->next->next
  42.   
  43.     bool found = detectLoop(head); 
  44.     if (found) 
  45.         cout << "Loop Found"
  46.     else 
  47.         cout << "No Loop"
  48.   
  49.     return 0; 

復雜度分析:

時間復雜度: O(n)。
只需循環一次即可。

輔助空間: O(1)。
不需要空間。

解決方案5:存放長度

在此方法中,將創建兩個指針,第一個(始終指向頭)和最后一個指針。每次最后一個指針移動時,我們都會計算第一個和最后一個之間的節點數,并檢查當前節點數是否大于先前的節點數,如果是,我們通過移動最后一個指針進行操作,否則就意味著我們已經到達循環的終點,因此我們相應地返回輸出。

  1. #include <bits/stdc++.h> 
  2. using namespace std; 
  3.   
  4. struct Node { 
  5.     int key
  6.     struct Node* next
  7. }; 
  8.   
  9. Node* newNode(int key
  10.     Node* temp = new Node; 
  11.     temp->key = key
  12.     temp->next = NULL
  13.     return temp
  14. void printList(Node* head) 
  15.     while (head != NULL) { 
  16.         cout << head->key << " "
  17.         head = head->next
  18.     } 
  19.     cout << endl; 
  20. int distance(Node* first, Node* last
  21.     int counter = 0; 
  22.   
  23.     Node* curr; 
  24.     curr = first
  25.   
  26.     while (curr != last) { 
  27.         counter += 1; 
  28.         curr = curr->next
  29.     } 
  30.   
  31.     return counter + 1; 
  32. bool detectLoop(Node* head) 
  33.     Node* temp = new Node; 
  34.   
  35.     Node *first, *last
  36.     first = head; 
  37.     last = head; 
  38.     int current_length = 0; 
  39.     int prev_length = -1; 
  40.   
  41.     while (current_length > prev_length && last != NULL) { 
  42.           prev_length = current_length; 
  43.         current_length = distance(firstlast); 
  44.         last = last->next
  45.     } 
  46.       
  47.     if (last == NULL) { 
  48.         return false
  49.     } 
  50.     else {  
  51.         return true
  52.     } 
  53. int main() 
  54.     Node* head = newNode(1); 
  55.     head->next = newNode(2); 
  56.     head->next->next = newNode(3); 
  57.     head->next->next->next = newNode(4); 
  58.     head->next->next->next->next = newNode(5); 
  59.     head->next->next->next->next->next = head->next->next
  60.   
  61.     bool found = detectLoop(head); 
  62.     if (found) 
  63.         cout << "Loop Found"
  64.     else 
  65.         cout << "No Loop Found"
  66.   
  67.     return 0; 

} 

責任編輯:龐桂玉 來源: 今日頭條
相關推薦

2023-10-29 00:55:44

2017-05-04 20:15:51

iOSNSTimer循環引用

2012-03-14 10:58:27

Java

2011-06-16 09:28:02

C++內存泄漏

2021-10-31 20:24:24

C++for循環

2024-10-07 08:26:05

編程Python異常處理

2015-06-10 13:49:53

2023-09-27 23:24:50

C++鏈表

2023-08-28 10:54:09

容器Docker

2009-09-28 10:09:09

Linux內核Linux循環鏈表

2023-11-03 07:58:54

CORSSpring

2010-01-21 09:34:57

C++語法

2010-01-27 16:05:06

C++堆棧

2011-03-04 13:07:47

Filezilla

2009-06-03 16:41:21

Eclipse亂碼Eclipse

2011-07-27 19:05:35

2025-03-19 08:40:00

2011-04-11 11:09:50

this指針

2009-05-31 09:07:35

Oracle鎖定

2011-01-19 17:54:48

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲成人久久久 | 亚洲欧美日韩国产综合 | 国产女人第一次做爰毛片 | 成人免费视频网站在线观看 | 亚洲成人一区二区 | 在线观看免费国产 | 91精品国产91久久久久久最新 | 麻豆久久久久久久久久 | 久久成人免费 | 高清国产午夜精品久久久久久 | 欧美性生交大片免费 | 99色播 | 国产av毛片 | 久久久久精 | 国产精品久久久久久久久图文区 | 黄篇网址| 国产精品无码专区在线观看 | 久久成人18免费网站 | 亚洲黄色av | 国产免费观看一区 | 在线中文字幕av | 亚洲精品乱码久久久久久蜜桃 | 国产一区二区日韩 | 青青艹在线视频 | 日韩精品成人免费观看视频 | 久久精品久久久久久 | 久久久夜色精品亚洲 | 欧美二区在线 | 成人激情视频免费观看 | 国产韩国精品一区二区三区 | 国产成人精品一区二 | 性一交一乱一透一a级 | 综合欧美亚洲 | 一本色道久久综合亚洲精品高清 | 伊人久久大香线 | 亚洲激情视频在线 | 欧美老妇交乱视频 | 国产99久久久久 | 日韩中文字幕一区二区 | 国产一区二区三区四区三区四 | 365夜爽爽欧美性午夜免费视频 |