首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

C++ 使用類別實現學生鏈表:參考模板鏈表將型別替換為 Student 類別

     編輯:程式設計疑難解答

思路

先寫通用模板鏈表 template class List,再實例化為 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 即可。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved