提供的 `simplelist.h` 是一個**帶頭結點的單鏈表模板類**實現,包含:
| 問題 | 原代碼 | 修正方案 |
|---|---|---|
| 頭文件守衛宏名錯誤 | `#if !defined_LIST_H_` / `#define _LIST_H` | 統一宏名:`#ifndef SIMPLELIST_H` / `#define SIMPLELIST_H` |
| 缺少必要頭文件 | 無 `#include <iostream>` 等 | 添加 ` |
| 模板成員函數定義需在頭文件 | 分離聲明/定義導致鏈接錯誤 | 全部實現放在 `.h` 中,或 `.tpp` 包含在 `.h` 尾部 |
| `output()` 方法語法錯誤 | `cout<data<` | `std::cout << current->data << ' ';` |
| `inputFront` 拼寫不一致 | 聲明 `inputFrond` / 定義 `inputFront` / 調用 `inputFrond` | 統一為 `inputFront` (前插法) |
| 拷貝構造/賦值運算符異常安全 | 先 `new LinkNode` 再拷貝,中途拋異常洩漏 | 採用 Copy-and-Swap 慣用語或智能指針 `std::unique_ptr` |
| 缺少 `const` 正確性 | 多處只讀方法未標 `const` | 檢查所有不修改狀態的方法加 `const` |
| 主函數 `cout l1.inputFrond(-1);` 語法錯誤 | `cout<<...< cout l1.inputFrond(-1);` | 分兩句:`std::cout << "提示..."; l1.inputFront(-1);` |
#ifndef SIMPLELIST_H
#define SIMPLELIST_H
#include
#include
#include
#include
template
class List {
struct Node {
T data;
std::unique_ptr next;
Node(const T& d, std::unique_ptr&& n = nullptr) : data(d), next(std::move(n)) {}
};
std::unique_ptr head = std::make_unique(T{}); // 哨兵節點
public:
List() = default;
List(const List& other) { copyFrom(other); }
List& operator=(List other) { swap(other); return *this; } // Copy-and-Swap
~List() = default; // unique_ptr 自動釋放
void push_front(const T& val) {
head->next = std::make_unique(val, std::move(head->next));
}
void print() const {
for (Node* p = head->next.get(); p; p = p->next.get())
std::cout << p->data << ' ';
std::cout << '\n';
}
// 迭代器支持範圍 for
struct Iterator {
Node* ptr;
T& operator*() { return ptr->data; }
Iterator& operator++() { ptr = ptr->next.get(); return *this; }
bool operator!=(const Iterator& o) const { return ptr != o.ptr; }
};
Iterator begin() { return {head->next.get()}; }
Iterator end() { return {nullptr}; }
private:
void copyFrom(const List& other) {
Node* src = other.head->next.get();
Node** dst = &head->next;
while (src) {
*dst = std::make_unique(src->data);
dst = &(*dst)->next;
src = src->next.get();
}
}
void swap(List& other) noexcept { head.swap(other.head); }
};
#endif // SIMPLELIST_H
#include "simplelist.h"
int main() {
List lst;
std::cout << "輸入整數以 -1 結束: ";
int x;
while (std::cin >> x && x != -1) lst.push_front(x);
std::cout << "列表內容: ";
lst.print(); // 或 for (auto v : lst) std::cout << v << ' ';
List lst2 = lst; // 拷貝構造
std::cout << "拷貝後: ";
lst2.print();
}