ที่นี่เราจะดูวิธีหาความยาวของ linked list โดยใช้วิธี iterative และ recursive หากตัวชี้ head กำหนด เราต้องทำตามขั้นตอนเหล่านี้เพื่อหาความยาว
- สำหรับ iterative approach:
- รับ head ของรายการ จนกว่าตัวชี้ current จะเป็น null ให้ไปยังโหนดถัดไปและเพิ่มจำนวนนับ
- สำหรับ recursive approach:
- ส่ง head เป็นอาร์กิวเมนต์ เงื่อนไขฐานคือเมื่ออาร์กิวเมนต์เป็น null ให้คืนค่า 0 มิฉะนั้น ให้เวียนเกิดเข้าไปในรายการและส่งโหนดถัดไปจากโหนดปัจจุบัน คืนค่า 1 + ความยาวของ sublist
ตัวอย่างโค้ด C++
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
void append(struct Node** start, int data) {
struct Node* new_node = new Node;
new_node->data = data;
new_node->next = (*start);
(*start) = new_node;
}
int count_recursive(Node* start) {
if (start == NULL)
return 0;
return 1 + count_recursive(start->next);
}
int count_iterative(Node* start) {
int count = 0;
Node* current = start;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
int main() {
Node* start = NULL;
append(&start, 1);
append(&start, 3);
append(&start, 1);
append(&start, 2);
append(&start, 1);
cout << "Node count using iterative approach: " << count_iterative(start) << endl;
cout << "Node count using recursion: " << count_recursive(start);
}
ผลลัพธ์
Node count using iterative approach: 5 Node count using recursion: 5