先寫通用模板鏈表 template ,再實例化為 List。
struct Student {
int id; string name; int age;
// 建構子、比較運算子...
};
template
class List {
struct Node { T data; Node *next; };
Node *head;
public:
void insert(const T& val) { /* ... */ }
bool erase(const T& val) { /* ... */ }
Node* find(const T& val) { /* ... */ }
// 迭代器支援...
};
int main() {
List stuList;
stuList.insert({1, "Alice", 20});
stuList.insert({2, "Bob", 21});
// ...
}
C++ 模板鏈表完整實作 —— 將範例中的 int 替換為 Student 即可。